forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
42 lines (36 loc) · 964 Bytes
/
Copy pathcode.js
File metadata and controls
42 lines (36 loc) · 964 Bytes
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
function DFSExplore(graph, source) {
var stack = [[source, null]], visited = [];
var node, prev, i, temp;
for (i = 0; i < graph.length; i++) {
visited.push(false);
}
visitedTracer._setData(visited);
while (stack.length > 0) {
temp = stack.pop();
node = temp [0];
prev = temp [1];
if (!visited[node]) {
visited[node] = true;
visitedTracer._notify(node, visited[node]);
if (prev !== undefined && graph[node][prev]) {
graphTracer._visit(node, prev)._wait();
} else {
graphTracer._visit(node)._wait();
}
for (i = 0; i < graph.length; i++) {
if (graph[node][i]) {
stack.push([i, node]);
}
}
}
}
return visited;
}
var visited = DFSExplore(G, 0);
var check = true;
for (var i = 0; i < visited.length; i++) check &= visited[i];
if (check) {
logger._print('The Graph is CONNECTED');
} else {
logger._print('The Graph is NOT CONNECTED');
}