Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- graph = {
- 1: [2,3],
- 2: [1,4,5],
- 3: [1,6,7],
- 4: [2],
- 5: [2],
- 6: [3],
- 7: [3],
- }
- def bfs(graph, initial, goal):
- visited = []
- queue = [initial]
- while queue:
- node = queue.pop(0)
- if node not in visited:
- visited.append(node)
- print('The node is:' + str(node))
- if node == goal:
- print('Goal node '+ '"' + str(node) + '"' +' is founded')
- return visited
- neighbours = graph[node]
- print('The neighbours of the node ' + str(node) + ' are: ' + str(neighbours))
- print('The visited nodes: ' + str(visited) + '\n')
- for neighbour in neighbours:
- if neighbour not in visited:
- queue.append(neighbour)
- print('Appending to queue ' + str(neighbour) + '...')
- print('The visited nodes: ' + str(visited) )
- print('The queue now: ' + str(queue)+ '\n')
- return visited
- print(bfs(graph, 1, 7));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement