04 · Graph Traversal & Topology

BFS · DFS · topological sort · Tarjan SCC — same graph · four lenses on its structure.

Build your graph in any panel: click empty space to add a node · drag from one node to another to add a directed edge (drop on the same node for a self-loop) · right-click a node to delete it (and its incident edges). Edits propagate to all four panels and auto-restart the run.

What you're watching

All four panels render the same graph structure (same nodes, same edges). They differ in the colouring each algorithm applies to the nodes once it finishes. Below each panel, result states the algorithm's output in a sentence — the BFS depth range, the DFS finish order, the topological order (or "cycle detected"), the SCC count and group sizes.

The four algorithms

BFS   queue · O(V + E)

Layer-by-layer exploration. Colour each node by its BFS depth from the start (or its layer in its connected component, if the graph has multiple). Useful for finding shortest paths in unweighted graphs — and as the lens that reveals "how spread out" a graph is.

DFS   stack / recursion · O(V + E)

Goes deep, then backtracks. Colour each node by its finish-time rank — the order in which DFS completed processing it. This is the foundation that topological sort and Tarjan SCC build on.

Topological sort   DAG only · O(V + E)

Produces a linear ordering of the nodes such that every edge goes from earlier to later in the sequence. Implemented as DFS with post-order push — the topological order is the reverse of the finish order. Only defined on a DAG; if the graph contains a cycle, this panel turns red and reports "cycle detected — no topological order exists".

Tarjan strongly-connected components   DFS + lowlink · O(V + E)

Finds maximal sets of nodes that are mutually reachable (strongly connected components). A DAG has one SCC per node; a cyclic graph has one or more multi-node SCCs. Each component gets a distinct colour; sizes are reported in the result line.

TRY THIS
Start with the seeded DAG (5 nodes, 5 edges, no cycles). Topological sort succeeds; Tarjan finds 5 single-node SCCs. Now add an edge from the rightmost node back to the leftmost (creates a cycle through all 5 nodes). Topological sort fails immediately; Tarjan groups all 5 nodes into one SCC.

Why these four are presented together

BFS and DFS are the two traversal primitives — every graph algorithm in this list (and many others) is a small modification of one of them. Topological sort is "DFS with post-order push." Tarjan SCC is "DFS with discovery-time + lowlink tracking." Seeing them on the same graph makes the family resemblance visible: each algorithm is the same skeleton with different bookkeeping.

What I learned writing this

← Back to all algorithms