Skip to content

Commit abba1fa

Browse files
author
Chris Pilcher
committed
BFS Added tests for minimum spanning tree.
1 parent aa8e352 commit abba1fa

File tree

9 files changed

+136
-9
lines changed

9 files changed

+136
-9
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
public class Edge {
1+
public class Edge : Equatable {
22
public var neighbor: Node
33

44
public init(neighbor: Node) {
55
self.neighbor = neighbor
66
}
77
}
8+
9+
public func ==(lhs: Edge, rhs: Edge) -> Bool {
10+
return lhs.neighbor == rhs.neighbor
11+
}

Breadth-First Search/BreadthFirstSearch.playground/Sources/Graph.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class Graph : CustomStringConvertible {
1+
public class Graph : CustomStringConvertible, Equatable {
22
public private(set) var nodes: [Node]
33

44
public init() {
@@ -50,3 +50,7 @@ public class Graph : CustomStringConvertible {
5050
return duplicated
5151
}
5252
}
53+
54+
public func ==(lhs: Graph, rhs: Graph) -> Bool {
55+
return lhs.nodes == rhs.nodes
56+
}

Breadth-First Search/BreadthFirstSearch.playground/Sources/Node.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
public class Node : CustomStringConvertible, Equatable {
22
public var neighbors: [Edge]
3-
3+
44
public private(set) var label: String
55
public var distance: Int?
66
public var visited: Bool
@@ -28,5 +28,5 @@ public class Node : CustomStringConvertible, Equatable {
2828
}
2929

3030
public func ==(lhs: Node, rhs: Node) -> Bool {
31-
return lhs.label == rhs.label
31+
return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
3232
}

Breadth-First Search/Edge.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
public class Edge {
1+
public class Edge : Equatable {
22
public var neighbor: Node
33

44
public init(neighbor: Node) {
55
self.neighbor = neighbor
66
}
77
}
8+
9+
public func ==(lhs: Edge, rhs: Edge) -> Bool {
10+
return lhs.neighbor == rhs.neighbor
11+
}

Breadth-First Search/Graph.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class Graph : CustomStringConvertible {
1+
public class Graph : CustomStringConvertible, Equatable {
22
public private(set) var nodes: [Node]
33

44
public init() {
@@ -50,3 +50,7 @@ public class Graph : CustomStringConvertible {
5050
return duplicated
5151
}
5252
}
53+
54+
public func ==(lhs: Graph, rhs: Graph) -> Bool {
55+
return lhs.nodes == rhs.nodes
56+
}

Breadth-First Search/Node.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ public class Node : CustomStringConvertible, Equatable {
2828
}
2929

3030
public func ==(lhs: Node, rhs: Node) -> Bool {
31-
return lhs.label == rhs.label
31+
return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
3232
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import XCTest
2+
3+
class BreadthFirstSearchMinimumSpanningTreeTests: XCTestCase {
4+
5+
func testMinimumSpanningTreeReturnsSameTreeWhenGivenTree() {
6+
let tree = Graph()
7+
let nodeA = tree.addNode("a")
8+
let nodeB = tree.addNode("b")
9+
let nodeC = tree.addNode("c")
10+
let nodeD = tree.addNode("d")
11+
let nodeE = tree.addNode("e")
12+
let nodeF = tree.addNode("f")
13+
let nodeG = tree.addNode("g")
14+
let nodeH = tree.addNode("h")
15+
tree.addEdge(nodeA, neighbor: nodeB)
16+
tree.addEdge(nodeA, neighbor: nodeC)
17+
tree.addEdge(nodeB, neighbor: nodeD)
18+
tree.addEdge(nodeB, neighbor: nodeE)
19+
tree.addEdge(nodeC, neighbor: nodeF)
20+
tree.addEdge(nodeC, neighbor: nodeG)
21+
tree.addEdge(nodeE, neighbor: nodeH)
22+
23+
let minimumSpanningTree = breadthFirstSearchMinimumSpanningTree(tree, source: nodeA)
24+
25+
XCTAssertEqual(minimumSpanningTree, tree)
26+
}
27+
28+
func testMinimumSpanningTreeReturnsMinimumSpanningTreeWhenGivenGraph() {
29+
let graphAndSourceNode = createGraph()
30+
let expectedMinimumSpanningTree = createMinimumSpanningTree()
31+
32+
let actualMinimumSpanningTree = breadthFirstSearchMinimumSpanningTree(graphAndSourceNode.graph,
33+
source: graphAndSourceNode.source)
34+
35+
XCTAssertEqual(actualMinimumSpanningTree, expectedMinimumSpanningTree)
36+
}
37+
38+
func createGraph() -> (graph: Graph, source: Node) {
39+
let graph = Graph()
40+
41+
let nodeA = graph.addNode("a")
42+
let nodeB = graph.addNode("b")
43+
let nodeC = graph.addNode("c")
44+
let nodeD = graph.addNode("d")
45+
let nodeE = graph.addNode("e")
46+
let nodeF = graph.addNode("f")
47+
let nodeG = graph.addNode("g")
48+
let nodeH = graph.addNode("h")
49+
let nodeI = graph.addNode("i")
50+
51+
graph.addEdge(nodeA, neighbor: nodeB)
52+
graph.addEdge(nodeA, neighbor: nodeH)
53+
graph.addEdge(nodeB, neighbor: nodeA)
54+
graph.addEdge(nodeB, neighbor: nodeC)
55+
graph.addEdge(nodeB, neighbor: nodeH)
56+
graph.addEdge(nodeC, neighbor: nodeB)
57+
graph.addEdge(nodeC, neighbor: nodeD)
58+
graph.addEdge(nodeC, neighbor: nodeF)
59+
graph.addEdge(nodeC, neighbor: nodeI)
60+
graph.addEdge(nodeD, neighbor: nodeC)
61+
graph.addEdge(nodeD, neighbor: nodeE)
62+
graph.addEdge(nodeD, neighbor: nodeF)
63+
graph.addEdge(nodeE, neighbor: nodeD)
64+
graph.addEdge(nodeE, neighbor: nodeF)
65+
graph.addEdge(nodeF, neighbor: nodeC)
66+
graph.addEdge(nodeF, neighbor: nodeD)
67+
graph.addEdge(nodeF, neighbor: nodeE)
68+
graph.addEdge(nodeF, neighbor: nodeG)
69+
graph.addEdge(nodeG, neighbor: nodeF)
70+
graph.addEdge(nodeG, neighbor: nodeH)
71+
graph.addEdge(nodeG, neighbor: nodeI)
72+
graph.addEdge(nodeH, neighbor: nodeA)
73+
graph.addEdge(nodeH, neighbor: nodeB)
74+
graph.addEdge(nodeH, neighbor: nodeG)
75+
graph.addEdge(nodeH, neighbor: nodeI)
76+
graph.addEdge(nodeI, neighbor: nodeC)
77+
graph.addEdge(nodeI, neighbor: nodeG)
78+
graph.addEdge(nodeI, neighbor: nodeH)
79+
80+
return (graph, nodeA)
81+
}
82+
83+
func createMinimumSpanningTree() -> Graph {
84+
let minimumSpanningTree = Graph()
85+
86+
let nodeA = minimumSpanningTree.addNode("a")
87+
let nodeB = minimumSpanningTree.addNode("b")
88+
let nodeC = minimumSpanningTree.addNode("c")
89+
let nodeD = minimumSpanningTree.addNode("d")
90+
let nodeE = minimumSpanningTree.addNode("e")
91+
let nodeF = minimumSpanningTree.addNode("f")
92+
let nodeG = minimumSpanningTree.addNode("g")
93+
let nodeH = minimumSpanningTree.addNode("h")
94+
let nodeI = minimumSpanningTree.addNode("i")
95+
96+
minimumSpanningTree.addEdge(nodeA, neighbor: nodeB)
97+
minimumSpanningTree.addEdge(nodeA, neighbor: nodeH)
98+
minimumSpanningTree.addEdge(nodeB, neighbor: nodeC)
99+
minimumSpanningTree.addEdge(nodeH, neighbor: nodeG)
100+
minimumSpanningTree.addEdge(nodeH, neighbor: nodeI)
101+
minimumSpanningTree.addEdge(nodeC, neighbor: nodeD)
102+
minimumSpanningTree.addEdge(nodeC, neighbor: nodeF)
103+
minimumSpanningTree.addEdge(nodeD, neighbor: nodeE)
104+
105+
return minimumSpanningTree
106+
}
107+
}

Breadth-First Search/Tests/BreadthFirstSearchTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import Foundation
21
import XCTest
32

4-
class QueueTest: XCTestCase {
3+
class BreadthFirstSearchTests: XCTestCase {
54

65
func testExploringTree() {
76
let tree = Graph()
@@ -83,3 +82,4 @@ class QueueTest: XCTestCase {
8382
XCTAssertEqual(nodesExplored, ["a"])
8483
}
8584
}
85+

Breadth-First Search/Tests/Tests.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
83AACB421C844CED00DDAFC7 /* BreadthFirstSearchMinimumSpanningTreeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83AACB411C844CED00DDAFC7 /* BreadthFirstSearchMinimumSpanningTreeTests.swift */; };
1011
83F9C9681C84437C00B3A87F /* BreadthFirstSearch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C9651C84437C00B3A87F /* BreadthFirstSearch.swift */; };
1112
83F9C9691C84437C00B3A87F /* BreadthFirstSearchMinimumSpanningTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C9661C84437C00B3A87F /* BreadthFirstSearchMinimumSpanningTree.swift */; };
1213
83F9C96A1C84437C00B3A87F /* BreadthFirstSearchShortestPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C9671C84437C00B3A87F /* BreadthFirstSearchShortestPath.swift */; };
@@ -20,6 +21,7 @@
2021
/* Begin PBXFileReference section */
2122
7B2BBC801C779D720067B71D /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
2223
7B2BBC941C779E7B0067B71D /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = SOURCE_ROOT; };
24+
83AACB411C844CED00DDAFC7 /* BreadthFirstSearchMinimumSpanningTreeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BreadthFirstSearchMinimumSpanningTreeTests.swift; sourceTree = SOURCE_ROOT; };
2325
83F9C9651C84437C00B3A87F /* BreadthFirstSearch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BreadthFirstSearch.swift; path = ../BreadthFirstSearch.swift; sourceTree = SOURCE_ROOT; };
2426
83F9C9661C84437C00B3A87F /* BreadthFirstSearchMinimumSpanningTree.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BreadthFirstSearchMinimumSpanningTree.swift; path = ../BreadthFirstSearchMinimumSpanningTree.swift; sourceTree = SOURCE_ROOT; };
2527
83F9C9671C84437C00B3A87F /* BreadthFirstSearchShortestPath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BreadthFirstSearchShortestPath.swift; path = ../BreadthFirstSearchShortestPath.swift; sourceTree = SOURCE_ROOT; };
@@ -69,6 +71,7 @@
6971
83F9C9661C84437C00B3A87F /* BreadthFirstSearchMinimumSpanningTree.swift */,
7072
83F9C9671C84437C00B3A87F /* BreadthFirstSearchShortestPath.swift */,
7173
7B2BBC941C779E7B0067B71D /* Info.plist */,
74+
83AACB411C844CED00DDAFC7 /* BreadthFirstSearchMinimumSpanningTreeTests.swift */,
7275
);
7376
name = Tests;
7477
path = TestsTests;
@@ -142,6 +145,7 @@
142145
isa = PBXSourcesBuildPhase;
143146
buildActionMask = 2147483647;
144147
files = (
148+
83AACB421C844CED00DDAFC7 /* BreadthFirstSearchMinimumSpanningTreeTests.swift in Sources */,
145149
83F9C9721C84449D00B3A87F /* Graph.swift in Sources */,
146150
83F9C9691C84437C00B3A87F /* BreadthFirstSearchMinimumSpanningTree.swift in Sources */,
147151
83F9C9681C84437C00B3A87F /* BreadthFirstSearch.swift in Sources */,

0 commit comments

Comments
 (0)