-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtempCodeRunnerFile.python
68 lines (47 loc) · 1.66 KB
/
tempCodeRunnerFile.python
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Heba Ashraf Raslan Sec:2 BN:32
from collections import defaultdict
from collections import deque
class Graph:
def __init__(self):
self.adj_list = defaultdict(list)
def add_edge(self, u, v):
self.adj_list[u].append(v)
self.adj_list[v].append(u)
def simulate_rreq(self, src, dest):
visited = set()
route = {}
queue = deque()
queue.append(src)
visited.add(src)
route[src] = [src]
while queue:
curr = queue.popleft()
if curr == dest:
continue # Skip destination node for efficiency
for neighbor in sorted(self.adj_list[curr]):
if neighbor not in visited:
visited.add(neighbor)
route[neighbor] = route[curr] + [neighbor]
queue.append(neighbor)
# Extract paths from route (optional)
paths = {k: v for k, v in route.items() if k != dest}
return paths
def print_routes(self, src, dest):
paths = self.simulate_rreq(src, dest)
for i in range(1, len(self.adj_list) + 1):
if i == src:
print(paths[src][0])
elif i not in paths:
print(-1)
else:
print(" ".join(map(str, paths[i])))
def read_input_from_user():
n, m = map(int, input().split())
graph = Graph()
for _ in range(m):
x, y = map(int, input().split())
graph.add_edge(x, y)
source, destination = map(int, input().split())
return graph, source, destination
graph, source, destination = read_input_from_user()
graph.print_routes(source, destination)