-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
51 lines (45 loc) · 1.11 KB
/
main.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
function Graph(vertexNum, edgeNum, arcs) {
this.vertexNum = vertexNum;
this.edgeNum = edgeNum;
this.arcs = arcs;
}
const output = [];
function bfs(graph, visited, vertex) {
visited[vertex] = true;
const que = [vertex];
while (que.length !== 0) {
const node = que.shift();
output.push(node);
for (let i = 0; i < graph.vertexNum; ++i) {
if (graph.arcs[vertex][i] == 1 && !visited[i]) {
visited[i] = true;
que.push(i);
}
}
}
}
function bfsTraversal(graph) {
let visited = new Array(graph.vertexNum);
for (let i = 0; i < graph.vertexNum; ++i) {
visited[i] = false;
}
for (let i = 0; i < graph.vertexNum; ++i) {
if (!visited[i]) {
bfs(graph, visited, i);
}
}
}
(function() {
const vertexNum = 5;
const edgeNum = 7;
const adjMat = [
[0,1,1,0,1],
[1,0,0,1,1],
[1,0,0,1,0],
[0,1,1,0,1],
[1,1,0,1,0]
];
const graph = new Graph(vertexNum, edgeNum, adjMat);
bfsTraversal(graph);
console.log(output);
})()