File tree Expand file tree Collapse file tree 3 files changed +49
-1
lines changed
algorithm/graph_search/dfs Expand file tree Collapse file tree 3 files changed +49
-1
lines changed Original file line number Diff line number Diff line change 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 ) ;
You can’t perform that action at this time.
0 commit comments