We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4e470ba commit 79a9f54Copy full SHA for 79a9f54
pygorithm/searching/breadth_first_search.py
@@ -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