-
Notifications
You must be signed in to change notification settings - Fork 5
/
2017-03-21-bfs.html
120 lines (96 loc) · 3.42 KB
/
2017-03-21-bfs.html
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
116
117
118
119
120
<title>Academy Pittsburgh Theory Session: Breadth-First Search</title>
<h1>Breadth-First Search (BFS) using Queues</h1>
<i>by David T. Allen (Lecture time: ~40 minutes)</i>
<p>Look at the console to see see the results of BFS and get information about what happens at every step.</p>
<p>Remember, BFS uses a first-in-first-out queue, which is like taking a number at the deli counter. Every customer that showed up at the deli is served before you.</p>
<p>Important terminology: a queue is a <b>data structure</b> and breadth-first search is an <b>algorithm</b>.</p>
<p>Consider a full tree, which is a tree where every node has a left and right child. In terms of <i>space</i> performance, almost every time you dequeue an element, you enqueue two more. That means the queue grows by one element after every comparison.</p>
<p>One advantage of BFS over DFS (depth-first search) is BFS can find the shortest path between two nodes on a graph, assuming all connected nodes are equal distances apart. In other words, it will give the shortest path when solving a maze but neither DFS nor BFS alone will give the shortest path on a map where a city is a node, because roads are different distances.</p>
<p>These reference links may be helpful:</p>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Breadth-first_search#/media/File:Animated_BFS.gif">Wikipedia GIF that explains BFS</a></li>
<li><a href="https://en.wikipedia.org/wiki/Breadth-first_search">Wikipedia page about BFS</a></li>
</ul>
<script>
class Queue {
constructor() {
this.data_array = [];
console.log("Instantiated a Queue");
}
Enqueue(node) {
this.data_array.push(node);
console.log("-- Enqueued " + node.value)
}
Dequeue() {
var node = this.data_array.shift();
console.log("Dequeued " + node.value)
return node;
}
IsEmpty() {
return this.data_array.length <= 0;
}
}
// The tree:
//
// A
// / \
// B C
// / \ / \
// D E F G
var tree = {
value : 'A',
left : {
value : 'B',
left : {
value : 'D'
},
right : {
value : 'E'
}
},
right : {
value : 'C',
left : {
value : 'F'
},
right : {
value : 'G'
}
}
}
function BFS(tree_root, search_value) {
var Q = new Queue();
Q.Enqueue(tree_root);
while (!Q.IsEmpty()) {
var node = Q.Dequeue();
if (node.value == search_value) {
console.log("Found " + node.value);
return node;
}
if ('left' in node && node.left != null) {
Q.Enqueue(node.left);
}
if ('right' in node && node.right != null) {
Q.Enqueue(node.right);
}
}
console.log("Couldn't find " + search_value);
return null;
}
var node;
console.log("==============================");
console.log("Searching for 'A' (the root)");
node = BFS(tree, 'A');
console.log("\nThe result of BFS(tree, 'A') is:");
console.log(node);
console.log("==============================");
console.log("Searching for 'F'");
node = BFS(tree, 'F');
console.log("\nThe result of BFS(tree, 'F') is:");
console.log(node);
console.log("==============================");
console.log("Searching for 'H' (does not exist)");
node = BFS(tree, 'H');
console.log("\nThe result of BFS(tree, 'H') is:");
console.log(node);
</script>