Sub topic 14
Tarjan vs. Kosaraju: Memory, Passes, and Output Order
This chapter assumes you have already worked through the earlier chapters on Kosaraju's algorithm, Tarjan's algorithm, and their O(V + E) analysis. You should be comfortable with the condensation DAG and with reading the kosaraju and tarjan functions defined there.
Both algorithms find the same components in linear time. They differ in how many times they walk the graph, how much extra memory they need, and the order in which they report components. Those differences decide which algorithm suits a given problem.
Side-by-Side Summary
| Property | Kosaraju | Tarjan |
|---|---|---|
| DFS passes | Two (graph, then transpose) | One |
| Extra edge storage | Full transpose: E extra entries | None |
| Per-vertex arrays | `visited`, `order`, `comp` | `index`, `low`, `on_stack`, plus the stack |
| Component order | Topological order of the condensation (sources first) | Reverse topological order (sinks first) |
| Conceptual difficulty | Lower: two plain DFS runs | Higher: low-link bookkeeping |
| Typical constant factor | Slower, because it touches every edge twice | Faster in practice |
Memory
Kosaraju's main cost is the transpose. Building radj copies every edge, so on a graph with millions of edges the transpose can take as much memory as the input. If your graph already stores incoming edges, for example as a compressed sparse format with both directions, the second pass can read them directly and this cost goes away.
Tarjan keeps only O(V) extra state: two integer arrays, a boolean array, and a stack that never holds more than V vertices. On a large, dense graph, this saving often decides the choice.
Both algorithms written recursively also use call-stack space proportional to the longest DFS path. The iterative versions move that cost onto an explicit list, but they do not remove it.
Output Order
The output order is the difference people most often get wrong. Kosaraju processes vertices in decreasing finish time on the transpose, so the first component it labels is a source of the condensation. Tarjan pops a component only after everything reachable from it is finished, so the first component it emits is a sink.
If you need components in topological order from Tarjan, reverse its list. The snippet below uses tarjan and kosaraju from the earlier chapters.
def tarjan_labels(n, sccs):
comp = [-1] * n
k = len(sccs)
for i, scc in enumerate(sccs):
for v in scc:
comp[v] = k - 1 - i # reverse so sources get small labels
return comp
adj = [[1], [2], [0, 3], [4], [5], [3], [5, 7], [6]]
print(kosaraju(8, adj)) # [1, 1, 1, 2, 2, 2, 0, 0]
print(tarjan_labels(8, tarjan(8, adj))) # [1, 1, 1, 2, 2, 2, 0, 0]Choosing Between Them
- Pick Tarjan when memory is tight, the graph is large, or reverse topological order is what you want. Dependency resolution, where you must handle a module's dependencies before the module itself, wants exactly this order.
- Pick Kosaraju when you want clear code that is easy to check, when incoming edges are already stored, or when you want sources first without an extra reversal.
- For 2-SAT, either works. Keep track of which direction your component numbers run, because the truth assignment depends on comparing them. The next chapter covers this.
Exercise
A graph has 50 million edges and you only store outgoing adjacency lists. You need each component's label, and you will process components so that every component comes after all components it has edges into. Which algorithm fits better, and do you need to reorder its output?
Show answer
Tarjan fits better. It avoids building a 50-million-edge transpose, and it already emits components sinks first. That means every component appears after the components it points to, so no reordering is needed.
Exercise
Why does Kosaraju's algorithm read every edge at least twice, while Tarjan's reads each edge once?
Show answer
Kosaraju runs one full DFS over the original edges and a second full DFS over the transposed edges. Building the transpose also reads each edge once more. Tarjan's single DFS examines each outgoing edge exactly once.