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
21 lines (21 loc) · 831 Bytes
/
code.js
File metadata and controls
21 lines (21 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function DFS(node, parent) { // node = current node, parent = previous node
tracer._visit(node, parent)._wait();
D[node] = true; // label current node as discovered
for (var i = 0; i < G[node].length; i++) {
if (G[node][i]) { // if the edge from current node to the i-th node exists
if (!D[i]) { // if the i-th node is not labeled as discovered
DFS(i, node); // recursively call DFS
}
}
}
D[node] = false; // label current node as undiscovered
tracer._leave(node, parent)._wait();
}
var D; // D[i] indicates whether the i-th node is discovered or not
for (var i = 0; i < G.length; i++) { // start from every node
D = [];
for (var j = 0; j < G.length; j++) D.push(false);
logger._print('start from ' + i);
DFS(i);
tracer._clear();
}