Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- nodes = int(input())
- edges = int(input())
- graph = [[] for _ in range(nodes + 1)]
- for _ in range(edges):
- source, destination = [int(x) for x in input().split()]
- graph[source].append(destination)
- start_node = int(input())
- end_node = int(input())
- visited = [False] * (nodes + 1)
- parent = [None] * (nodes + 1)
- visited[start_node] = True
- q = deque([start_node])
- while q:
- node = q.popleft()
- if node == end_node:
- break
- for child in graph[node]:
- if visited[child]:
- continue
- visited[child] = True
- q.append(child)
- parent[child] = node
- path = deque()
- node = end_node
- while node is not None:
- path.appendleft(node)
- node = parent[node]
- print(f'Shortest path length is: {len(path)-1}')
- print(*path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement