Skip to content

Commit 89fb804

Browse files
committed
Updating to Swift3
I upgraded to swift 3 because it did not run as a grammar problem.
1 parent dd54980 commit 89fb804

File tree

5 files changed

+18
-24
lines changed

5 files changed

+18
-24
lines changed

Shortest Path (Unweighted)/ShortestPath.playground/Pages/Shortest path example.xcplaygroundpage/Contents.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ func breadthFirstSearchShortestPath(graph: Graph, source: Node) -> Graph {
22
let shortestPathGraph = graph.duplicate()
33

44
var queue = Queue<Node>()
5-
let sourceInShortestPathsGraph = shortestPathGraph.findNodeWithLabel(source.label)
6-
queue.enqueue(sourceInShortestPathsGraph)
5+
let sourceInShortestPathsGraph = shortestPathGraph.findNodeWithLabel(label: source.label)
6+
queue.enqueue(element: sourceInShortestPathsGraph)
77
sourceInShortestPathsGraph.distance = 0
88

99
while let current = queue.dequeue() {
1010
for edge in current.neighbors {
1111
let neighborNode = edge.neighbor
1212
if !neighborNode.hasDistance {
13-
queue.enqueue(neighborNode)
13+
queue.enqueue(element: neighborNode)
1414
neighborNode.distance = current.distance! + 1
1515
}
1616
}
@@ -23,14 +23,14 @@ func breadthFirstSearchShortestPath(graph: Graph, source: Node) -> Graph {
2323

2424
let graph = Graph()
2525

26-
let nodeA = graph.addNode("a")
27-
let nodeB = graph.addNode("b")
28-
let nodeC = graph.addNode("c")
29-
let nodeD = graph.addNode("d")
30-
let nodeE = graph.addNode("e")
31-
let nodeF = graph.addNode("f")
32-
let nodeG = graph.addNode("g")
33-
let nodeH = graph.addNode("h")
26+
let nodeA = graph.addNode(label: "a")
27+
let nodeB = graph.addNode(label: "b")
28+
let nodeC = graph.addNode(label: "c")
29+
let nodeD = graph.addNode(label: "d")
30+
let nodeE = graph.addNode(label: "e")
31+
let nodeF = graph.addNode(label: "f")
32+
let nodeG = graph.addNode(label: "g")
33+
let nodeH = graph.addNode(label: "h")
3434

3535
graph.addEdge(nodeA, neighbor: nodeB)
3636
graph.addEdge(nodeA, neighbor: nodeC)
@@ -40,5 +40,5 @@ graph.addEdge(nodeC, neighbor: nodeF)
4040
graph.addEdge(nodeC, neighbor: nodeG)
4141
graph.addEdge(nodeE, neighbor: nodeH)
4242

43-
let shortestPathGraph = breadthFirstSearchShortestPath(graph, source: nodeA)
43+
let shortestPathGraph = breadthFirstSearchShortestPath(graph: graph, source: nodeA)
4444
print(shortestPathGraph.nodes)

Shortest Path (Unweighted)/ShortestPath.playground/Pages/Shortest path example.xcplaygroundpage/timeline.xctimeline

-6
This file was deleted.

Shortest Path (Unweighted)/ShortestPath.playground/Sources/Graph.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Graph: CustomStringConvertible, Equatable {
1111
return node
1212
}
1313

14-
public func addEdge(source: Node, neighbor: Node) {
14+
public func addEdge(_ source: Node, neighbor: Node) {
1515
let edge = Edge(neighbor: neighbor)
1616
source.neighbors.append(edge)
1717
}
@@ -35,13 +35,13 @@ 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)
43+
let source = duplicated.findNodeWithLabel(label: node.label)
44+
let neighbour = duplicated.findNodeWithLabel(label: edge.neighbor.label)
4545
duplicated.addEdge(source, neighbor: neighbour)
4646
}
4747
}

Shortest Path (Unweighted)/ShortestPath.playground/Sources/Node.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Node: CustomStringConvertible, Equatable {
2323
}
2424

2525
public func remove(edge: Edge) {
26-
neighbors.removeAtIndex(neighbors.indexOf { $0 === edge }!)
26+
neighbors.remove(at: neighbors.index { $0 === edge }!)
2727
}
2828
}
2929

Shortest Path (Unweighted)/ShortestPath.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func breadthFirstSearchShortestPath(graph: Graph, source: Node) -> Graph {
1010
for edge in current.neighbors {
1111
let neighborNode = edge.neighbor
1212
if !neighborNode.hasDistance {
13-
queue.enqueue(neighborNode)
13+
queue.enqueue(element: neighborNode)
1414
neighborNode.distance = current.distance! + 1
1515
}
1616
}

0 commit comments

Comments
 (0)