Skip to content

Commit 6e6ecfb

Browse files
Kelvin LauKelvin Lau
authored andcommitted
Fixed playgrounds for Swift 3.
1 parent 79c46da commit 6e6ecfb

File tree

4 files changed

+30
-33
lines changed

4 files changed

+30
-33
lines changed
Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
2+
13
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
24
var nodesExplored = [source.label]
35
source.visited = true
46

57
for edge in source.neighbors {
68
if !edge.neighbor.visited {
7-
nodesExplored += depthFirstSearch(graph, source: edge.neighbor)
9+
nodesExplored += depthFirstSearch(graph: graph, source: edge.neighbor)
810
}
911
}
1012
return nodesExplored
@@ -14,24 +16,24 @@ func depthFirstSearch(graph: Graph, source: Node) -> [String] {
1416

1517
let graph = Graph()
1618

17-
let nodeA = graph.addNode("a")
18-
let nodeB = graph.addNode("b")
19-
let nodeC = graph.addNode("c")
20-
let nodeD = graph.addNode("d")
21-
let nodeE = graph.addNode("e")
22-
let nodeF = graph.addNode("f")
23-
let nodeG = graph.addNode("g")
24-
let nodeH = graph.addNode("h")
25-
26-
graph.addEdge(nodeA, neighbor: nodeB)
27-
graph.addEdge(nodeA, neighbor: nodeC)
28-
graph.addEdge(nodeB, neighbor: nodeD)
29-
graph.addEdge(nodeB, neighbor: nodeE)
30-
graph.addEdge(nodeC, neighbor: nodeF)
31-
graph.addEdge(nodeC, neighbor: nodeG)
32-
graph.addEdge(nodeE, neighbor: nodeH)
33-
graph.addEdge(nodeE, neighbor: nodeF)
34-
graph.addEdge(nodeF, neighbor: nodeG)
35-
36-
let nodesExplored = depthFirstSearch(graph, source: nodeA)
19+
let nodeA = graph.addNode(label: "a")
20+
let nodeB = graph.addNode(label: "b")
21+
let nodeC = graph.addNode(label: "c")
22+
let nodeD = graph.addNode(label: "d")
23+
let nodeE = graph.addNode(label: "e")
24+
let nodeF = graph.addNode(label: "f")
25+
let nodeG = graph.addNode(label: "g")
26+
let nodeH = graph.addNode(label: "h")
27+
28+
graph.addEdge(source: nodeA, neighbor: nodeB)
29+
graph.addEdge(source: nodeA, neighbor: nodeC)
30+
graph.addEdge(source: nodeB, neighbor: nodeD)
31+
graph.addEdge(source: nodeB, neighbor: nodeE)
32+
graph.addEdge(source: nodeC, neighbor: nodeF)
33+
graph.addEdge(source: nodeC, neighbor: nodeG)
34+
graph.addEdge(source: nodeE, neighbor: nodeH)
35+
graph.addEdge(source: nodeE, neighbor: nodeF)
36+
graph.addEdge(source: nodeF, neighbor: nodeG)
37+
38+
let nodesExplored = depthFirstSearch(graph: graph, source: nodeA)
3739
print(nodesExplored)

Depth-First Search/DepthFirstSearch.playground/Pages/Simple Example.xcplaygroundpage/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class Graph: CustomStringConvertible, Equatable {
55
self.nodes = []
66
}
77

8-
public func addNode(label: String) -> Node {
8+
@discardableResult public func addNode(label: String) -> Node {
99
let node = Node(label: label)
1010
nodes.append(node)
1111
return node
@@ -35,14 +35,14 @@ public class Graph: CustomStringConvertible, Equatable {
3535
let duplicated = Graph()
3636

3737
for node in nodes {
38-
duplicated.addNode(node.label)
38+
duplicated.addNode(label: node.label)
3939
}
4040

4141
for node in nodes {
4242
for edge in node.neighbors {
43-
let source = duplicated.findNodeWithLabel(node.label)
44-
let neighbour = duplicated.findNodeWithLabel(edge.neighbor.label)
45-
duplicated.addEdge(source, neighbor: neighbour)
43+
let source = duplicated.findNodeWithLabel(label: node.label)
44+
let neighbour = duplicated.findNodeWithLabel(label: edge.neighbor.label)
45+
duplicated.addEdge(source: source, neighbor: neighbour)
4646
}
4747
}
4848

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
public class Node: CustomStringConvertible, Equatable {
23
public var neighbors: [Edge]
34

@@ -23,7 +24,7 @@ public class Node: CustomStringConvertible, Equatable {
2324
}
2425

2526
public func remove(edge: Edge) {
26-
neighbors.removeAtIndex(neighbors.indexOf { $0 === edge }!)
27+
neighbors.remove(at: neighbors.index { $0 === edge }!)
2728
}
2829
}
2930

0 commit comments

Comments
 (0)