forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreadth_first_search.py
More file actions
28 lines (22 loc) · 859 Bytes
/
Copy pathbreadth_first_search.py
File metadata and controls
28 lines (22 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Author: OMKAR PATHAK
# Created On: 1st August 2017
# breadth first search algorithm
def search(graph, startVertex):
# Take a list for stoting already visited vertexes
if startVertex not in graph or graph[startVertex] is None or graph[startVertex] == []:
return None
# create a list to store all the vertexes for BFS and a set to store the visited vertices
visited, queue = set(), [startVertex]
while queue:
vertex = queue.pop(0)
if vertex not in visited:
visited.add(vertex)
queue.extend(graph[vertex] - visited)
return visited
# time complexities
def time_complexities():
return '''O(V + E) where V = Number of vertices and E = Number of Edges'''
# easily retrieve the source code of the bfs function
def get_code():
import inspect
return inspect.getsource(search)