Topological Sorting
Topological Sorting
A topological sort takes a directed acyclic graph and produces a linear ordering of all its vertices such that if the graph
G contains an edge (v, w) then the vertex v comes before vertex w in the ordering.
i. Call DFS (G) for a graph G. Compute finish times for each one of the vertices.
ii. Store the vertices in a list in the decreasing order of their finish times.
iii. Return the ordered list as the result of the topological sort.
1/12 2/11
3/4
A B C
D E F
Topological Sorting: A, B, D, E, F, C
Heat
Pan
1/12 13/14
¾ cup
milk
2/11 3/8
15/16
Pour ¼
1 cup cup
1 egg
mix
17/18 4/7
Turn
1 when
tablespo bubbly
on oil
5/6
Heat
Syrup End/Eat
9/10