Skip to content

Commit 9bd6c97

Browse files
committed
Minimum Spanning Tree updated to Swift4
1 parent 0e0ae64 commit 9bd6c97

File tree

2 files changed

+58
-53
lines changed

2 files changed

+58
-53
lines changed

Minimum Spanning Tree/MinimumSpanningTree.playground/Contents.swift

+56-51
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,70 @@
33
Kruskal's and Prim's algorithms.
44
*/
55

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
1510

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)
2319
}
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)
2732
}
2833

2934
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)
4640
}
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+
}
5966
}
60-
}
6167
}
62-
}
63-
64-
return (cost: cost, tree: tree)
68+
69+
return (cost: cost, tree: tree)
6570
}
6671

6772
/*:

Minimum Spanning Tree/MinimumSpanningTree.playground/Sources/Heap.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public struct Heap<T> {
147147

148148
let size = elements.count - 1
149149
if index != size {
150-
swap(&elements[index], &elements[size])
150+
elements.swapAt(index,size)
151151
shiftDown(index, heapSize: size)
152152
shiftUp(index)
153153
}
@@ -200,7 +200,7 @@ public struct Heap<T> {
200200
}
201201
if first == parentIndex { return }
202202

203-
swap(&elements[parentIndex], &elements[first])
203+
elements.swapAt(parentIndex,first)
204204
parentIndex = first
205205
}
206206
}

0 commit comments

Comments
 (0)