LogiQuest
Back to Graphs
19

What is the output of the following code?

from collections import deque
g = {1: [2, 3], 2: [4], 3: [4], 4: []}
seen, q, order = {1}, deque([1]), []
while q:
    v = q.popleft()
    order.append(v)
    for w in g[v]:
        if w not in seen:
            seen.add(w)
            q.append(w)
print(order)
Medium

Discussion (0)

  • No discussion yet. Ask a doubt or share a shortcut.

Log in to join the discussion.