Appended Too Early
A DFS-based topological sort:
order = []
visited = [False] * n
def dfs(v):
visited[v] = True
order.append(v)
for u in adj[v]:
if not visited[u]:
dfs(u)
for v in range(n):
if not visited[v]:
dfs(v)
print(*order)
On the DAG 0→1, 0→2, 1→3, 2→3 with adjacency lists in increasing order, it prints 0 1 3 2. What is wrong with that output?
Sign in to answer questions and track your progress
Sign In