-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbfs.py
More file actions
26 lines (18 loc) · 688 Bytes
/
bfs.py
File metadata and controls
26 lines (18 loc) · 688 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
def bfs(graph, start_node):
visited = []
queue = [start_node]
while queue:
s = queue.pop(0)
if s not in visited:
print(s, end=" ")
visited.append(s)
for neighbour in graph[s]:
if neighbour not in visited:
queue.append(neighbour)
graph_input = {}
num_edges = int(input("Enter the number of edges: "))
for _ in range(num_edges):
src_node, *adj_nodes = input("Enter source node and its adjacent nodes separated by spaces: ").split()
graph_input[src_node] = adj_nodes
start_node = input("Enter the start node for BFS traversal: ")
bfs(graph_input, start_node)