Problem Solving

Topological Sort

Students put prerequisite craft steps in order.

Topological Sort definition

A topological sort orders the vertices of a directed graph so that every edge points from an earlier vertex to a later one.

In Spanish
ordenamiento topológico
Topic
Graph algorithms and networks

Topological Sort in a picture

Topological Sort diagramFor prerequisites A → C and B → C, both A and B must precede C.Start with A → C and B → CA and B have no incoming edgesPlace A and B in either orderRemove their outgoing edgesPlace C lastBoth prerequisites now precede it
For prerequisites A → C and B → C, both A and B must precede C.

How it works

A directed graph admits such an ordering exactly when it has no directed cycle. One method repeatedly removes a vertex with no incoming edges in the remaining graph, placing it next in the order.

Worked examples

Give a valid order for A → B and B → C.

A, B, C.

Can A and B swap if they both only point to C?

Yes. A, B, C and B, A, C both work.

Can a directed cycle be sorted this way?

No. It would force a vertex to come before itself.

Common mistake

Use edge direction as precedence. A topological sort need not order vertices alphabetically or numerically.

Quick check

Which order works for A → C and B → C?

Must the order be unique?

Show answer

No.

What stops the zero-incoming-edge procedure before all vertices are placed?

Show answer

A directed cycle remains.

Sources

Flashcard

Topological Sort

Reveal the definition

A topological sort orders the vertices of a directed graph so that every edge points from an earlier vertex to a later one.

Effortless Math. “Topological Sort.” Math Dictionary.