2. 2
Ordering a graph
• Suppose we have a directed acyclic graph (DAG) of courses, and we
want to find an order in which the courses can be taken.
Must take all prereqs before you can take a given course. Example:
• [142, 143, 140, 154, 341, 374, 331, 403, 311, 332, 344,
312, 351, 333, 352, 373, 414, 410, 417, 413, 415]
• There might be more than one allowable ordering.
How can we find a valid ordering of the vertices?
142
143
154
140
311
312
331
351
333
341
344
403
352
373
410
332
374
415
413
417
414
3. 3
Topological Sort
• topological sort: Given a digraph G = (V, E), a total ordering of G's
vertices such that for every edge (v, w) in E, vertex v precedes w in
the ordering. Examples:
determining the order to recalculate updated cells in a spreadsheet
finding an order to recompile files that have dependencies
• (any problem of finding an order to perform tasks with dependencies)
142
143
154
140
311
312
331
351
333
341
344
403
352
373
410
332
374
415
413
417
414
4. 4
Topo sort example
• How many valid topological sort orderings can you find for the
vertices in the graph below?
[A, B, C, D, E, F], [A, B, C, D, F, E],
[A, B, D, C, E, F], [A, B, D, C, F, E],
[B, A, C, D, E, F], [B, A, C, D, F, E],
[B, A, D, C, E, F], [B, A, D, C, F, E],
[B, C, A, D, E, F], [B, C, A, D, F, E],
...
What if there were a new vertex G unconnected to the others?
C
A
F
D
B E
5. 5
Topo sort: Algorithm 1
• function topologicalSort():
ordering := { }.
Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
(If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
C
A
F
D
B E
6. 6
Topo sort example
• function topologicalSort():
ordering := { }.
Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
(If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
ordering = { B }
C
A
F
D
B E
7. 7
Topo sort example
• function topologicalSort():
ordering := { }.
Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
(If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
ordering = { B, C }
C
A
F
D
B E
8. 8
Topo sort example
• function topologicalSort():
ordering := { }.
Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
(If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
ordering = { B, C, A }
C
A
F
D
B E
9. 9
Topo sort example
• function topologicalSort():
ordering := { }.
Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
(If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
ordering = { B, C, A, D }
C
A
F
D
B E
10. 10
Topo sort example
• function topologicalSort():
ordering := { }.
Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
(If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
ordering = { B, C, A, D, F }
C
A
F
D
B E
11. 11
Topo sort example
• function topologicalSort():
ordering := { }.
Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
(If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph.
• ordering += v .
ordering = { B, C, A, D, F, E }
C
A
F
D
B E
12. 12
Revised algorithm
• We don't want to literally delete vertices and edges from the graph
while trying to topological sort it; so let's revise the algorithm:
map := {each vertex its in-degree}.
queue := {all vertices with in-degree = 0}.
ordering := { }.
Repeat until queue is empty:
• Dequeue the first vertex v from the queue.
• ordering += v.
• Decrease the in-degree of all v's neighbors by 1 in the map.
• queue += {any neighbors whose in-degree is now 0}.
If all vertices are processed, success.
Otherwise, there is a cycle.