-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from dogra25130/graph-traversal
BFS / DFS traversals
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// MARK: - Basic requirement | ||
struct Edge { | ||
let from: Int | ||
let to: Int | ||
} | ||
|
||
public class Node { | ||
var val: Int | ||
var neighbors: [Int] | ||
public init(_ val: Int) { | ||
self.val = val | ||
self.neighbors = [] | ||
} | ||
} | ||
|
||
// MARK: - BFS implementation | ||
func testBFS(edges: [Edge]) { | ||
|
||
var graph = [Int: Node]() | ||
for edge in edges { | ||
graph[edge.from] = Node(edge.from) | ||
graph[edge.to] = Node(edge.to) | ||
} | ||
for edge in edges { | ||
graph[edge.from]?.neighbors.append(edge.to) | ||
graph[edge.to]?.neighbors.append(edge.from) | ||
} | ||
var visited: [Bool] = Array(repeating: false, count: graph.count + 1) | ||
var nodesOfCurrentLevel: [Int] = [] | ||
|
||
for node in 1...graph.count { | ||
if visited[node] == false { | ||
nodesOfCurrentLevel.append(node) | ||
while(nodesOfCurrentLevel.isEmpty == false) { | ||
var nodesOfNextLevel: [Int] = [] | ||
let sizeOfQueue = nodesOfCurrentLevel.count | ||
for index in 0..<sizeOfQueue { | ||
let currNode = nodesOfCurrentLevel[index] | ||
if(visited[currNode] == true){ | ||
continue | ||
} | ||
print("\(currNode) ") | ||
visited[currNode] = true | ||
guard let neighbors = graph[currNode]?.neighbors else { continue } | ||
for neigh in neighbors { | ||
if visited[neigh] == false { | ||
nodesOfNextLevel.append(neigh) | ||
} | ||
} | ||
} | ||
nodesOfCurrentLevel = nodesOfNextLevel | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Input Graph | ||
func setup() { | ||
let edges = [ | ||
Edge(from: 1, to: 2), | ||
Edge(from: 1, to: 4), | ||
Edge(from: 2, to: 3), | ||
Edge(from: 2, to: 4), | ||
Edge(from: 2, to: 5), | ||
Edge(from: 3, to: 5), | ||
Edge(from: 4, to: 5), | ||
Edge(from: 4, to: 6), | ||
Edge(from: 5, to: 6), | ||
Edge(from: 5, to: 6), | ||
Edge(from: 6, to: 7), | ||
] | ||
testBFS(edges: edges) | ||
} | ||
|
||
setup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// MARK: - Basic requirement | ||
struct Edge { | ||
let from: Int | ||
let to: Int | ||
} | ||
|
||
public class Node { | ||
var val: Int | ||
var neighbors: [Int] | ||
public init(_ val: Int) { | ||
self.val = val | ||
self.neighbors = [] | ||
} | ||
} | ||
|
||
// MARK: - DFS Recursion | ||
func dfs(vertex: Int, visited: inout [Bool], graph: [Int: Node]) { | ||
if visited[vertex] == true { | ||
return | ||
} | ||
visited[vertex] = true | ||
print("\(vertex) ") | ||
guard let neighbors = graph[vertex] else { return } | ||
for neighbor in neighbors.neighbors { | ||
dfs(vertex: neighbor, visited: &visited, graph: graph) | ||
} | ||
} | ||
|
||
func testDFS(edges: [Edge]) { | ||
var graph = [Int: Node]() | ||
for edge in edges { | ||
graph[edge.from] = Node(edge.from) | ||
graph[edge.to] = Node(edge.to) | ||
} | ||
for edge in edges { | ||
graph[edge.from]?.neighbors.append(edge.to) | ||
graph[edge.to]?.neighbors.append(edge.from) | ||
} | ||
var visited: [Bool] = Array(repeating: false, count: graph.count + 1) | ||
for node in 1...graph.count { | ||
if visited[node] == false { | ||
dfs(vertex: node, visited: &visited, graph: graph) | ||
} | ||
} | ||
} | ||
|
||
|
||
// MARK: - setup | ||
func setup() { | ||
let edges = [ | ||
Edge(from: 1, to: 2), | ||
Edge(from: 1, to: 4), | ||
Edge(from: 2, to: 3), | ||
Edge(from: 2, to: 4), | ||
Edge(from: 2, to: 5), | ||
Edge(from: 3, to: 5), | ||
Edge(from: 4, to: 5), | ||
Edge(from: 4, to: 6), | ||
Edge(from: 5, to: 6), | ||
Edge(from: 5, to: 6), | ||
Edge(from: 6, to: 7), | ||
] | ||
testDFS(edges: edges) | ||
} |