Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrating dfs to swift3 #238

Merged
merged 1 commit into from Sep 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,4 +1,4 @@
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
func depthFirstSearch(_ graph: Graph, source: Node) -> [String] {
var nodesExplored = [source.label]
source.visited = true

Expand Down

This file was deleted.

@@ -1,11 +1,11 @@
public class Edge: Equatable {
public var neighbor: Node

public init(neighbor: Node) {
public init(_ neighbor: Node) {
self.neighbor = neighbor
}
}

public func == (lhs: Edge, rhs: Edge) -> Bool {
public func == (_ lhs: Edge, rhs: Edge) -> Bool {
return lhs.neighbor == rhs.neighbor
}
Expand Up @@ -5,14 +5,14 @@ public class Graph: CustomStringConvertible, Equatable {
self.nodes = []
}

public func addNode(label: String) -> Node {
let node = Node(label: label)
public func addNode(_ label: String) -> Node {
let node = Node(label)
nodes.append(node)
return node
}

public func addEdge(source: Node, neighbor: Node) {
let edge = Edge(neighbor: neighbor)
public func addEdge(_ source: Node, neighbor: Node) {
let edge = Edge(neighbor)
source.neighbors.append(edge)
}

Expand All @@ -27,7 +27,7 @@ public class Graph: CustomStringConvertible, Equatable {
return description
}

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

Expand All @@ -50,6 +50,6 @@ public class Graph: CustomStringConvertible, Equatable {
}
}

public func == (lhs: Graph, rhs: Graph) -> Bool {
public func == (_ lhs: Graph, rhs: Graph) -> Bool {
return lhs.nodes == rhs.nodes
}
Expand Up @@ -5,7 +5,7 @@ public class Node: CustomStringConvertible, Equatable {
public var distance: Int?
public var visited: Bool

public init(label: String) {
public init(_ label: String) {
self.label = label
neighbors = []
visited = false
Expand All @@ -22,11 +22,11 @@ public class Node: CustomStringConvertible, Equatable {
return distance != nil
}

public func remove(edge: Edge) {
neighbors.removeAtIndex(neighbors.indexOf { $0 === edge }!)
public func remove(_ edge: Edge) {
neighbors.remove(at: neighbors.index { $0 === edge }!)
}
}

public func == (lhs: Node, rhs: Node) -> Bool {
public func == (_ lhs: Node, rhs: Node) -> Bool {
return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
}
2 changes: 1 addition & 1 deletion Depth-First Search/DepthFirstSearch.swift
@@ -1,4 +1,4 @@
func depthFirstSearch(graph: Graph, source: Node) -> [String] {
func depthFirstSearch(_ graph: Graph, source: Node) -> [String] {
var nodesExplored = [source.label]
source.visited = true

Expand Down
2 changes: 1 addition & 1 deletion Depth-First Search/README.markdown
Expand Up @@ -27,7 +27,7 @@ The parent of a node is the one that "discovered" that node. The root of the tre
Simple recursive implementation of depth-first search:

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

Expand Down