-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.py
More file actions
32 lines (26 loc) · 910 Bytes
/
BFS.py
File metadata and controls
32 lines (26 loc) · 910 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
26
27
28
29
30
31
32
1 # BFS on graph
def bfs(graph, start_node):
visited = []
queue = [start_node]
while queue:
current_node = queue.pop(0)
if current_node not in visited:
visited.append(current_node)
print(current_node, end=" ")
for neighbor in graph[current_node]:
if neighbor not in visited:
queue.append(neighbor)
def graph_input():
graph = {}
nodes = int(input("Enter the number of nodes: "))
for i in range(nodes):
node = input(f"Enter the name of node {i + 1}: ").strip()
neighbors = input(f"Enter the neighbors of {node}: ").strip().split()
graph[node] = neighbors
return graph
if __name__ == "__main__":
print("Graph Input")
graph = graph_input()
start_node = input("Enter the starting node: ").strip()
print("\nBFS Traversal")
bfs(graph, start_node)