Skip to content

Commit

Permalink
Merge pull request #45 from dogra25130/graph-traversal
Browse files Browse the repository at this point in the history
BFS / DFS traversals
  • Loading branch information
carloshmartins authored Mar 11, 2024
2 parents 2ce2905 + c459742 commit 5c30db0
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
75 changes: 75 additions & 0 deletions graph/BFS/BFS.swift
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()
64 changes: 64 additions & 0 deletions graph/DFS/DFS.swift
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)
}

0 comments on commit 5c30db0

Please sign in to comment.