-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
46 lines (40 loc) · 1013 Bytes
/
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
function Graph(vertexNum, edgeNum, arcs) {
this.vertexNum = vertexNum;
this.edgeNum = edgeNum;
this.arcs = arcs;
}
const output = []
function dfs(graph, visited, vertex) {
visited[vertex] = true;
output.push(vertex);
for (let i = 0; i < graph.vertexNum; ++i) {
if (graph.arcs[vertex][i] == 1 && visited[i] == false) {
dfs(graph, visited, i);
}
}
}
function dfsTraversal(graph) {
const 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] == false) {
dfs(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);
dfsTraversal(graph);
console.log(output);
})()