Question DSA-GRAPH-0042
MediumWhat is the output of this code?
g = {"A": ["B", "C"], "B": ["D"], "C": ["D", "E"], "D": ["F"], "E": ["F"], "F": []}
seen, order = set(), []
def dfs(u):
seen.add(u)
for v in g[u]:
if v not in seen:
dfs(v)
order.append(u)
dfs("A")
print(order[::-1])
- A.
['F', 'D', 'B', 'E', 'C', 'A']
- B.
['A', 'B', 'D', 'F', 'C', 'E']
- C.
Sign up free to see the answer and a step-by-step explanation.