-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs2.py
36 lines (31 loc) · 912 Bytes
/
bfs2.py
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
33
34
35
36
def bfs(graph, start, goal):
queue = []
visited = []
queue.append(start)
visited.append(start)
path = ""
found = False
while queue:
m = queue.pop(0)
path += m + " "
if m == goal:
found = True
break
for neighbour in graph.get(m, []):
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
if found:
print("Goal node found")
print("Path:", path)
else:
print("Goal node not found")
n = int(input("Enter no. of nodes: "))
graph = {}
for i in range(n):
key = input("Enter the parent node: ")
value = input("Enter the child nodes (space-separated): ").split()
graph[key] = value
start = input("Enter start node: ")
goal = input("Enter goal node: ")
bfs(graph, start, goal)