Intuition The one core idea
A directed graph is a set of dots joined by one-way arrows. Topological sort lines those dots up on a straight track so that every arrow points forward — which is only possible when the arrows never loop back on themselves.
Before you can trust the parent note, you must own every piece of vocabulary it throws at you. This page builds each one from a picture, in the order they depend on each other. Nothing here assumes you have seen a graph before.
A vertex (plural vertices ) is just a labelled dot. It stands for one "thing" — a task, a course, a step. We usually name them with letters or numbers: u , v , 0 , 5 .
The letters u and v appear everywhere in the parent note. They are not special — they are just placeholder names for "some vertex" and "another vertex", the way x is a placeholder number in algebra.
Why we need it: everything else (edges, orderings) is built on top of vertices. No dots, nothing to connect or sort.
A directed edge written u → v is a one-way arrow from vertex u to vertex v . Read it as "u must come before v " or "u points to v ".
The arrowhead matters. u → v is not the same as v → u — that is the "directed" part. Think of it as "you must read the book (u ) before writing the essay (v )".
Intuition Why the direction is the whole point
Topological sort's entire job is to respect these arrows. If we ignored direction, there would be nothing to sort — we could put dots in any order. The arrow u → v is a rule : u before v , always.
The symbol E (from G = ( V , E ) ) is simply the collection of all these arrows . The symbol V is the collection of all the dots .
Definition Directed graph
A directed graph G is the pair ( V , E ) : a set of vertices V and a set of directed edges E between them. The notation G = ( V , E ) literally means "graph G is made of dots V and arrows E ".
V = the set of dots. Example: V = { 0 , 1 , 2 , 3 , 4 , 5 } .
E = the set of arrows. Example: E = { 5 → 0 , 5 → 2 , 4 → 0 , … } .
Why we need it: the parent note says algorithms run in O ( V + E ) and detects cycles when "emitted < ∣ V ∣ ". You cannot read those lines without knowing V , E , and ∣ V ∣ .
The indegree of a vertex v , written indeg ( v ) , is the number of arrows pointing into v — how many prerequisites v has left.
Count only the arrowheads landing on v , ignore arrows leaving it.
Intuition Why Kahn's algorithm lives and dies on this number
A vertex with indeg ( v ) = 0 has no unfinished prerequisites — it is ready to go right now. That is exactly the vertex Kahn's algorithm emits next. As you remove a vertex, every arrow it sent out disappears, so its neighbours' indegrees drop by one. When a neighbour's count hits zero, it becomes ready. That cascade is the whole algorithm.
Why we need it: the parent's indegree table (0:2, 1:2, 2:1, ...) is meaningless without this definition.
A cycle is a path of arrows that leads back to where it started, e.g. a → b → c → a . Follow the arrows and you return home.
Intuition Why a cycle kills topological sort
A cycle demands: a before b , b before c , and c before a . Chaining these, a must come before a — impossible. So the moment a cycle exists, no valid line-up can exist. This is why the parent keeps checking for cycles.
Zero / degenerate cases you must know:
A self-loop a → a is the smallest cycle — instantly impossible to sort.
A graph with no edges at all: every ordering is valid (nothing constrains anything).
A single vertex, no edges : trivially sorted (the order is just that one vertex).
A DAG is a directed graph with no cycles . "Acyclic" = "without cycles". This is the exact class of graphs for which a topological order exists.
See Directed Acyclic Graph (DAG) for the deep dive. Why we need it: it is the precondition for the whole parent topic.
Definition Vertex colours during DFS
While DFS explores, each vertex has a colour:
White = not visited yet.
Gray = visited, still being explored (currently on the recursion stack — we went in but haven't come out).
Black = fully finished (we explored everything reachable and popped it off).
Intuition Why gray is the cycle detector
If DFS is standing at u , sees an edge u → v , and v is gray , that means v is an ancestor still on the stack — there is already a path v ⇝ u . Adding u → v closes a loop. Gray edge = cycle. This is the "back edge" the parent mentions.
Why we need it: the DFS pseudocode says "if v GRAY: report CYCLE" and "mark u BLACK". Those lines are noise without the colour meanings.
f ( u ) is the moment DFS finishes vertex u — the tick of a clock when we colour u black and drop it in the "done" pile. A larger f ( u ) means "finished later".
Why we need it: the entire correctness of the DFS-based algorithm rests on this one inequality.
O ( V + E )
O ( ⋯ ) describes how the running time grows as the graph grows. O ( V + E ) means the work is proportional to (number of vertices) plus (number of edges) — you touch each dot once and each arrow once. This is as fast as reading the graph itself.
Why we need it: the parent claims both algorithms are O ( V + E ) ; this notation is how we say "linear in the size of the graph".
Edge u to v - one-way arrow
Indegree - arrows into a vertex
Cycle - arrows that loop back
DAG - graph with no cycles
Kahn algorithm - remove indegree zero
Visited colours white gray black
DFS-based topological sort
Read top to bottom: dots and arrows come first, everything about ordering comes last.
Cover the right side and test yourself. You are ready for the parent note when every line is instant.
What does u → v mean in plain words? "u must come before v " — a one-way arrow from u to v .
What is indeg ( v ) ? The number of arrows pointing into v (its remaining prerequisites).
What does ∣ V ∣ mean? The count of vertices — how many dots the graph has.
What is a cycle, and why does it block sorting? A loop of arrows returning to start; it forces a vertex before itself, which is impossible.
What does DAG stand for? Directed Acyclic Graph — a directed graph with no cycles.
When does a topological order exist? If and only if the graph is a DAG.
What do white, gray, and black mean in DFS? Unvisited, on the stack (in progress), fully finished.
What does a gray neighbour signal during DFS? A back edge → a cycle.
State the finish-time law for an edge u → v . f ( u ) > f ( v ) : u finishes after v , so reverse the finish order to sort.
What does O ( V + E ) mean? Running time grows linearly with the number of vertices plus edges.
Parent topic — this page feeds directly into it.
Directed Acyclic Graph (DAG) — the graph class defined in section 6.
Cycle Detection in Directed Graphs — built on the cycle idea (section 5) and gray edges (section 7).
Depth-First Search (DFS) — where finish times and colours come from.
Breadth-First Search (BFS) — the traversal Kahn's indegree method mimics.