forked from Graph-Visualization/graph-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.js
59 lines (49 loc) · 1.2 KB
/
bfs.js
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
const GraphBase = require('./graph-base');
const Queue = require('./ds/queue');
class BFS extends GraphBase {
constructor(numVertices = 0, numEdges = 0, startingPoint = 0) {
super(numVertices, numEdges);
this.startingPoint = startingPoint;
}
bfs() {
let queue = new Queue();
let Adj = this.getSimpleAdj();
let visited = [];
let vertex = [];
Adj.forEach((value, key) => {
visited[key] = false;
vertex.push(key);
});
let root = vertex[0];
queue.enqueue(root);
let bfs_order = [];
while (!queue.isEmpty()) {
let node = queue.dequeue();
if (!visited[node]) {
bfs_order.push(node);
visited[node] = true;
}
for (let i = 0; i < Adj.get(node).length; i++) {
if (!visited[Adj.get(node)[i]]) {
queue.enqueue(Adj.get(node)[i]);
}
}
}
return bfs_order;
}
}
// const g = new BFS(5)
// vertices = [0,1,2,3]
// for(let i=0;i<vertices.length;i++)
// {
// g.addVertex(vertices[i])
// }
// g.addEdge(0,1);
// g.addEdge(0, 2);
// g.addEdge(1,2);
// g.addEdge(2,0);
// g.addEdge(2,3);
// g.addEdge(3,3);
// console.log(g.getSimpleAdj())
// console.log(g.bfs())
module.exports = BFS;