Skip to content

BFS (Graph Algorithm) #38

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

Merged
merged 2 commits into from
Oct 14, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Graph_Algorithms/src/Breadth_First_Traversal/BFS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#BFS by Master-Fury
import collections

"""
Wrapper function for the print function.
Used as the default visitFunc for bfs
"""
def visitPrint(i):
print(i)

"""
A class representing a undirected graph of nodes.
An edge can be added between two nodes by calling addEdge
*This class assumes all edge weights are equal
"""
class Graph:
def __init__(self):
self.adjList = collections.defaultdict(set)

def addEdge(self, node1, node2):
self.adjList[node1].add(node2)
self.adjList[node2].add(node1)

"""
Given a 'start' node and a 'graph', call visitFunc
sequentially on the current node, and then its children
and so forth.
When visiting each node, mark it as visited by adding it to the hashmap.
Then queue up all of its children to be visited next.
"""
def bfs(start, graph, visitFunc=visitPrint):
visited = collections.defaultdict(bool)
queue = collections.deque()

queue.append(start)

while(len(queue) > 0):
current = queue.popleft()

if (not visited[current]):
visited[current] = True
visitFunc(current)
for neighbor in graph.adjList[current]:
queue.append(neighbor)

# Testing the breadth first search implementation
if __name__ == "__main__":

# Testing on this tree
# 1
# / \
# / \
# 2 3
# / \ / \
# 4 5 6 7

g = Graph()
g.addEdge(1, 2)
g.addEdge(1, 3)
g.addEdge(2, 4)
g.addEdge(2, 5)
g.addEdge(3, 6)
g.addEdge(3, 7)

print("Test 1:")
bfs(1, g)

print("\nTest2:")
bfs(2, g)
54 changes: 54 additions & 0 deletions Graph_Algorithms/src/Depth_First_Traversal/DFS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#DFS by Master-Fury
import collections

class Graph:
def __init__(self):
self.adjList = collections.defaultdict(set)

def addEdge(self, node1, node2):
self.adjList[node1].add(node2)
self.adjList[node2].add(node1)

def dfsHelper(current, graph, visited, visitFunc):
if (visited[current]):
return

visited[current] = True;

visitFunc(current)

for neighbor in graph.adjList[current]:
dfsHelper(neighbor, graph, visited, visitFunc)


def dfs(current, graph, visitFunc):
visited = collections.defaultdict(bool)
dfsHelper(current, graph, visited, visitFunc)

def visitPrint(i):
print(i)

# Testing the depth first search implementation
if __name__ == "__main__":

# Testing on this tree
# 1
# / \
# / \
# 2 3
# / \ / \
# 4 5 6 7

g = Graph()
g.addEdge(1, 2)
g.addEdge(1, 3)
g.addEdge(2, 4)
g.addEdge(2, 5)
g.addEdge(3, 6)
g.addEdge(3, 7)

print("Test 1:")
dfs(1, g, visitPrint)

print("\nTest2:")
dfs(2, g, visitPrint)