3.5.7 · D1Graphs

Foundations — Topological sort — DFS-based, Kahn's algorithm (BFS-based)

1,668 words8 min readBack to topic

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.


1. A vertex (a dot)

The letters and appear everywhere in the parent note. They are not special — they are just placeholder names for "some vertex" and "another vertex", the way is a placeholder number in algebra.

Figure — Topological sort — DFS-based, Kahn's algorithm (BFS-based)

Why we need it: everything else (edges, orderings) is built on top of vertices. No dots, nothing to connect or sort.


2. An edge and the arrow

The arrowhead matters. is not the same as — that is the "directed" part. Think of it as "you must read the book () before writing the essay ()".

Figure — Topological sort — DFS-based, Kahn's algorithm (BFS-based)

The symbol (from ) is simply the collection of all these arrows. The symbol is the collection of all the dots.


3. The graph

  • = the set of dots. Example: .
  • = the set of arrows. Example: .

Why we need it: the parent note says algorithms run in and detects cycles when "emitted ". You cannot read those lines without knowing , , and .


4. Indegree

Count only the arrowheads landing on , ignore arrows leaving it.

Figure — Topological sort — DFS-based, Kahn's algorithm (BFS-based)

Why we need it: the parent's indegree table (0:2, 1:2, 2:1, ...) is meaningless without this definition.


5. A cycle (a loop of arrows)

Figure — Topological sort — DFS-based, Kahn's algorithm (BFS-based)

Zero / degenerate cases you must know:

  • A self-loop 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).

6. DAG — Directed Acyclic Graph

See Directed Acyclic Graph (DAG) for the deep dive. Why we need it: it is the precondition for the whole parent topic.


7. Visited colours: white / gray / black

Why we need it: the DFS pseudocode says "if GRAY: report CYCLE" and "mark BLACK". Those lines are noise without the colour meanings.


8. Finish time

Why we need it: the entire correctness of the DFS-based algorithm rests on this one inequality.


9. Big-O:

Why we need it: the parent claims both algorithms are ; this notation is how we say "linear in the size of the graph".


Prerequisite map

Vertex - a dot

Edge u to v - one-way arrow

Graph G equals V and E

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

Finish time f of u

DFS-based topological sort

Topological sort

Big-O of V plus E

Read top to bottom: dots and arrows come first, everything about ordering comes last.


Equipment checklist

Cover the right side and test yourself. You are ready for the parent note when every line is instant.

What does mean in plain words?
" must come before " — a one-way arrow from to .
What is ?
The number of arrows pointing into (its remaining prerequisites).
What does 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 .
: finishes after , so reverse the finish order to sort.
What does mean?
Running time grows linearly with the number of vertices plus edges.

Connections

  • 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.