Skip to content

Commit ae60a89

Browse files
author
Chris Pilcher
committed
BFS adding readme documentation for calculating minimum spanning tree
1 parent 7b42056 commit ae60a89

File tree

1 file changed

+145
-3
lines changed

1 file changed

+145
-3
lines changed

Breadth-First Search/README.markdown

+145-3
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,156 @@ breadthFirstSearchShortestPath(graph, source: nodeA)
175175

176176
Breadth-first search can be used to calculate the [minimum spanning tree](../Minimum Spanning Tree/) on an unweighted graph.
177177

178+
Let's calculate the minimum spanning tree for the following graph:
179+
178180
![Minimum spanning tree](Images/Minimum_Spanning_Tree.png)
179181

180-
The minimum spanning tree is represented by the bold edges.
182+
*Note: the minimum spanning tree is represented by the bold edges.*
183+
184+
Start with the source node ``a`` and add it to a queue and mark it as visited.
185+
```swift
186+
queue.enqueue(a)
187+
a.visited = true
188+
```
189+
The queue is now ``[ a ]``. Dequeue ``a`` and enqueue the neighbor nodes ``b`` and ``h`` and mark them as visited.
190+
```swift
191+
queue.dequeue(a)
192+
queue.enqueue(b)
193+
b.visited = true
194+
queue.enqueue(h)
195+
h.visited = true
196+
```
197+
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.
198+
```swift
199+
queue.dequeue(b)
200+
queue.enqueue(c)
201+
c.visited = true
202+
b.removeEdgeTo(h)
203+
```
204+
The queue is now ``[ h, c ]``. Dequeue ``h`` and enqueue the neighbor nodes ``g`` and ``i`` and mark them as visited.
205+
```swift
206+
queue.dequeue(h)
207+
queue.enqueue(g)
208+
g.visited = true
209+
queue.enqueue(i)
210+
i.visited = true
211+
```
212+
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.
213+
```swift
214+
queue.dequeue(c)
215+
queue.enqueue(d)
216+
d.visited = true
217+
queue.enqueue(f)
218+
f.visited = true
219+
c.removeEdgeTo(i)
220+
```
221+
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.
222+
```swift
223+
queue.dequeue(g)
224+
g.removeEdgeTo(f)
225+
g.removeEdgeTo(i)
226+
```
227+
The queue is now ``[ i, d, f ]``. Dequeue ``i``.
228+
```swift
229+
queue.dequeue(i)
230+
```
231+
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.
232+
```swift
233+
queue.dequeue(d)
234+
queue.enqueue(e)
235+
e.visited = true
236+
d.removeEdgeTo(f)
237+
```
238+
The queue is now ``[ f, e ]``. Dequeue ``f``. Remove the edge between ``f`` to ``e`` because ``e`` has already been visited.
239+
```swift
240+
queue.dequeue(f)
241+
f.removeEdgeTo(e)
242+
```
243+
The queue is now ``[ e ]``. Dequeue ``e``.
244+
```swift
245+
queue.dequeue(e)
246+
```
247+
The queue is now empty which means the minimum spanning tree has been computed.
248+
249+
Here's the code:
250+
```swift
251+
func breadthFirstSearchMinimumSpanningTree(graph: Graph, source: Node) -> Graph {
252+
let minimumSpanningTree = graph.duplicate()
253+
254+
var queue = Queue<Node>()
255+
let sourceInMinimumSpanningTree = minimumSpanningTree.findNodeWithLabel(source.label)
256+
queue.enqueue(sourceInMinimumSpanningTree)
257+
sourceInMinimumSpanningTree.visited = true
258+
259+
while !queue.isEmpty {
260+
let current = queue.dequeue()!
261+
for edge in current.neighbors {
262+
let neighborNode = edge.neighbor
263+
if !neighborNode.visited {
264+
neighborNode.visited = true
265+
queue.enqueue(neighborNode)
266+
} else {
267+
current.remove(edge)
268+
}
269+
}
270+
}
271+
272+
return minimumSpanningTree
273+
}
274+
```
275+
Put this code in a playground and test it like so:
276+
```swift
277+
let graph = Graph()
181278

182-
TODO: explain steps to generate minimum spanning tree
279+
let nodeA = graph.addNode("a")
280+
let nodeB = graph.addNode("b")
281+
let nodeC = graph.addNode("c")
282+
let nodeD = graph.addNode("d")
283+
let nodeE = graph.addNode("e")
284+
let nodeF = graph.addNode("f")
285+
let nodeG = graph.addNode("g")
286+
let nodeH = graph.addNode("h")
287+
let nodeI = graph.addNode("i")
183288

289+
graph.addEdge(nodeA, neighbor: nodeB)
290+
graph.addEdge(nodeA, neighbor: nodeH)
291+
graph.addEdge(nodeB, neighbor: nodeA)
292+
graph.addEdge(nodeB, neighbor: nodeC)
293+
graph.addEdge(nodeB, neighbor: nodeH)
294+
graph.addEdge(nodeC, neighbor: nodeB)
295+
graph.addEdge(nodeC, neighbor: nodeD)
296+
graph.addEdge(nodeC, neighbor: nodeF)
297+
graph.addEdge(nodeC, neighbor: nodeI)
298+
graph.addEdge(nodeD, neighbor: nodeC)
299+
graph.addEdge(nodeD, neighbor: nodeE)
300+
graph.addEdge(nodeD, neighbor: nodeF)
301+
graph.addEdge(nodeE, neighbor: nodeD)
302+
graph.addEdge(nodeE, neighbor: nodeF)
303+
graph.addEdge(nodeF, neighbor: nodeC)
304+
graph.addEdge(nodeF, neighbor: nodeD)
305+
graph.addEdge(nodeF, neighbor: nodeE)
306+
graph.addEdge(nodeF, neighbor: nodeG)
307+
graph.addEdge(nodeG, neighbor: nodeF)
308+
graph.addEdge(nodeG, neighbor: nodeH)
309+
graph.addEdge(nodeG, neighbor: nodeI)
310+
graph.addEdge(nodeH, neighbor: nodeA)
311+
graph.addEdge(nodeH, neighbor: nodeB)
312+
graph.addEdge(nodeH, neighbor: nodeG)
313+
graph.addEdge(nodeH, neighbor: nodeI)
314+
graph.addEdge(nodeI, neighbor: nodeC)
315+
graph.addEdge(nodeI, neighbor: nodeG)
316+
graph.addEdge(nodeI, neighbor: nodeH)
317+
318+
let minimumSpanningTree = breadthFirstSearchMinimumSpanningTree(graph, source: nodeA)
319+
320+
print(minimumSpanningTree) // [node: a edges: ["b", "h"]]
321+
// [node: b edges: ["c"]]
322+
// [node: c edges: ["d", "f"]]
323+
// [node: d edges: ["e"]]
324+
// [node: h edges: ["g", "i"]]
325+
```
184326
## See also
185327

186-
[Graph](../Graph/), [Tree](../Tree/), [Queues](../Queue/), [Shortest Path](../Shortest Path/), [Breadth-first search on Wikipedia](https://en.wikipedia.org/wiki/Breadth-first_search), [Minimum spanning tree on Wikipedia](https://en.wikipedia.org/wiki/Minimum_spanning_tree).
328+
[Graph](../Graph/), [Tree](../Tree/), [Queues](../Queue/), [Shortest Path](../Shortest Path/), [Minimum Spanning Tree](../Minimum Spanning Tree/).
187329

188330
*Written by [Chris Pilcher](https://github.com/chris-pilcher)*

0 commit comments

Comments
 (0)