You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Breadth-First Search/README.markdown
+99-46Lines changed: 99 additions & 46 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Breadth-First Search
2
2
3
-
Breadth-first search (BFS) is an algorithm for traversing or searching [tree](../Tree/) or [graph](../Graph/) data structures. It starts at the tree source and explores the neighbor nodes first, before moving to the next level neighbors.
3
+
Breadth-first search (BFS) is an algorithm for traversing or searching [tree](../Tree/) or [graph](../Graph/) data structures. It starts at the tree source and explores the immediate neighbor nodes first, before moving to the next level neighbors.
4
4
5
5
## Animated example
6
6
@@ -9,49 +9,67 @@ Breadth-first search (BFS) is an algorithm for traversing or searching [tree](..
9
9
Let's follow the animated example by using a [queue](../Queue/).
10
10
11
11
Start with the source node ``a`` and add it to a queue.
12
+
12
13
```swift
13
14
queue.enqueue(a)
14
15
```
15
-
The queue is now ``[ a ]``. Dequeue ``a`` and enqueue the neighbor nodes ``b`` and ``c``.
16
+
17
+
The queue is now ``[ a ]``. Dequeue ``a`` and enqueue its two neighbor nodes ``b`` and ``c``.
18
+
16
19
```swift
17
-
queue.dequeue(a)
20
+
queue.dequeue() // a
18
21
queue.enqueue(b)
19
22
queue.enqueue(c)
20
23
```
21
-
The queue is now ``[ b, c ]``. Dequeue ``b`` and enqueue the neighbor nodes ``d`` and ``e``.
24
+
25
+
The queue is now ``[ b, c ]``. Dequeue ``b`` and enqueue `b`'s neighbor nodes ``d`` and ``e``.
26
+
22
27
```swift
23
-
queue.dequeue(b)
28
+
queue.dequeue() // b
24
29
queue.enqueue(d)
25
30
queue.enqueue(e)
26
31
```
27
-
The queue is now ``[ c, d, e ]``. Dequeue ``c`` and enqueue the neighbor nodes ``f`` and ``g``.
32
+
33
+
The queue is now ``[ c, d, e ]``. Dequeue ``c`` and enqueue `c`'s neighbor nodes ``f`` and ``g``.
34
+
28
35
```swift
29
-
queue.dequeue(c)
36
+
queue.dequeue() // c
30
37
queue.enqueue(f)
31
38
queue.enqueue(g)
32
39
```
40
+
33
41
The queue is now ``[ d, e, f, g ]``. Dequeue ``d`` which has no neighbor nodes.
42
+
34
43
```swift
35
-
queue.dequeue(d)
44
+
queue.dequeue() // d
36
45
```
37
-
The queue is now ``[ e, f, g ]``. Dequeue ``e`` and enqueue the single neighbor node ``h``.
46
+
47
+
The queue is now ``[ e, f, g ]``. Dequeue ``e`` and enqueue its single neighbor node ``h``.
48
+
38
49
```swift
39
-
queue.dequeue(e)
50
+
queue.dequeue() // e
40
51
queue.enqueue(h)
41
52
```
53
+
42
54
The queue is now ``[ f, g, h ]``. Dequeue ``f`` which has no neighbor nodes.
55
+
43
56
```swift
44
-
queue.dequeue(f)
57
+
queue.dequeue() // f
45
58
```
59
+
46
60
The queue is now ``[ g, h ]``. Dequeue ``g`` which has no neighbor nodes.
61
+
47
62
```swift
48
-
queue.dequeue(g)
63
+
queue.dequeue() // g
49
64
```
65
+
50
66
The queue is now ``[ h ]``. Dequeue ``h`` which has no neighbor nodes.
67
+
51
68
```swift
52
-
queue.dequeue(h)
69
+
queue.dequeue() // h
53
70
```
54
-
The queue is now empty which means all nodes have been explored.
71
+
72
+
The queue is now empty, meaning that all nodes have been explored. The order in which the nodes were explored is `a`, `b`, `c`, `d`, `e`, `f`, `g`, `h`.
let nodesExplored =breadthFirstSearch(graph, source: nodeA)
107
-
print(nodesExplored)// This will output: ["a", "b", "c", "d", "e", "f", "g", "h"]
125
+
print(nodesExplored)
108
126
```
109
127
128
+
This will output: `["a", "b", "c", "d", "e", "f", "g", "h"]`
129
+
110
130
## Applications
111
131
112
132
Breadth-first search can be used to solve many problems, for example:
113
133
114
-
* Computing the shortest path between a source node and each of the other nodes
115
-
* Only for unweighted graphs
134
+
* Computing the shortest path between a source node and each of the other nodes (only for unweighted graphs)
116
135
* Calculating the minimum spanning tree on an unweighted graph
117
136
118
137
## Shortest path example
119
138
120
-
Breadth-first search can be used to compute the [shortest path](../Shortest Path/) between a source node and each of the other nodes because it explores all of the neighbor nodes before moving to the next level neighbors. Let's follow the animated example and calculate the shortest path to all the other nodes:
139
+
Breadth-first search can be used to compute the [shortest path](../Shortest Path/) between a source node and each of the other nodes in the tree or graph, because it explores all of the immediate neighbor nodes first before moving to the next level neighbors.
140
+
141
+
Let's follow the animated example and calculate the shortest path to all the other nodes. Start with the source node ``a`` and add it to a queue with a distance of ``0``.
121
142
122
-
Start with the source node ``a`` and add it to a queue with a distance of ``0``.
123
143
```swift
124
144
queue.enqueue(a)
125
145
a.distance=0
126
146
```
127
-
The queue is now ``[ a ]``. Dequeue ``a`` and enqueue the neighbor nodes ``b`` and ``c`` with a distance of ``1``.
147
+
148
+
The queue is now ``[ a ]``. Dequeue ``a`` and enqueue its two neighbor nodes ``b`` and ``c`` with a distance of ``1``.
149
+
128
150
```swift
129
-
queue.dequeue(a)
151
+
queue.dequeue() // a
130
152
queue.enqueue(b)
131
-
b.distance= a.distance+1// result: 1
153
+
b.distance= a.distance+1// result: 1
132
154
queue.enqueue(c)
133
-
c.distance= a.distance+1// result: 1
155
+
c.distance= a.distance+1// result: 1
134
156
```
135
-
The queue is now ``[ b, c ]``. Dequeue ``b`` and enqueue the neighbor nodes ``d`` and ``e`` with a distance of ``2``.
157
+
158
+
The queue is now ``[ b, c ]``. Dequeue ``b`` and enqueue `b`'s neighbor nodes ``d`` and ``e`` with a distance of ``2``.
159
+
136
160
```swift
137
-
queue.dequeue(b)
161
+
queue.dequeue() // b
138
162
queue.enqueue(d)
139
-
d.distance= b.distance+1// result: 2
163
+
d.distance= b.distance+1// result: 2
140
164
queue.enqueue(e)
141
-
e.distance= b.distance+1// result: 2
165
+
e.distance= b.distance+1// result: 2
142
166
```
143
167
144
168
Continue until the queue is empty to calculate the shortest path to all other nodes.
Breadth-first search can be used to calculate the [minimum spanning tree](../Minimum Spanning Tree/) on an unweighted graph.
211
+
Breadth-first search can be used to calculate the [minimum spanning tree](../Minimum Spanning Tree/) on an unweighted graph. A minimum spanning tree describes a path that contains the smallest number of edges that are needed to visit every node in the graph.
185
212
186
213
Let's calculate the minimum spanning tree for the following graph:
187
214
@@ -190,71 +217,92 @@ Let's calculate the minimum spanning tree for the following graph:
190
217
*Note: the minimum spanning tree is represented by the bold edges.*
191
218
192
219
Start with the source node ``a`` and add it to a queue and mark it as visited.
220
+
193
221
```swift
194
222
queue.enqueue(a)
195
223
a.visited=true
196
224
```
197
-
The queue is now ``[ a ]``. Dequeue ``a`` and enqueue the neighbor nodes ``b`` and ``h`` and mark them as visited.
225
+
226
+
The queue is now ``[ a ]``. Dequeue ``a`` and enqueue its immediate neighbor nodes ``b`` and ``h`` and mark them as visited.
227
+
198
228
```swift
199
-
queue.dequeue(a)
229
+
queue.dequeue() // a
200
230
queue.enqueue(b)
201
231
b.visited=true
202
232
queue.enqueue(h)
203
233
h.visited=true
204
234
```
235
+
205
236
The queue is now ``[ b, h ]``. Dequeue ``b`` and enqueue the neighbor node ``c`` mark it as visited. Remove the edge between ``b`` to ``h`` because ``h`` has already been visited.
237
+
206
238
```swift
207
-
queue.dequeue(b)
239
+
queue.dequeue() // b
208
240
queue.enqueue(c)
209
241
c.visited=true
210
242
b.removeEdgeTo(h)
211
243
```
244
+
212
245
The queue is now ``[ h, c ]``. Dequeue ``h`` and enqueue the neighbor nodes ``g`` and ``i`` and mark them as visited.
246
+
213
247
```swift
214
-
queue.dequeue(h)
248
+
queue.dequeue() // h
215
249
queue.enqueue(g)
216
250
g.visited=true
217
251
queue.enqueue(i)
218
252
i.visited=true
219
253
```
254
+
220
255
The queue is now ``[ c, g, i ]``. Dequeue ``c`` and enqueue the neighbor nodes ``d`` and ``f`` and mark them as visited. Remove the edge between ``c`` to ``i`` because ``i`` has already been visited.
256
+
221
257
```swift
222
-
queue.dequeue(c)
258
+
queue.dequeue() // c
223
259
queue.enqueue(d)
224
260
d.visited=true
225
261
queue.enqueue(f)
226
262
f.visited=true
227
263
c.removeEdgeTo(i)
228
264
```
265
+
229
266
The queue is now ``[ g, i, d, f ]``. Dequeue ``g`` and remove the edges between ``g`` to ``f`` and ``g`` to ``i`` because ``f`` and ``i`` have already been visited.
267
+
230
268
```swift
231
-
queue.dequeue(g)
269
+
queue.dequeue() // g
232
270
g.removeEdgeTo(f)
233
271
g.removeEdgeTo(i)
234
272
```
273
+
235
274
The queue is now ``[ i, d, f ]``. Dequeue ``i``.
275
+
236
276
```swift
237
-
queue.dequeue(i)
277
+
queue.dequeue() // i
238
278
```
279
+
239
280
The queue is now ``[ d, f ]``. Dequeue ``d`` and enqueue the neighbor node ``e`` mark it as visited. Remove the edge between ``d`` to ``f`` because ``f`` has already been visited.
281
+
240
282
```swift
241
-
queue.dequeue(d)
283
+
queue.dequeue() // d
242
284
queue.enqueue(e)
243
285
e.visited=true
244
286
d.removeEdgeTo(f)
245
287
```
288
+
246
289
The queue is now ``[ f, e ]``. Dequeue ``f``. Remove the edge between ``f`` to ``e`` because ``e`` has already been visited.
290
+
247
291
```swift
248
-
queue.dequeue(f)
292
+
queue.dequeue() // f
249
293
f.removeEdgeTo(e)
250
294
```
295
+
251
296
The queue is now ``[ e ]``. Dequeue ``e``.
297
+
252
298
```swift
253
-
queue.dequeue(e)
299
+
queue.dequeue() // e
254
300
```
255
-
The queue is now empty which means the minimum spanning tree has been computed.
301
+
302
+
The queue is now empty, which means the minimum spanning tree has been computed.
This function returns a new `Graph` object that describes just the minimum spanning tree. In the figure, that would be the graph containing just the bold edges.
333
+
283
334
Put this code in a playground and test it like so:
0 commit comments