Skip to content

Commit 87802ee

Browse files
committed
Put Edge and Node into Graph.swift
It's only supporting code so it shouldn't draw too much attention to itself.
1 parent 99907cf commit 87802ee

File tree

13 files changed

+123
-111
lines changed

13 files changed

+123
-111
lines changed

Breadth-First Search/BreadthFirstSearch.playground/Pages/Minimum spanning tree example.xcplaygroundpage/Contents.swift

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//: [Previous](@previous)
2+
3+
//: # Minimum Spanning Tree Example
4+
15
func breadthFirstSearchMinimumSpanningTree(graph: Graph, source: Node) -> Graph {
26
let minimumSpanningTree = graph.duplicate()
37

Breadth-First Search/BreadthFirstSearch.playground/Pages/Shortest path example.xcplaygroundpage/Contents.swift

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//: [Previous](@previous)
2+
3+
//: # Shortest Path Example
4+
15
func breadthFirstSearchShortestPath(graph: Graph, source: Node) -> Graph {
26
let shortestPathGraph = graph.duplicate()
37

@@ -44,3 +48,6 @@ graph.addEdge(nodeE, neighbor: nodeH)
4448

4549
let shortestPathGraph = breadthFirstSearchShortestPath(graph, source: nodeA)
4650
print(shortestPathGraph.nodes)
51+
52+
//: [Next: Minimum Spanning Tree Example](@next)
53+

Breadth-First Search/BreadthFirstSearch.playground/Pages/Simple example.xcplaygroundpage/Contents.swift

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//: # Breadth-First Search
2+
13
func breadthFirstSearch(graph: Graph, source: Node) -> [String] {
24
var queue = Queue<Node>()
35
queue.enqueue(source)
@@ -46,3 +48,6 @@ graph.addEdge(nodeE, neighbor: nodeH)
4648

4749
let nodesExplored = breadthFirstSearch(graph, source: nodeA)
4850
print(nodesExplored)
51+
52+
//: [Next: Shortest Path Example](@next)
53+

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

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class Graph : CustomStringConvertible, Equatable {
1313

1414
public func addEdge(source: Node, neighbor: Node) {
1515
let edge = Edge(neighbor: neighbor)
16-
edge.neighbor = neighbor
1716
source.neighbors.append(edge)
1817
}
1918

Breadth-First Search/Edge.swift

-11
This file was deleted.

Breadth-First Search/Graph.swift

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1+
// MARK: - Edge
2+
3+
public class Edge : Equatable {
4+
public var neighbor: Node
5+
6+
public init(neighbor: Node) {
7+
self.neighbor = neighbor
8+
}
9+
}
10+
11+
public func ==(lhs: Edge, rhs: Edge) -> Bool {
12+
return lhs.neighbor == rhs.neighbor
13+
}
14+
15+
// MARK: - Node
16+
17+
public class Node : CustomStringConvertible, Equatable {
18+
public var neighbors: [Edge]
19+
20+
public private(set) var label: String
21+
public var distance: Int?
22+
public var visited: Bool
23+
24+
public init(label: String) {
25+
self.label = label
26+
neighbors = []
27+
visited = false
28+
}
29+
30+
public var description: String {
31+
if let distance = distance {
32+
return "Node(label: \(label), distance: \(distance))"
33+
}
34+
return "Node(label: \(label), distance: infinity)"
35+
}
36+
37+
public var hasDistance: Bool {
38+
return distance != nil
39+
}
40+
41+
public func remove(edge: Edge) {
42+
neighbors.removeAtIndex(neighbors.indexOf{ $0 === edge }!)
43+
}
44+
}
45+
46+
public func ==(lhs: Node, rhs: Node) -> Bool {
47+
return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
48+
}
49+
50+
// MARK: - Graph
51+
152
public class Graph : CustomStringConvertible, Equatable {
253
public private(set) var nodes: [Node]
354

@@ -13,7 +64,6 @@ public class Graph : CustomStringConvertible, Equatable {
1364

1465
public func addEdge(source: Node, neighbor: Node) {
1566
let edge = Edge(neighbor: neighbor)
16-
edge.neighbor = neighbor
1767
source.neighbors.append(edge)
1868
}
1969

Breadth-First Search/Node.swift

-32
This file was deleted.

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

+5-13
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
83F9C9691C84437C00B3A87F /* BreadthFirstSearchMinimumSpanningTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C9661C84437C00B3A87F /* BreadthFirstSearchMinimumSpanningTree.swift */; };
1414
83F9C96A1C84437C00B3A87F /* BreadthFirstSearchShortestPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C9671C84437C00B3A87F /* BreadthFirstSearchShortestPath.swift */; };
1515
83F9C96C1C8443E800B3A87F /* BreadthFirstSearchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C96B1C8443E800B3A87F /* BreadthFirstSearchTests.swift */; };
16-
83F9C9711C84449D00B3A87F /* Edge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C96D1C84449D00B3A87F /* Edge.swift */; };
1716
83F9C9721C84449D00B3A87F /* Graph.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C96E1C84449D00B3A87F /* Graph.swift */; };
18-
83F9C9731C84449D00B3A87F /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C96F1C84449D00B3A87F /* Node.swift */; };
1917
83F9C9741C84449D00B3A87F /* Queue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C9701C84449D00B3A87F /* Queue.swift */; };
2018
/* End PBXBuildFile section */
2119

@@ -28,9 +26,7 @@
2826
83F9C9661C84437C00B3A87F /* BreadthFirstSearchMinimumSpanningTree.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BreadthFirstSearchMinimumSpanningTree.swift; path = ../BreadthFirstSearchMinimumSpanningTree.swift; sourceTree = SOURCE_ROOT; };
2927
83F9C9671C84437C00B3A87F /* BreadthFirstSearchShortestPath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BreadthFirstSearchShortestPath.swift; path = ../BreadthFirstSearchShortestPath.swift; sourceTree = SOURCE_ROOT; };
3028
83F9C96B1C8443E800B3A87F /* BreadthFirstSearchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BreadthFirstSearchTests.swift; sourceTree = SOURCE_ROOT; };
31-
83F9C96D1C84449D00B3A87F /* Edge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Edge.swift; path = ../Edge.swift; sourceTree = SOURCE_ROOT; };
3229
83F9C96E1C84449D00B3A87F /* Graph.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Graph.swift; path = ../Graph.swift; sourceTree = SOURCE_ROOT; };
33-
83F9C96F1C84449D00B3A87F /* Node.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Node.swift; path = ../Node.swift; sourceTree = SOURCE_ROOT; };
3430
83F9C9701C84449D00B3A87F /* Queue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Queue.swift; path = ../Queue.swift; sourceTree = SOURCE_ROOT; };
3531
/* End PBXFileReference section */
3632

@@ -64,16 +60,14 @@
6460
7B2BBC831C779D720067B71D /* Tests */ = {
6561
isa = PBXGroup;
6662
children = (
67-
83F9C96D1C84449D00B3A87F /* Edge.swift */,
68-
83F9C96E1C84449D00B3A87F /* Graph.swift */,
69-
83F9C96F1C84449D00B3A87F /* Node.swift */,
70-
83F9C9701C84449D00B3A87F /* Queue.swift */,
7163
83F9C9651C84437C00B3A87F /* BreadthFirstSearch.swift */,
72-
83F9C9671C84437C00B3A87F /* BreadthFirstSearchShortestPath.swift */,
73-
83F9C9661C84437C00B3A87F /* BreadthFirstSearchMinimumSpanningTree.swift */,
7464
83F9C96B1C8443E800B3A87F /* BreadthFirstSearchTests.swift */,
75-
83AACB431C8456D200DDAFC7 /* BreadthFirstSearchShortestPathTests.swift */,
65+
83F9C9661C84437C00B3A87F /* BreadthFirstSearchMinimumSpanningTree.swift */,
7666
83AACB411C844CED00DDAFC7 /* BreadthFirstSearchMinimumSpanningTreeTests.swift */,
67+
83F9C9671C84437C00B3A87F /* BreadthFirstSearchShortestPath.swift */,
68+
83AACB431C8456D200DDAFC7 /* BreadthFirstSearchShortestPathTests.swift */,
69+
83F9C96E1C84449D00B3A87F /* Graph.swift */,
70+
83F9C9701C84449D00B3A87F /* Queue.swift */,
7771
7B2BBC941C779E7B0067B71D /* Info.plist */,
7872
);
7973
name = Tests;
@@ -155,9 +149,7 @@
155149
83F9C9681C84437C00B3A87F /* BreadthFirstSearch.swift in Sources */,
156150
83F9C9741C84449D00B3A87F /* Queue.swift in Sources */,
157151
83F9C96A1C84437C00B3A87F /* BreadthFirstSearchShortestPath.swift in Sources */,
158-
83F9C9711C84449D00B3A87F /* Edge.swift in Sources */,
159152
83F9C96C1C8443E800B3A87F /* BreadthFirstSearchTests.swift in Sources */,
160-
83F9C9731C84449D00B3A87F /* Node.swift in Sources */,
161153
);
162154
runOnlyForDeploymentPostprocessing = 0;
163155
};

Depth-First Search/DepthFirstSearch.playground/Sources/Graph.swift

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class Graph : CustomStringConvertible, Equatable {
1313

1414
public func addEdge(source: Node, neighbor: Node) {
1515
let edge = Edge(neighbor: neighbor)
16-
edge.neighbor = neighbor
1716
source.neighbors.append(edge)
1817
}
1918

Depth-First Search/Edge.swift

-11
This file was deleted.

Depth-First Search/Graph.swift

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1+
// MARK: - Edge
2+
3+
public class Edge : Equatable {
4+
public var neighbor: Node
5+
6+
public init(neighbor: Node) {
7+
self.neighbor = neighbor
8+
}
9+
}
10+
11+
public func ==(lhs: Edge, rhs: Edge) -> Bool {
12+
return lhs.neighbor == rhs.neighbor
13+
}
14+
15+
// MARK: - Node
16+
17+
public class Node : CustomStringConvertible, Equatable {
18+
public var neighbors: [Edge]
19+
20+
public private(set) var label: String
21+
public var distance: Int?
22+
public var visited: Bool
23+
24+
public init(label: String) {
25+
self.label = label
26+
neighbors = []
27+
visited = false
28+
}
29+
30+
public var description: String {
31+
if let distance = distance {
32+
return "Node(label: \(label), distance: \(distance))"
33+
}
34+
return "Node(label: \(label), distance: infinity)"
35+
}
36+
37+
public var hasDistance: Bool {
38+
return distance != nil
39+
}
40+
41+
public func remove(edge: Edge) {
42+
neighbors.removeAtIndex(neighbors.indexOf{ $0 === edge }!)
43+
}
44+
}
45+
46+
public func ==(lhs: Node, rhs: Node) -> Bool {
47+
return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
48+
}
49+
50+
// MARK: - Graph
51+
152
public class Graph : CustomStringConvertible, Equatable {
253
public private(set) var nodes: [Node]
354

@@ -13,7 +64,6 @@ public class Graph : CustomStringConvertible, Equatable {
1364

1465
public func addEdge(source: Node, neighbor: Node) {
1566
let edge = Edge(neighbor: neighbor)
16-
edge.neighbor = neighbor
1767
source.neighbors.append(edge)
1868
}
1969

Depth-First Search/Node.swift

-32
This file was deleted.

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

-8
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@
88

99
/* Begin PBXBuildFile section */
1010
83F9C96C1C8443E800B3A87F /* DepthFirstSearchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C96B1C8443E800B3A87F /* DepthFirstSearchTests.swift */; };
11-
83F9C9711C84449D00B3A87F /* Edge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C96D1C84449D00B3A87F /* Edge.swift */; };
1211
83F9C9721C84449D00B3A87F /* Graph.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C96E1C84449D00B3A87F /* Graph.swift */; };
13-
83F9C9731C84449D00B3A87F /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83F9C96F1C84449D00B3A87F /* Node.swift */; };
1412
FFC6E11E1C8656D10046BA79 /* DepthFirstSearch.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFC6E11D1C8656D10046BA79 /* DepthFirstSearch.swift */; };
1513
/* End PBXBuildFile section */
1614

1715
/* Begin PBXFileReference section */
1816
7B2BBC801C779D720067B71D /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
1917
7B2BBC941C779E7B0067B71D /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = SOURCE_ROOT; };
2018
83F9C96B1C8443E800B3A87F /* DepthFirstSearchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DepthFirstSearchTests.swift; sourceTree = SOURCE_ROOT; };
21-
83F9C96D1C84449D00B3A87F /* Edge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Edge.swift; path = ../Edge.swift; sourceTree = SOURCE_ROOT; };
2219
83F9C96E1C84449D00B3A87F /* Graph.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Graph.swift; path = ../Graph.swift; sourceTree = SOURCE_ROOT; };
23-
83F9C96F1C84449D00B3A87F /* Node.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Node.swift; path = ../Node.swift; sourceTree = SOURCE_ROOT; };
2420
FFC6E11D1C8656D10046BA79 /* DepthFirstSearch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DepthFirstSearch.swift; path = ../DepthFirstSearch.swift; sourceTree = SOURCE_ROOT; };
2521
/* End PBXFileReference section */
2622

@@ -54,9 +50,7 @@
5450
7B2BBC831C779D720067B71D /* Tests */ = {
5551
isa = PBXGroup;
5652
children = (
57-
83F9C96D1C84449D00B3A87F /* Edge.swift */,
5853
83F9C96E1C84449D00B3A87F /* Graph.swift */,
59-
83F9C96F1C84449D00B3A87F /* Node.swift */,
6054
83F9C96B1C8443E800B3A87F /* DepthFirstSearchTests.swift */,
6155
FFC6E11D1C8656D10046BA79 /* DepthFirstSearch.swift */,
6256
7B2BBC941C779E7B0067B71D /* Info.plist */,
@@ -134,10 +128,8 @@
134128
buildActionMask = 2147483647;
135129
files = (
136130
83F9C9721C84449D00B3A87F /* Graph.swift in Sources */,
137-
83F9C9711C84449D00B3A87F /* Edge.swift in Sources */,
138131
FFC6E11E1C8656D10046BA79 /* DepthFirstSearch.swift in Sources */,
139132
83F9C96C1C8443E800B3A87F /* DepthFirstSearchTests.swift in Sources */,
140-
83F9C9731C84449D00B3A87F /* Node.swift in Sources */,
141133
);
142134
runOnlyForDeploymentPostprocessing = 0;
143135
};

0 commit comments

Comments
 (0)