Graph TheoryD2Phase 2dagorderingindegree

Topological Sort

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.

Visualizer
O(V + E)Topological Sort
Deterministic Kahn traversal playback with indegree decrements, zero-indegree queue pushes, and dependency-safe output ordering.

Processed

0

Queue Size

0

Relaxations

0

Result

Processing

Ready

Press Play or Step to start execution.

Output Order

n/a

Zero-Indegree Queue

empty

Current Edge

n/a

queuedcurrentprocessedcycle-blocked
No edges available for visualization.
Topological Sort Implementation
Pseudocode + TypeScript
Reference implementation examples are intentionally abstracted from the playback engine so learners can map concepts to code.
pseudocode
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 }
1.00xNo Run