-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.js
118 lines (96 loc) · 2.34 KB
/
bfs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Define a class Node
function Node(data, adjacent) {
this.data = data;
adjacent = adjacent ? adjacent : [];
this.adjacent = adjacent;
};
// Helper function for connecting Nodes
Node.prototype.addAdjacent = function(node) {
this.adjacent.push(node);
};
// Helper function for outputting a Node
Node.prototype.log = function() {
console.log(this.data.toString());
};
// Create some Nodes
var node1 = new Node('1');
var node2 = new Node('2');
var node3 = new Node('3');
var node4 = new Node('4');
var node5 = new Node('5');
// Add some relationships
node1.addAdjacent(node2);
node1.addAdjacent(node3);
node2.addAdjacent(node3);
node2.addAdjacent(node4);
node4.addAdjacent(node5);
// We'll define the Graph as starting from node1
var graph = node1;
// Define Breadth First Search
var BFS = function(root, visit) {
var queue = [];
root.visited = true;
visit(root);
queue.push(root);
var current;
while(queue.length !== 0) {
current = queue.shift();
current.adjacent.forEach(function(node){
if (!node.visited) {
node.visited = true;
visit(node);
queue.push(node);
}
});
}
};
// First we'll make sure that we visited all the Nodes
// in the graph
console.log('The Nodes in the graph are:');
// Run it
BFS(graph, function(node){
node.log();
});
// How about we try something more interesting than
// saying the value of each Node?
console.log('And now for something completely different...');
// Wipe out the last run
node1.visited = false;
node2.visited = false;
node3.visited = false;
node4.visited = false;
node5.visited = false;
// Let's find the shortest path between two Nodes
var start = node1;
var end = node5;
console.log('The shortest path between ' + end.data + ' and ' + start.data + ' is:');
// A varient of BFS to help us solve the above problem
var BFS2 = function(root, visit) {
var queue = [];
root.visited = true;
visit(root, null);
queue.push(root);
var current;
while(queue.length !== 0) {
current = queue.shift();
current.adjacent.forEach(function(node){
if (!node.visited) {
node.visited = true;
visit(node, current);
queue.push(node);
}
});
}
};
// Run it!
BFS2(start, function(node, previous){
node.previous = previous;
if (node.data === end.data) {
var current = node;
current.log();
while(current.previous) {
current.previous.log();
current = current.previous;
}
}
});