Algorithm dossier

  • Category: Graph
  • Worst-case complexity: O(n)
  • Approach: Iterative
  • Data structure: Graph
  • First formalised: 1960s

Why this snippet is Topological Sort

Why topological sort (Kahn). The d array is in-degrees. Seed the queue with zero-in-degree vertices; pop one, append to output, decrement neighbours, push any new zeroes. The output is a linear order respecting every directed edge u → v. Failure mode. If the graph has a cycle, the queue drains before all vertices are emitted — len(out) < n is the cycle-detection check the caller would add.

How to read a redacted algorithm

Algodle strips identifier names so the snippet has to be read for its shape: the control flow, the data structures it manipulates, the order in which it visits its input. Loops with two pointers crawling toward each other are usually search or partition. A recursion that splits its input in half and recurses on both halves is divide-and-conquer. A priority queue plus graph traversal is almost certainly Dijkstra, Prim, or A*. Six hint columns — category, complexity, approach, data structure, era — let you triangulate even when the snippet itself is opaque.