File tree Expand file tree Collapse file tree 3 files changed +0
-76
lines changed
Expand file tree Collapse file tree 3 files changed +0
-76
lines changed Original file line number Diff line number Diff line change 33const Node = require ( './graphGenerator' ) ;
44
55const BFS = ( start ) => {
6- // initailize the open to be the nodes we are currently exploring
7- let open = [ ] ;
8- open . push ( start ) ;
96
10- // Keep track of the nodes we have already visited, so we don't repeat nodes
11- let visited_nodes = [ start ] ;
12- let searched_path = [ ] ;
13-
14- // Keep checking nodes until our open array is empty
15- while ( open . length > 0 ) {
16-
17- // Pull the first item off of our queue
18- let current = open . shift ( ) ;
19-
20- // Add the current node to our search path stack
21- searched_path . push ( current ) ;
22-
23- //Iterate through all of the neighbors of the current node
24- current . neighbors . forEach ( ( next ) => {
25-
26- // If we haven't already visisted a node, add it to our visisted stack, and add it to our open queue
27- if ( visited_nodes . indexOf ( next ) < 0 ) {
28- visited_nodes . push ( next ) ;
29- open . push ( next ) ;
30- }
31- } ) ;
32- }
33-
34- //Once we have traversed the whole graph - return the search path
35- return searched_path ;
367}
378
389module . exports = BFS ;
Original file line number Diff line number Diff line change 22
33const Node = require ( './graphGenerator' ) ;
44
5- /*
6- 1 procedure DFS(G,v):
7- 2 label v as discovered
8- 3 for all edges from v to w in G.adjacentEdges(v) do
9- 4 if vertex w is not labeled as discovered then
10- 5 recursively call DFS(G,w)
11- */
125const DFS = ( start , searchFor ) => {
13- if ( ! searchFor || ! start ) {
14- throw new Error ( 'Invalid input' ) ;
15- }
166
17- //If the node we are searching for
18- if ( searchFor === start . value ) {
19- return start ;
20- }
21- let i ;
22- let child ;
23- let found ;
24- let children = start . neighbors ;
25-
26- // iterate through all of the starting nodes children
27- for ( i = 0 ; i < children . length ; i ++ ) {
28- child = children [ i ] ;
29-
30- //Recursviely call the child nodes until we find
31- found = DFS ( child , searchFor ) ;
32-
33- // If we find the item we are searching for - return the node
34- if ( found ) {
35- return found ;
36- }
37- }
38- // If we cannot find the node - return false;
39- return false ;
407}
418
429module . exports = DFS ;
Original file line number Diff line number Diff line change 11// Implement a graph generator that represents a set of objects where some pairs of objects are connected by links.
22
33const Node = function ( name , value ) {
4- this . name = name ;
5- this . value = value ;
6- this . neighbors = [ ] ;
7- }
8-
9- Node . prototype . addNeighbors = function ( neighbors ) {
10- this . neighbors = this . neighbors . concat ( neighbors ) ;
11- return this . neighbors ;
12- }
134
14- Node . prototype . neighbors = function ( ) {
15- return this . neighbors ;
165}
176
18- Node . prototype . toString = function ( ) {
19- return this . name ;
20- }
217
228module . exports = Node ;
You can’t perform that action at this time.
0 commit comments