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/BreadthFirstSearch.playground/Pages/Minimum spanning tree example.xcplaygroundpage/Contents.swift
Copy file name to clipboardExpand all lines: Breadth-First Search/BreadthFirstSearch.playground/Pages/Shortest path example.xcplaygroundpage/Contents.swift
Copy file name to clipboardExpand all lines: Breadth-First Search/README.markdown
+53-38
Original file line number
Diff line number
Diff line change
@@ -4,72 +4,86 @@ Breadth-first search (BFS) is an algorithm for traversing or searching [tree](..
4
4
5
5
## Animated example
6
6
7
-

7
+
Here's how breadth-first search works on a graph:
8
8
9
-
Let's follow the animated example by using a [queue](../Queue/).
9
+

10
10
11
-
Start with the source node ``a`` and add it to a queue.
11
+
When we visit a node, we color it black. We also put its neighbor nodes into a [queue](../Queue/). In the animation the nodes that are enqueued but not visited yet are shown in gray.
12
+
13
+
Let's follow the animated example. We start with the source node `A` and add it to a queue. In the animation this is shown as node `A` becoming gray.
12
14
13
15
```swift
14
-
queue.enqueue(a)
16
+
queue.enqueue(A)
15
17
```
16
18
17
-
The queue is now ``[ a ]``. Dequeue ``a`` and enqueue its two neighbor nodes ``b`` and ``c``.
19
+
The queue is now `[ A ]`. The idea is that, as long as there are nodes in the queue, we visit the node that's at the front of the queue, and enqueue its immediate neighbor nodes if they have not been visited yet.
20
+
21
+
To start traversing the graph, we pull the first node off the queue, `A`, and color it black. Then we enqueue its two neighbor nodes `B` and `C`. This colors them gray.
18
22
19
23
```swift
20
-
queue.dequeue() //a
21
-
queue.enqueue(b)
22
-
queue.enqueue(c)
24
+
queue.dequeue() //A
25
+
queue.enqueue(B)
26
+
queue.enqueue(C)
23
27
```
24
28
25
-
The queue is now ``[ b, c ]``. Dequeue ``b`` and enqueue `b`'s neighbor nodes ``d`` and ``e``.
29
+
The queue is now `[ B, C ]`. We dequeue `B`, and enqueue `B`'s neighbor nodes `D` and `E`.
26
30
27
31
```swift
28
-
queue.dequeue() //b
29
-
queue.enqueue(d)
30
-
queue.enqueue(e)
32
+
queue.dequeue() //B
33
+
queue.enqueue(D)
34
+
queue.enqueue(E)
31
35
```
32
36
33
-
The queue is now ``[ c, d, e ]``. Dequeue ``c`` and enqueue `c`'s neighbor nodes ``f`` and ``g``.
37
+
The queue is now `[ C, D, E ]`. Dequeue `C`, and enqueue `C`'s neighbor nodes `F` and `G`.
34
38
35
39
```swift
36
-
queue.dequeue() //c
37
-
queue.enqueue(f)
38
-
queue.enqueue(g)
40
+
queue.dequeue() //C
41
+
queue.enqueue(F)
42
+
queue.enqueue(G)
39
43
```
40
44
41
-
The queue is now ``[ d, e, f, g ]``. Dequeue ``d`` which has no neighbor nodes.
45
+
The queue is now `[ D, E, F, G ]`. Dequeue `D`, which has no neighbor nodes.
42
46
43
47
```swift
44
-
queue.dequeue() //d
48
+
queue.dequeue() //D
45
49
```
46
50
47
-
The queue is now ``[ e, f, g ]``. Dequeue ``e`` and enqueue its single neighbor node ``h``.
51
+
The queue is now `[ E, F, G ]`. Dequeue `E` and enqueue its single neighbor node `H`. Note that `B` is also a neighbor for `E` but we've already visited `B`, so we're not adding it to the queue again.
48
52
49
53
```swift
50
-
queue.dequeue() //e
51
-
queue.enqueue(h)
54
+
queue.dequeue() //E
55
+
queue.enqueue(H)
52
56
```
53
57
54
-
The queue is now ``[ f, g, h ]``. Dequeue ``f`` which has no neighbor nodes.
58
+
The queue is now `[ F, G, H ]`. Dequeue `F`, which has no unvisited neighbor nodes.
55
59
56
60
```swift
57
-
queue.dequeue() //f
61
+
queue.dequeue() //F
58
62
```
59
63
60
-
The queue is now ``[ g, h ]``. Dequeue ``g`` which has no neighbor nodes.
64
+
The queue is now `[ G, H ]`. Dequeue `G`, which has no unvisited neighbor nodes.
61
65
62
66
```swift
63
-
queue.dequeue() //g
67
+
queue.dequeue() //G
64
68
```
65
69
66
-
The queue is now ``[ h ]``. Dequeue ``h`` which has no neighbor nodes.
70
+
The queue is now `[ H ]`. Dequeue `H`, which has no unvisited neighbor nodes.
67
71
68
72
```swift
69
-
queue.dequeue() //h
73
+
queue.dequeue() //H
70
74
```
71
75
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`.
76
+
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`.
77
+
78
+
We can show this as a tree:
79
+
80
+

81
+
82
+
The parent of a node is the one that "discovered" that node. The root of the tree is the node you started the breadth-first search from.
83
+
84
+
For an unweighted graph, this tree defines a shortest path from the starting node to every other node in the tree. So breadth-first search is one way to find the shortest path between two nodes in a graph.
85
+
86
+
Breadth-first search can be used on directed and undirected graphs.
0 commit comments