Skip to content

Commit 6e66690

Browse files
authored
BFS implementation in python fixes
1 parent d5b4b97 commit 6e66690

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Graph Traversal/BFS/bfs.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import collections
22
def bfs(graph, root):
3-
visited, queue = set(), collections.deque([root])
4-
visited.add(root)
3+
visited, queue = [], collections.deque([root])
4+
visited.append(root)
55
while queue:
66
vertex = queue.popleft()
77
for neighbour in graph[vertex]:
88
if neighbour not in visited:
9-
visited.add(neighbour)
10-
queue.append(neighbour)
9+
visited.append(neighbour)
10+
queue.append(neighbour)
11+
return visited
1112
if __name__ == '__main__':
12-
graph = {0: [1, 2], 1: [2], 2: [3], 3: [1,2]}
13-
breadth_first_search(graph, 0)
13+
graph = {0: [1,3], 1: [2], 2: [3], 3: [1,2]}
14+
print bfs(graph, 0)

0 commit comments

Comments
 (0)