Title: Graph Algorithms: Topological Sort
1Graph Algorithms Topological Sort
The topological sorting problem given a
directed, acyclic graph G (V, E) , find a
linear ordering of the vertices such that
for all (v, w) ? E, v precedes w in the ordering.
B
C
A
F
D
E
2Graph Algorithms Topological Sort
The topological sorting problem given a
directed, acyclic graph G (V, E) , find a
linear ordering of the vertices such that
for all (v, w) ? E, v precedes w in the ordering.
B
C
A
E
A
D
F
B
C
F
D
E
3Graph Algorithms Topological Sort
The topological sorting problem given a
directed, acyclic graph G (V, E) , find a
linear ordering of the vertices such that
for all (v, w) ? E, v precedes w in the ordering.
Any linear ordering in which all the arrows go to
the right.
B
C
A
E
A
D
F
B
C
F
D
E
4Graph Algorithms Topological Sort
The topological sorting problem given a
directed, acyclic graph G (V, E) , find a
linear ordering of the vertices such that
for all (v, w) ? E, v precedes w in the ordering.
Any linear ordering in which all the arrows go to
the right.
B
C
A
F
A
D
E
B
C
F
D
E
This is not a topological ordering.
5Graph Algorithms Topological Sort
The topological sorting algorithm Identify
the subset of vertices that have no incoming
edge.
B
C
A
F
D
E
6Graph Algorithms Topological Sort
The topological sorting algorithm Identify
the subset of vertices that have no incoming
edge. (In general, this subset must be
nonemptywhy?)
B
C
A
F
D
E
7Graph Algorithms Topological Sort
The topological sorting algorithm Identify
the subset of vertices that have no incoming
edge. (In general, this subset must be
nonemptybecause the graph is acyclic.)
B
C
A
F
D
E
8Graph Algorithms Topological Sort
The topological sorting algorithm Identify
the subset of vertices that have no incoming
edge. Select one of them.
B
C
A
F
D
E
9Graph Algorithms Topological Sort
The topological sorting algorithm Remove it,
and its outgoing edges, and add it to the
output.
B
C
A
F
D
E
10Graph Algorithms Topological Sort
The topological sorting algorithm Again,
identify the subset of vertices that have no
incoming edge, . . .
B
C
A
F
D
E
11Graph Algorithms Topological Sort
The topological sorting algorithm Again,
identify the subset of vertices that have no
incoming edge, select one of them, . . .
B
C
A
F
D
E
12Graph Algorithms Topological Sort
The topological sorting algorithm Again,
identify the subset of vertices that have no
incoming edge, select one of them, remove it
and any outgoing edges, and put it in the output.
B
C
A
F
D
E
13Graph Algorithms Topological Sort
The topological sorting algorithm Again,
identify the subset of vertices that have no
incoming edge, select one of them, remove it
and any outgoing edges, and put it in the output.
B
C
A
F
D
E
14Graph Algorithms Topological Sort
The topological sorting algorithm Again,
identify the subset of vertices that have no
incoming edge, select one of them, remove it
and any outgoing edges, and put it in the output.
C
A
B
F
D
E
15Graph Algorithms Topological Sort
The topological sorting algorithm Again,
identify the subset of vertices that have no
incoming edge, select one of them, remove it
and any outgoing edges, and put it in the output.
C
A
B
F
D
E
16Graph Algorithms Topological Sort
The topological sorting algorithm Again,
identify the subset of vertices that have no
incoming edge, select one of them, remove it
and any outgoing edges, and put it in the output.
A
B
C
F
D
E
17Graph Algorithms Topological Sort
The topological sorting algorithm Again,
identify the subset of vertices that have no
incoming edge, select one of them, remove it
and any outgoing edges, and put it in the output.
A
B
C
F
D
E
18Graph Algorithms Topological Sort
The topological sorting algorithm Again,
identify the subset of vertices that have no
incoming edge, select one of them, remove it
and any outgoing edges, and put it in the output.
A
B
C
F
D
E
19Graph Algorithms Topological Sort
The topological sorting algorithm Again,
identify the subset of vertices that have no
incoming edge, select one of them, remove it
and any outgoing edges, and put it in the output.
A
B
C
F
D
E
20Graph Algorithms Topological Sort
The topological sorting algorithm Again,
identify the subset of vertices that have no
incoming edge, select one of them, remove it
and any outgoing edges, and put it in the output.
A
B
C
F
D
E
21Graph Algorithms Topological Sort
The topological sorting algorithm finished!
B
C
A
A
B
C
F
D
E
F
D
E
22Graph Algorithms Topological Sort
The topological sorting algorithm Time bound?
B
C
A
A
B
C
F
D
E
F
D
E
23Graph Algorithms Topological Sort
The topological sorting algorithm Time
bound Break down into total time to Find
vertices with no predecessors ? Remove edges
? Place vertices in output ?
B
C
A
A
B
C
F
D
E
F
D
E
24Graph Algorithms Topological Sort
The topological sorting algorithm Time
bound Break down into total time to Find
vertices with no predecessors ? Remove edges
O(E) Place vertices in output ?
B
C
A
A
B
C
F
D
E
F
D
E
25Graph Algorithms Topological Sort
The topological sorting algorithm Time
bound Break down into total time to Find
vertices with no predecessors ? Remove edges
O(E) Place vertices in output O(V)
B
C
A
A
B
C
F
D
E
F
D
E
26Graph Algorithms Topological Sort
Find vertices with no predecessors ?
Assume an adjacency list representation
A B C D E F
B
D
B
C
C
A
E
D
F
E
D
E
27Graph Algorithms Topological Sort
The topological sorting algorithm and
initialize and maintain for each vertex its
no. of predecessors.
B
0
D
A B C D E F
1
1
B
1
C
0
C
1
0
A
E
D
F
2
E
2
2
2
D
E
0
28Graph Algorithms Topological Sort
Find vertices with no predecessors ?
Time for each vertex O(V)
B
0
D
A B C D E F
B
1
C
C
1
A
E
D
F
2
E
2
D
E
0
29Graph Algorithms Topological Sort
Find vertices with no predecessors ?
2
Total time O(V )
B
0
D
A B C D E F
B
1
C
C
1
A
E
D
F
2
E
2
D
E
0
30Graph Algorithms Topological Sort
The topological sorting algorithm Time
bound Break down into total time to Find
vertices with no predecessors O(V ) Remove
edges O(E) Place vertices in output O(V)
2
31Graph Algorithms Topological Sort
The topological sorting algorithm Time
bound Break down into total time to Find
vertices with no predecessors O(V ) Remove
edges O(E) Place vertices in output O(V)
2
2
Total O(V E)
32Graph Algorithms Topological Sort
The topological sorting algorithm Time
bound Break down into total time to Find
vertices with no predecessors O(V ) Remove
edges O(E) Place vertices in output O(V)
2
2
Total O(V E)
Too much!
33Graph Algorithms Topological Sort
The topological sorting algorithm We need a
faster way to do this step Find vertices with
no predecessors.
34Graph Algorithms Topological Sort
The topological sorting algorithm Key idea
initialize and maintain a queue (or
stack) holding pointers to the vertices with 0
predecessors
A B C D E F
B
0
D
1
1
B
1
C
0
C
0
1
A
E
D
F
2
E
2
2
2
D
E
0
35Graph Algorithms Topological Sort
The topological sorting algorithm As each
vertex is removed, update the predecessor counts,
and for any vertex whose count has
become zero, put it in the queue.
A B C D E F
B
0
D
1
1
B
1
C
0
C
0
1
A
E
D
F
2
E
2
2
2
D
E
0
36Graph Algorithms Topological Sort
The topological sorting algorithm As each
vertex is removed, update the predecessor counts,
and for any vertex whose count has
become zero, put it in the queue.
No scan is required, so O(1).
A B C D E F
0
0
1
B
0
C
C
0
1
E
D
F
1
E
1
2
2
D
Output A
E
0
37Graph Algorithms Topological Sort
The topological sorting algorithm As each
vertex is removed, update the predecessor counts,
and for any vertex whose count has
become zero, put it in the queue.
A B C D E F
0
0
1
B
0
C
C
1
E
D
1
E
1
2
2
D
Output A F
E
0
38Graph Algorithms Topological Sort
The topological sorting algorithm As each
vertex is removed, update the predecessor counts,
and for any vertex whose count has
become zero, put it in the queue.
A B C D E F
0
0
0
C
0
E
D
1
E
1
2
2
D
Output A F B
E
0
39Graph Algorithms Topological Sort
The topological sorting algorithm As each
vertex is removed, update the predecessor counts,
and for any vertex whose count has
become zero, put it in the queue.
A B C D E F
0
0
0
0
E
0
1
1
D
Output A F B C
E
0
40Graph Algorithms Topological Sort
The topological sorting algorithm As each
vertex is removed, update the predecessor counts,
and for any vertex whose count has
become zero, put it in the queue.
A B C D E F
0
0
0
0
0
0
Output A F B C D
E
0
41Graph Algorithms Topological Sort
The topological sorting algorithm As each
vertex is removed, update the predecessor counts,
and for any vertex whose count has
become zero, put it in the queue. Finished!
A B C D E F
0
0
0
0
0
Output A F B C D E
0
42Graph Algorithms Topological Sort
The topological sorting algorithm Time
bound Now the time for each part is Find
vertices with no predecessors O(V) Remove
edges O(E) Place vertices in output O(V)
Total O(VE)
Linear in VE. Much better!