Topological sort

Time complexity: O(V + E).

Result

Examples

How it works

A topological sort orders tasks so each comes after everything it depends on. Kahn’s algorithm repeatedly takes a node with no remaining arrows pointing into it.

When to use it

Build systems, course prerequisites, spreadsheet recalculation and task scheduling.

Other algorithms