Skip to content
Open
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
73 changes: 73 additions & 0 deletions mpi4py_examples/GBH_EPC/bfs_dfs_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'''
DFS & BFS algorithms on python by:

1- Guillermo Bertrand Hernandez.
2- Erick Perez Coronado.
'''

from collections import deque
import time

class GraphSearchAlgorithms:

def __init__(self, graph=None):
self.graph = graph

def dfs(self, start, visited=None):

#Start measuring time
self.startTime = time.time()

if visited is None: visited = set()

visited.add(start)
print(start, " -> ", end='')

for neighbor in self.graph[start] - visited:
self.dfs(neighbor, visited)

def bfs(self, start):
visited = set()
queue = deque([start])
visited.add(start)

while queue:
node = queue.popleft()
print(node, " -> ", end='')

for neighbor in self.graph[node] - visited:
queue.append(neighbor)
visited.add(neighbor)

def setGraph(self, graph):
self.graph = graph

if __name__ == "__main__":

#Measure time.
startTime = 0
endTime = 0
graph = {
'A': {'B', 'C'},
'B': {'A', 'D', 'E'},
'C': {'A', 'F', 'G'},
'D': {'B'},
'E': {'B'},
'F': {'C'},
'G': {'C'}
}

#Instantiate lexical analyzer
graphSearch = GraphSearchAlgorithms(graph)

startTime = time.time()
print("DFS: ", end='')
graphSearch.dfs('A')
endTime = time.time()
print("\nDFS execution time: ", (endTime - startTime)*1000, " miliseconds.")

startTime = time.time()
print("\nBFS: ", end='')
graphSearch.bfs('A')
endTime = time.time()
print("\nBFS execution time: ", (endTime - startTime)*1000, " miliseconds.")