-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre.js
54 lines (40 loc) · 968 Bytes
/
re.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
class Graph{
constructor (){
this.vertices = {}
}
addVertex(v){
if(!this.vertices[v]){
this.vertices[v] = []
}
}
addEdge(v1 , v2){
this.vertices[v1].push(v2)
this.vertices[v2].push(v1)
}
bfs(v){
const visited = {}
const queue = []
visited[v] = true
queue.push(v)
console.log(visited);
while(queue.length > 0){
const current = queue.shift()
for(let a of this.vertices[current]){
if(!visited[a]){
visited[a] = true
queue.push(a)
console.log("Visited after adding", a, ":", visited);
}
}
}
}
}
const graph = new Graph()
graph.addVertex(1)
graph.addVertex(3)
graph.addVertex(5)
graph.addVertex(8)
graph.addVertex(2)
graph.addEdge(1, 5);
graph.bfs(1)
// console.log(graph);