-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_BreadthFirstSearch.js
More file actions
99 lines (83 loc) · 1.84 KB
/
Graph_BreadthFirstSearch.js
File metadata and controls
99 lines (83 loc) · 1.84 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
/*
너비 우선 검색
- 첫번째 정점에서 시작해 가능한 한 첫번째 정점과 가까운 정점을 방문
- 배열 대신 큐를 이용해 방문한 정점을 저장
*/
function Graph (v) {
this.vertices = v;
this.edges = 0;
this.adj = [];
for (var i = 0; i < this.vertices; i++) {
this.adj[i] = [];
this.adj[i].push("");
};
this.addEdge = addEdge;
this.showGraph = showGraph;
function addEdge (v, w) {
this.adj[v].push(w);
this.adj[w].push(v);
this.edges++;
}
function showGraph () {
for (var i = 0; i < this.vertices; i++) {
process.stdout.write(i + " -> ");
for (var j = 0; j < this.vertices; j++) {
if (this.adj[i][j] != undefined) {
process.stdout.write(this.adj[i][j] + ' ');
};
};
console.log("");
};
}
this.marked = [];
for (var i = 0; i < this.vertices; i++) {
this.marked[i] = false;
};
this.edgeTo = [];
this.bfs = bfs;
function bfs(s) {
var queue = [];
this.marked[s] = true;
queue.push(s);
while(queue.length > 0) {
var v = queue.shift(); // 큐에서 가져옴
if(v != undefined)
console.log("Visited vertex:", v);
for(var w in this.adj[v]){
var ww = this.adj[v][w];
if(this.marked[ww] == false){
this.edgeTo[ww] = v;
this.marked[ww] = true;
queue.push(ww);
}
}
}
};
this.pathTo = pathTo;
this.hasPathTo = hasPathTo;
function pathTo (v) {
var source = 0;
if(this.hasPathTo(v) == false)
return undefined;
var path = [];
for (var i = v; i != source; i = this.edgeTo[i])
path.push(i);
path.push(source);
return path;
}
function hasPathTo (v) {
return this.marked[v];
}
}
var g = new Graph(5);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(2, 4);
g.showGraph();
g.bfs(0);
// 최단 경로 찾기
var vertex = 4;
var paths = g.pathTo(vertex);
paths.reverse();
console.log(paths.join('-'));