|
| 1 | +/** |
| 2 | + The following code demonstrates getting MST of the graph below by both |
| 3 | + Kruskal's and Prim's algorithms. |
| 4 | + */ |
| 5 | + |
| 6 | +func minimumSpanningTreeKruskal<T>(graph: Graph<T>) -> (cost: Int, tree: Graph<T>) { |
| 7 | + var cost: Int = 0 |
| 8 | + var tree = Graph<T>() |
| 9 | + let sortedEdgeListByWeight = graph.edgeList.sorted(by: { $0.weight < $1.weight }) |
| 10 | + |
| 11 | + var unionFind = UnionFind<T>() |
| 12 | + for vertex in graph.vertices { |
| 13 | + unionFind.addSetWith(vertex) |
| 14 | + } |
| 15 | + |
| 16 | + for edge in sortedEdgeListByWeight { |
| 17 | + let v1 = edge.vertex1 |
| 18 | + let v2 = edge.vertex2 |
| 19 | + if !unionFind.inSameSet(v1, and: v2) { |
| 20 | + cost += edge.weight |
| 21 | + tree.addEdge(edge) |
| 22 | + unionFind.unionSetsContaining(v1, and: v2) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return (cost: cost, tree: tree) |
| 27 | +} |
| 28 | + |
| 29 | +func minimumSpanningTreePrim<T>(graph: Graph<T>) -> (cost: Int, tree: Graph<T>) { |
| 30 | + var cost: Int = 0 |
| 31 | + var tree = Graph<T>() |
| 32 | + |
| 33 | + if graph.vertices.isEmpty { |
| 34 | + return (cost: cost, tree: tree) |
| 35 | + } |
| 36 | + |
| 37 | + var visited = Set<T>() |
| 38 | + var priorityQueue = PriorityQueue<(vertex: T, weight: Int, parent: T?)>( |
| 39 | + sort: { $0.weight < $1.weight }) |
| 40 | + |
| 41 | + priorityQueue.enqueue((vertex: graph.vertices.first!, weight: 0, parent: nil)) |
| 42 | + while let head = priorityQueue.dequeue() { |
| 43 | + let vertex = head.vertex |
| 44 | + if visited.contains(vertex) { |
| 45 | + continue |
| 46 | + } |
| 47 | + visited.insert(vertex) |
| 48 | + |
| 49 | + cost += head.weight |
| 50 | + if let prev = head.parent { |
| 51 | + tree.addEdge(vertex1: prev, vertex2: vertex, weight: head.weight) |
| 52 | + } |
| 53 | + |
| 54 | + if let neighbours = graph.adjList[vertex] { |
| 55 | + for neighbour in neighbours { |
| 56 | + let nextVertex = neighbour.vertex |
| 57 | + if !visited.contains(nextVertex) { |
| 58 | + priorityQueue.enqueue((vertex: nextVertex, weight: neighbour.weight, parent: vertex)) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return (cost: cost, tree: tree) |
| 65 | +} |
| 66 | + |
| 67 | +/*: |
| 68 | +  |
| 69 | + */ |
| 70 | + |
| 71 | +var graph = Graph<Int>() |
| 72 | +graph.addEdge(vertex1: 1, vertex2: 2, weight: 6) |
| 73 | +graph.addEdge(vertex1: 1, vertex2: 3, weight: 1) |
| 74 | +graph.addEdge(vertex1: 1, vertex2: 4, weight: 5) |
| 75 | +graph.addEdge(vertex1: 2, vertex2: 3, weight: 5) |
| 76 | +graph.addEdge(vertex1: 2, vertex2: 5, weight: 3) |
| 77 | +graph.addEdge(vertex1: 3, vertex2: 4, weight: 5) |
| 78 | +graph.addEdge(vertex1: 3, vertex2: 5, weight: 6) |
| 79 | +graph.addEdge(vertex1: 3, vertex2: 6, weight: 4) |
| 80 | +graph.addEdge(vertex1: 4, vertex2: 6, weight: 2) |
| 81 | +graph.addEdge(vertex1: 5, vertex2: 6, weight: 6) |
| 82 | + |
| 83 | +print("===== Kruskal's =====") |
| 84 | +let result1 = minimumSpanningTreeKruskal(graph: graph) |
| 85 | +print("Minimum spanning tree total weight: \(result1.cost)") |
| 86 | +print("Minimum spanning tree:") |
| 87 | +print(result1.tree) |
| 88 | + |
| 89 | +print("===== Prim's =====") |
| 90 | +let result2 = minimumSpanningTreePrim(graph: graph) |
| 91 | +print("Minimum spanning tree total weight: \(result2.cost)") |
| 92 | +print("Minimum spanning tree:") |
| 93 | +print(result2.tree) |
0 commit comments