Skip to content

Commit 05b9ef2

Browse files
committed
Adding DFS
1 parent 6a40310 commit 05b9ef2

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

Topological Sort/Topological Sort.playground/Contents.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,27 @@
22

33
import UIKit
44

5-
var str = "Hello, playground"
5+
let graph = Graph()
6+
7+
let node5 = graph.addNode("5")
8+
let node7 = graph.addNode("7")
9+
let node3 = graph.addNode("3")
10+
let node11 = graph.addNode("11")
11+
let node8 = graph.addNode("8")
12+
let node2 = graph.addNode("2")
13+
let node9 = graph.addNode("9")
14+
let node10 = graph.addNode("10")
15+
16+
graph.addEdge(fromNode: node5, toNode: node11)
17+
graph.addEdge(fromNode: node7, toNode: node11)
18+
graph.addEdge(fromNode: node7, toNode: node8)
19+
graph.addEdge(fromNode: node3, toNode: node8)
20+
graph.addEdge(fromNode: node3, toNode: node10)
21+
graph.addEdge(fromNode: node11, toNode: node2)
22+
graph.addEdge(fromNode: node11, toNode: node9)
23+
graph.addEdge(fromNode: node11, toNode: node10)
24+
graph.addEdge(fromNode: node8, toNode: node9)
25+
26+
print(graph.description, terminator: "")
27+
28+
graph.depthFirstSearch("5")
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
public class Graph: CustomStringConvertible {
3+
4+
private var adjacencyLists: [String : [String]]
5+
6+
public init() {
7+
adjacencyLists = [String : [String]]()
8+
}
9+
10+
public func addNode(value: String) -> String {
11+
adjacencyLists[value] = []
12+
return value
13+
}
14+
15+
public func addEdge(fromNode from: String, toNode to: String) -> Bool {
16+
adjacencyLists[from]?.append(to)
17+
return adjacencyLists[from] != nil ? true : false
18+
}
19+
20+
public var description: String {
21+
return adjacencyLists.description
22+
}
23+
24+
private func adjacencyList(forValue: String) -> [String]? {
25+
for (key, adjacencyList) in adjacencyLists {
26+
if key == forValue {
27+
return adjacencyList
28+
}
29+
}
30+
return nil
31+
}
32+
}
33+
34+
extension Graph {
35+
36+
public func depthFirstSearch(source: String) -> [String] {
37+
var result = [String]()
38+
var stack = Stack<String>()
39+
40+
result.append(source)
41+
stack.push(source)
42+
43+
while !stack.isEmpty {
44+
if let element = stack.pop(), temp = adjacencyList(element) {
45+
for node in temp {
46+
stack.push(node)
47+
result.append(node)
48+
}
49+
}
50+
}
51+
52+
return result
53+
}
54+
}
55+
56+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Foundation
2+
3+
public struct Stack<T> {
4+
private var array = [T]()
5+
6+
public var isEmpty: Bool {
7+
return array.isEmpty
8+
}
9+
10+
public var count: Int {
11+
return array.count
12+
}
13+
14+
public mutating func push(element: T) {
15+
array.append(element)
16+
}
17+
18+
public mutating func pop() -> T? {
19+
if isEmpty {
20+
return nil
21+
} else {
22+
return array.removeLast()
23+
}
24+
}
25+
26+
public func peek() -> T? {
27+
return array.last
28+
}
29+
}
30+
31+
extension Stack: SequenceType {
32+
public func generate() -> AnyGenerator<T> {
33+
var curr = self
34+
return anyGenerator {
35+
_ -> T? in
36+
return curr.pop()
37+
}
38+
}
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

0 commit comments

Comments
 (0)