Skip to content

Commit 6bc50e5

Browse files
committed
Address comments and refactor
1 parent 9a363c7 commit 6bc50e5

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

Minimum Spanning Tree/MinimumSpanningTree.playground/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ func minimumSpanningTreePrim<T>(graph: Graph<T>) -> (cost: Int, tree: Graph<T>)
3030
var cost: Int = 0
3131
var tree = Graph<T>()
3232

33-
if graph.vertices.isEmpty {
33+
guard let start = graph.vertices.first else {
3434
return (cost: cost, tree: tree)
3535
}
3636

3737
var visited = Set<T>()
3838
var priorityQueue = PriorityQueue<(vertex: T, weight: Int, parent: T?)>(
3939
sort: { $0.weight < $1.weight })
4040

41-
priorityQueue.enqueue((vertex: graph.vertices.first!, weight: 0, parent: nil))
41+
priorityQueue.enqueue((vertex: start, weight: 0, parent: nil))
4242
while let head = priorityQueue.dequeue() {
4343
let vertex = head.vertex
4444
if visited.contains(vertex) {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ public struct Graph<T: Hashable>: CustomStringConvertible {
3636
edgeList.append(Edge(vertex1: v1, vertex2: v2, weight: w))
3737
vertices.insert(v1)
3838
vertices.insert(v2)
39-
if adjList[v1] == nil {
40-
adjList[v1] = [(vertex: v2, weight: w)]
39+
40+
if let _ = adjList[v1] {
41+
adjList[v1]?.append((vertex: v2, weight: w))
4142
} else {
42-
adjList[v1]!.append((vertex: v2, weight: w))
43+
adjList[v1] = [(vertex: v2, weight: w)]
4344
}
4445

45-
if adjList[v2] == nil {
46-
adjList[v2] = [(vertex: v1, weight: w)]
46+
if let _ = adjList[v2] {
47+
adjList[v2]?.append((vertex: v1, weight: w))
4748
} else {
48-
adjList[v2]!.append((vertex: v1, weight: w))
49+
adjList[v2] = [(vertex: v1, weight: w)]
4950
}
5051
}
5152

Minimum Spanning Tree/MinimumSpanningTree.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Minimum Spanning Tree/Prim.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ func minimumSpanningTreePrim<T>(graph: Graph<T>) -> (cost: Int, tree: Graph<T>)
1010
var cost: Int = 0
1111
var tree = Graph<T>()
1212

13-
if graph.vertices.isEmpty {
13+
guard let start = graph.vertices.first else {
1414
return (cost: cost, tree: tree)
1515
}
1616

1717
var visited = Set<T>()
1818
var priorityQueue = PriorityQueue<(vertex: T, weight: Int, parent: T?)>(
1919
sort: { $0.weight < $1.weight })
2020

21-
priorityQueue.enqueue((vertex: graph.vertices.first!, weight: 0, parent: nil))
21+
priorityQueue.enqueue((vertex: start, weight: 0, parent: nil))
2222
while let head = priorityQueue.dequeue() {
2323
let vertex = head.vertex
2424
if visited.contains(vertex) {
@@ -43,3 +43,4 @@ func minimumSpanningTreePrim<T>(graph: Graph<T>) -> (cost: Int, tree: Graph<T>)
4343

4444
return (cost: cost, tree: tree)
4545
}
46+

0 commit comments

Comments
 (0)