|
3 | 3 | Kruskal's and Prim's algorithms.
|
4 | 4 | */
|
5 | 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 |
| - } |
| 6 | +// last checked with Xcode 9.0b4 |
| 7 | +#if swift(>=4.0) |
| 8 | +print("Hello, Swift 4!") |
| 9 | +#endif |
15 | 10 |
|
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) |
| 11 | +func minimumSpanningTreeKruskal<T>(graph: Graph<T>) -> (cost: Int, tree: Graph<T>) { |
| 12 | + var cost: Int = 0 |
| 13 | + var tree = Graph<T>() |
| 14 | + let sortedEdgeListByWeight = graph.edgeList.sorted(by: { $0.weight < $1.weight }) |
| 15 | + |
| 16 | + var unionFind = UnionFind<T>() |
| 17 | + for vertex in graph.vertices { |
| 18 | + unionFind.addSetWith(vertex) |
23 | 19 | }
|
24 |
| - } |
25 |
| - |
26 |
| - return (cost: cost, tree: tree) |
| 20 | + |
| 21 | + for edge in sortedEdgeListByWeight { |
| 22 | + let v1 = edge.vertex1 |
| 23 | + let v2 = edge.vertex2 |
| 24 | + if !unionFind.inSameSet(v1, and: v2) { |
| 25 | + cost += edge.weight |
| 26 | + tree.addEdge(edge) |
| 27 | + unionFind.unionSetsContaining(v1, and: v2) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return (cost: cost, tree: tree) |
27 | 32 | }
|
28 | 33 |
|
29 | 34 | func minimumSpanningTreePrim<T>(graph: Graph<T>) -> (cost: Int, tree: Graph<T>) {
|
30 |
| - var cost: Int = 0 |
31 |
| - var tree = Graph<T>() |
32 |
| - |
33 |
| - guard let start = graph.vertices.first else { |
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: start, weight: 0, parent: nil)) |
42 |
| - while let head = priorityQueue.dequeue() { |
43 |
| - let vertex = head.vertex |
44 |
| - if visited.contains(vertex) { |
45 |
| - continue |
| 35 | + var cost: Int = 0 |
| 36 | + var tree = Graph<T>() |
| 37 | + |
| 38 | + guard let start = graph.vertices.first else { |
| 39 | + return (cost: cost, tree: tree) |
46 | 40 | }
|
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)) |
| 41 | + |
| 42 | + var visited = Set<T>() |
| 43 | + var priorityQueue = PriorityQueue<(vertex: T, weight: Int, parent: T?)>( |
| 44 | + sort: { $0.weight < $1.weight }) |
| 45 | + |
| 46 | + priorityQueue.enqueue((vertex: start, weight: 0, parent: nil)) |
| 47 | + while let head = priorityQueue.dequeue() { |
| 48 | + let vertex = head.vertex |
| 49 | + if visited.contains(vertex) { |
| 50 | + continue |
| 51 | + } |
| 52 | + visited.insert(vertex) |
| 53 | + |
| 54 | + cost += head.weight |
| 55 | + if let prev = head.parent { |
| 56 | + tree.addEdge(vertex1: prev, vertex2: vertex, weight: head.weight) |
| 57 | + } |
| 58 | + |
| 59 | + if let neighbours = graph.adjList[vertex] { |
| 60 | + for neighbour in neighbours { |
| 61 | + let nextVertex = neighbour.vertex |
| 62 | + if !visited.contains(nextVertex) { |
| 63 | + priorityQueue.enqueue((vertex: nextVertex, weight: neighbour.weight, parent: vertex)) |
| 64 | + } |
| 65 | + } |
59 | 66 | }
|
60 |
| - } |
61 | 67 | }
|
62 |
| - } |
63 |
| - |
64 |
| - return (cost: cost, tree: tree) |
| 68 | + |
| 69 | + return (cost: cost, tree: tree) |
65 | 70 | }
|
66 | 71 |
|
67 | 72 | /*:
|
|
0 commit comments