Directed acyclic graph ordering with indegree queue transitions.
Easy Explanation
Topological Sort orders tasks so every prerequisite appears before the tasks that depend on it.
Processed
0
Queue Size
0
Relaxations
0
Result
Processing
Press Play or Step to start execution.
Output Order
n/a
Zero-Indegree Queue
empty
Current Edge
n/a
function topologicalSort(nodeCount, edges):
indegree <- array of nodeCount filled with 0
graph <- adjacency list
for each (from, to) in edges:
add to to graph[from]
indegree[to] <- indegree[to] + 1
queue <- all nodes with indegree 0
order <- []
while queue not empty:
node <- pop from queue
append node to order
for each neighbor in graph[node]:
indegree[neighbor] <- indegree[neighbor] - 1
if indegree[neighbor] == 0:
push neighbor to queue
if length(order) != nodeCount:
return { hasCycle: true, order: order }
return { hasCycle: false, order: order }