Skip to content

Commit 563d4f7

Browse files
committed
cleaned up old work
1 parent e7ca224 commit 563d4f7

File tree

3 files changed

+0
-76
lines changed

3 files changed

+0
-76
lines changed

breadthFirstSearch.js

-29
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,7 @@
33
const Node = require('./graphGenerator');
44

55
const 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

389
module.exports = BFS;

depthFirstSearch.js

-33
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,8 @@
22

33
const 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-
*/
125
const 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

429
module.exports = DFS;

graphGenerator.js

-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
// Implement a graph generator that represents a set of objects where some pairs of objects are connected by links.
22

33
const 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

228
module.exports = Node;

0 commit comments

Comments
 (0)