-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphSearch.ts
More file actions
198 lines (191 loc) · 5.43 KB
/
graphSearch.ts
File metadata and controls
198 lines (191 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import AdjacencyListGraph from '@datastructure/Graph/AdjacencyListGraph';
import AdjacencyMatrixGraph from '@datastructure/Graph/AdjacencyMatrixGraph';
export class SearchAdjMatrixGraph extends AdjacencyMatrixGraph {
private vistedVertex: { [key: string]: boolean };
constructor() {
super();
this.vistedVertex = {};
}
resetVisted() {
let init: { [key: string]: boolean } = {};
this.vistedVertex = Object.keys(this.vistedVertex).reduce((acc, cur) => {
acc[cur] = false;
return acc;
}, init);
return this;
}
isVisited(targetIdx: number) {
const targetVertex = this.vertexMap[targetIdx];
return targetVertex && !this.vistedVertex[targetVertex];
}
dfs(vertex: string) {
this.resetVisted();
const vertexIdx = this.findVertexIdx(vertex);
if (vertexIdx === -1) {
console.log('입력한 정점이 없습니다.');
return this;
}
const result = [];
const stack = [];
stack.push(vertex);
while (stack.length > 0) {
const current = stack.pop();
if (!current) break;
if (this.vistedVertex[current]) continue;
this.vistedVertex[current] = true;
result.push(current);
const currentIdx = this.findVertexIdx(current);
this.nodes[currentIdx].forEach((isLinked, idx) => {
if (this.isVisited(idx) && isLinked !== 0) {
stack.push(this.vertexMap[idx]);
}
});
}
console.log('dfs with stack :>> ', result.join(' '));
return this;
}
dfsRecur(vertex: string) {
this.resetVisted();
const result: Array<string> = [];
const recur = (targetVertex: string) => {
this.vistedVertex[targetVertex] = true;
result.push(targetVertex);
const targetIdx = this.findVertexIdx(targetVertex);
this.nodes[targetIdx].forEach((isLinked, idx) => {
if (isLinked !== 0 && this.isVisited(idx)) {
const next = this.vertexMap[idx];
if (next) recur(next);
}
});
};
recur(vertex);
console.log('dfs with recursive :>> ', result.join(' '));
return this;
}
bfs(vertex: string) {
this.resetVisted();
const result = [];
const queue: Array<string> = [];
queue.push(vertex);
this.vistedVertex[vertex] = true;
while (queue.length > 0) {
const current = queue.shift();
if (!current) continue;
result.push(current);
const currentIdx = this.findVertexIdx(current);
this.nodes[currentIdx].forEach((isLinked, idx) => {
if (this.isVisited(idx) && isLinked !== 0) {
const next = this.vertexMap[idx];
if (next) {
this.vistedVertex[next] = true;
queue.push(next);
}
}
});
}
console.log('bfs :>> ', result.join(' '));
return this;
}
}
export class SearchAdjListGraph extends AdjacencyListGraph {
private vistedVertex: { [key: string]: boolean };
constructor() {
super();
this.vistedVertex = {};
}
resetVisted() {
let init: { [key: string]: boolean } = {};
this.vistedVertex = Object.keys(this.vistedVertex).reduce((acc, cur) => {
acc[cur] = false;
return acc;
}, init);
return this;
}
dfs(vertex: string) {
this.resetVisted();
if (!this.nodes.has(vertex)) {
console.log('입력한 정점이 없습니다.');
return this;
}
const result = [];
const stack = [];
stack.push(vertex);
while (stack.length > 0) {
const current = stack.pop();
if (!current) continue;
if (this.vistedVertex[current]) continue;
this.vistedVertex[current] = true;
result.push(current);
this.nodes.get(current)?.forEach((linkedVertex) => {
if (!this.vistedVertex[linkedVertex]) {
stack.push(linkedVertex);
}
});
}
console.log('dfs with stack :>> ', result.join(' '));
return this;
}
dfsRecur(vertex: string) {
this.resetVisted();
const result: Array<string> = [];
const recur = (targetVertex: string) => {
this.vistedVertex[targetVertex] = true;
result.push(targetVertex);
this.nodes.get(targetVertex)?.forEach((linkedVertex) => {
if (!this.vistedVertex[linkedVertex]) {
recur(linkedVertex);
}
});
};
recur(vertex);
console.log('dfs with recursive :>> ', result.join(' '));
return this;
}
bfs(vertex: string) {
this.resetVisted();
const result = [];
const queue: Array<string> = [];
queue.push(vertex);
this.vistedVertex[vertex] = true;
while (queue.length > 0) {
const current = queue.shift();
if (!current) continue;
result.push(current);
this.nodes.get(current)?.forEach((linkedVertex) => {
if (!this.vistedVertex[linkedVertex]) {
this.vistedVertex[linkedVertex] = true;
queue.push(linkedVertex);
}
});
}
console.log('bfs :>> ', result.join(' '));
return this;
}
}
// const graph = new AdjacencyMatrixGraph();
// const graph = new SearchAdjMatrixGraph();
// graph.create(8);
// const graph = new AdjacencyListGraph();
const graph = new SearchAdjListGraph();
graph
.addVertex('A')
.addVertex('B')
.addVertex('C')
.addVertex('D')
.addVertex('E')
.addVertex('F')
.addVertex('G')
.addVertex('H')
.addEdge('A', 'B')
.addEdge('A', 'C')
.addEdge('B', 'D')
.addEdge('C', 'D')
.addEdge('C', 'E')
.addEdge('D', 'F')
.addEdge('E', 'G')
.addEdge('E', 'H')
.addEdge('G', 'H')
.dfs('A')
.dfsRecur('A')
.bfs('A');
graph.display();