Skip to content

Commit 79a9f54

Browse files
committed
Added Breadth First Search
1 parent 4e470ba commit 79a9f54

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Author: OMKAR PATHAK
2+
# Created On: 1st August 2017
3+
4+
# breadth first search algorithm
5+
def bfs(graph, startVertex):
6+
# Take a list for stoting already visited vertexes
7+
if startVertex not in graph or graph[startVertex] is None or graph[startVertex] == []:
8+
return None
9+
10+
# create a list to store all the vertexes for BFS and a set to store the visited vertices
11+
visited, queue = set(), [startVertex]
12+
13+
while queue:
14+
vertex = queue.pop(0)
15+
if vertex not in visited:
16+
visited.add(vertex)
17+
queue.extend(graph[vertex] - visited)
18+
19+
return visited
20+
21+
# time complexities
22+
def time_complexities():
23+
return '''O(V + E) where V = Number of vertices and E = Number of Edges'''

0 commit comments

Comments
 (0)