Skip to content

Commit 215ca93

Browse files
committed
migrating dfs to swift3
1 parent 2da215d commit 215ca93

File tree

7 files changed

+15
-21
lines changed

7 files changed

+15
-21
lines changed

Depth-First Search/DepthFirstSearch.playground/Pages/Simple Example.xcplaygroundpage/Contents.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
1+
func depthFirstSearch(_ graph: Graph, source: Node) -> [String] {
22
var nodesExplored = [source.label]
33
source.visited = true
44

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

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
public class Edge: Equatable {
22
public var neighbor: Node
33

4-
public init(neighbor: Node) {
4+
public init(_ neighbor: Node) {
55
self.neighbor = neighbor
66
}
77
}
88

9-
public func == (lhs: Edge, rhs: Edge) -> Bool {
9+
public func == (_ lhs: Edge, rhs: Edge) -> Bool {
1010
return lhs.neighbor == rhs.neighbor
1111
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ public class Graph: CustomStringConvertible, Equatable {
55
self.nodes = []
66
}
77

8-
public func addNode(label: String) -> Node {
9-
let node = Node(label: label)
8+
public func addNode(_ label: String) -> Node {
9+
let node = Node(label)
1010
nodes.append(node)
1111
return node
1212
}
1313

14-
public func addEdge(source: Node, neighbor: Node) {
15-
let edge = Edge(neighbor: neighbor)
14+
public func addEdge(_ source: Node, neighbor: Node) {
15+
let edge = Edge(neighbor)
1616
source.neighbors.append(edge)
1717
}
1818

@@ -27,7 +27,7 @@ public class Graph: CustomStringConvertible, Equatable {
2727
return description
2828
}
2929

30-
public func findNodeWithLabel(label: String) -> Node {
30+
public func findNodeWithLabel(_ label: String) -> Node {
3131
return nodes.filter { $0.label == label }.first!
3232
}
3333

@@ -50,6 +50,6 @@ public class Graph: CustomStringConvertible, Equatable {
5050
}
5151
}
5252

53-
public func == (lhs: Graph, rhs: Graph) -> Bool {
53+
public func == (_ lhs: Graph, rhs: Graph) -> Bool {
5454
return lhs.nodes == rhs.nodes
5555
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class Node: CustomStringConvertible, Equatable {
55
public var distance: Int?
66
public var visited: Bool
77

8-
public init(label: String) {
8+
public init(_ label: String) {
99
self.label = label
1010
neighbors = []
1111
visited = false
@@ -22,11 +22,11 @@ public class Node: CustomStringConvertible, Equatable {
2222
return distance != nil
2323
}
2424

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

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

Depth-First Search/DepthFirstSearch.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
1+
func depthFirstSearch(_ graph: Graph, source: Node) -> [String] {
22
var nodesExplored = [source.label]
33
source.visited = true
44

Depth-First Search/README.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The parent of a node is the one that "discovered" that node. The root of the tre
2727
Simple recursive implementation of depth-first search:
2828

2929
```swift
30-
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
30+
func depthFirstSearch(_ graph: Graph, source: Node) -> [String] {
3131
var nodesExplored = [source.label]
3232
source.visited = true
3333

0 commit comments

Comments
 (0)