Question DSA-GRAPH-0009
MediumWhat is the output of this code?
g = {0: [1, 2], 1: [3], 2: [3], 3: [4], 4: []}
seen, out = set(), []
def dfs(u):
seen.add(u)
for v in g[u]:
if v not in seen:
dfs(v)
out.append(u)
for u in g:
if u not in seen:
dfs(u)
print(out[::-1])
- A.
[0, 1, 3, 2, 4]
- B.
[4, 3, 2, 1, 0]
- C.
Sign up free to see the answer and a step-by-step explanation.