Skip to content

Commit 8752bec

Browse files
author
Raghav Dua
committed
Merge pull request algorithm-visualizer#52 from duaraghav8/master
Added DFS Exploration (determine connectedness of Graph)
2 parents 7a93107 + 680bee7 commit 8752bec

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

algorithm/graph_search/dfs/desc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"tree": "Searching a tree",
2525
"all_paths": "Going through all possible paths without making any circuit",
2626
"weighted_graph": "DFS of Weighted Graph",
27-
"shortest_path": "Finding the shortest path"
27+
"shortest_path": "Finding the shortest path",
28+
"exploration": "Explore an undirected graph to see if it is connected"
2829
}
2930
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function DFSExplore (graph, source) {
2+
var stack = [ [source, null] ], visited = {};
3+
var node, prev, i, temp;
4+
5+
while (stack.length > 0) {
6+
temp = stack.pop ();
7+
node = temp [0];
8+
prev = temp [1];
9+
10+
if (!visited [node]) {
11+
visited [node] = true;
12+
logger._print (node);
13+
14+
if (prev !== undefined && graph [node] [prev]) { tracer._visit (node, prev)._wait (200); console.log ('tracer ' + prev + ', ' + node); }
15+
else { tracer._visit (node)._wait (200); console.log ('tracer ' + node); }
16+
17+
for (i = 0; i < graph.length; i++) {
18+
if (graph [node] [i]) {
19+
stack.push ([i, node]);
20+
}
21+
}
22+
}
23+
}
24+
25+
return visited;
26+
}
27+
28+
29+
var visited = DFSExplore (G, 0);
30+
if (G.length === Object.keys (visited).length) {
31+
logger._print ('The Graph is CONNECTED');
32+
}
33+
else {
34+
logger._print ('The Graph is NOT CONNECTED');
35+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var tracer = new UndirectedGraphTracer ();
2+
var logger = new LogTracer ();
3+
var G = [
4+
[0,1,0,0,0,0],
5+
[1,0,0,1,1,0],
6+
[0,0,0,1,0,0],
7+
[0,1,1,0,1,1],
8+
[0,1,0,1,0,0],
9+
[0,0,0,1,0,0]
10+
];
11+
12+
tracer._setData (G);

0 commit comments

Comments
 (0)