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 0aec412 commit c66528cCopy full SHA for c66528c
Graphs/BreadtFirstSearch_2.py
@@ -0,0 +1,31 @@
1
+# From https://www.educative.io/edpresso/how-to-implement-a-breadth-first-search-in-python
2
+
3
+def bfs(graph, node):
4
+ visited = [] # List to keep track of visited nodes.
5
+ queue = [] #Initialize a queue
6
7
+ visited.append(node)
8
+ queue.append(node)
9
10
+ while queue:
11
+ s = queue.pop(0)
12
+ print(s, end = " ")
13
14
+ for neighbour in graph[s]:
15
+ if neighbour not in visited:
16
+ visited.append(neighbour)
17
+ queue.append(neighbour)
18
19
20
+# Driver Code
21
22
+graph = {
23
+ 'A' : ['B','C'],
24
+ 'B' : ['D', 'E'],
25
+ 'C' : ['F'],
26
+ 'D' : [],
27
+ 'E' : ['F'],
28
+ 'F' : []
29
+}
30
31
+bfs(graph, 'C')
0 commit comments