Question DSA-QUE-0016
HardWhat is the output of this code?
from collections import deque
g = {1: [2, 3], 2: [4], 3: [4], 4: []}
seen, q, order = {1}, deque([1]), []
while q:
u = q.popleft()
order.append(u)
for v in g[u]:
if v not in seen:
seen.add(v)
q.append(v)
print(order)
- A.
[2, 4, 1, 3]
- B.
[4, 3, 2, 1]
- C.
[1, 2, 3, 4]
- D.
[1, 3, 2, 4]
Sign up free to see the answer and a step-by-step explanation.