Professional Documents
Culture Documents
CSC263 Tutorial 9
Topological sort
• We have a set of tasks and a set of dependencies
(precedence constraints) of form “task A must be
done before task B”
• Topological sort: An ordering of the tasks that
conforms with the given dependencies
• Goal: Find a topological sort of the tasks or decide
that there is no such ordering
Examples
• Scheduling: When scheduling task graphs in
distributed systems, usually we first need to sort the
tasks topologically
...and then assign them to resources (the most
efficient scheduling is an NP-complete problem)
• Or during compilation to order modules/libraries
d c
a g f
e
Examples
• Resolving dependencies: apt-get uses
topological sorting to obtain the admissible
sequence in which a set of Debian packages
can be installed/removed
Topological sort more formally
• Suppose that in a directed graph G = (V, E)
vertices V represent tasks, and each edge (u, v)∊E
means that task u must be done before task v
Cross edges
b c
c c
d=∞ d=1
∞ Next we discover the vertex d
f=∞ b c f=∞
d=∞ d=∞
f=∞ d e f=∞
d=∞ f
f=∞
Topological sort
1) Call DFS(G) to compute the
Time = 3
2
finishing times f[v]
d=2
∞ d=∞
f=∞ d e f=∞
d=∞ f
f=∞
Topological sort
1) Call DFS(G) to compute the
Time = 4
3
finishing times f[v]
d=∞ 2) Let’s
as each
say wevertex is finished,
start the DFS
f=∞ a from
inserttheitvertex
onto cthe front of a
d=1
linked
Next welist
discover the vertex d
d=∞
f=∞ b c f=∞
Next we discover the vertex f
d=2 d=∞ f is done, move back to d
f=∞ d e f=∞
d=3 f
f=4
∞
f
Topological sort
1) Call DFS(G) to compute the
Time = 5
4
finishing times f[v]
d f
Topological sort
1) Call DFS(G) to compute the
Time = 6
5
finishing times f[v]
d f
Topological sort
1) Call DFS(G) to compute the
Time = 7
6
finishing times f[v]
∞
d=9 Let’s now call DFS visit from
f=∞ a the vertex a
d=3 f
f=4
c e d f
Topological sort
1) Call DFS(G) to compute the
Time = 11
10
finishing times f[v]
b c e d f
Topological sort
1) Call DFS(G) to compute the
Time = 12
11
finishing times f[v]
b c e d f
Topological sort
1) Call DFS(G) to compute the
Time = 13
11
finishing times f[v]
d=9 Let’s
WE now
HAVEcallTHE
DFS RESULT!
visit from
f = 12 a the vertex a
3) return the linked list of
d=1 Next we discover the vertex c,
d = 10
b c
vertices
but c was already processed
f = 11 f=8
=> (a,c) is a cross edge
d=2 d=6
d e Next we discover the vertex b
f=5 f=7
b is done as (b,d) is a cross
d=3 edge => now move back to c
f
f=4
a is done as well
a b c e d f
Topological sort
Time = 13
11
The linked list is sorted in
d=9 decreasing order of finishing
f = 12 a
times f[]
d = 10 d=1
f = 11 b c f=8 Try yourself with different
vertex order for DFS visit
d=2 d=6
d e
f=5 f=7 Note: If you redraw the graph
so that all vertices are in a line
d=3 f ordered by a valid topological
f=4 sort, then all edges point
„from left to right“
a b c e d f
Time complexity of TS(G)
• Running time of topological sort:
Θ(n + m)
where n=|V| and m=|E|
• Why? Depth first search takes Θ(n + m) time
in the worst case, and inserting into the front
of a linked list takes Θ(1) time
Proof of correctness
• Theorem: TOPOLOGICAL-SORT(G) produces a
topological sort of a DAG G