You are on page 1of 8

2010/11/3

Topological Sort
22c:21 Computer Science II Data Structures
A topological sort of a directed graph is to place all vertices in a list such that if there is a path from v to w, v comes before w in the list. The graph must be acyclic (no cycles exist)

Topological Sort

Applications
A topological sort is useful in something like a course prerequisite system, where you know what classes you need to take before you can take others. Planning and scheduling.

Graph 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) in E, v precedes w in the ordering.
B C A F D E A

Graph 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 F D E A B F C D E

2010/11/3

Graph 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 A B F C D E A Any linear ordering in which all the arrows go to the right.

Graph 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 F D E A B E C D F Any linear ordering in which all the arrows go to the right.

This is not a topological ordering.

Graph Algorithms: Topological Sort


The topological sorting algorithm: Identify the subset of vertices that have no incoming edge.

Graph 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 A

B C F D E

Graph 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.)

Graph 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 A

B C F D E

2010/11/3

Graph Algorithms: Topological Sort


The topological sorting algorithm: Remove it, and its outgoing edges, and add it to the output.

Graph Algorithms: Topological Sort


The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, . . .

B C F D E A

B C F D E A

Graph Algorithms: Topological Sort


The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, . . .

Graph 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 F D E A

B C A D E F

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

Graph 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 D E F D E C A F B

2010/11/3

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

Graph 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 D E F B D E A F B C

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

Graph 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 D E

C E

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

Graph 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 E

2010/11/3

Graph Algorithms: Topological Sort


The topological sorting algorithm: finished!

Graph Algorithms: Topological Sort


The topological sorting algorithm: Time bound?

B C A F D E A F B C D E A

B C F D E A F B C D E

Graph 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 F D E A F B C D E A

Graph 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 F D E A F B C D E

Graph 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 F D E A F B C D E A

Graph Algorithms: Topological Sort

Find vertices with no predecessors: ? Assume an adjacency list representation:


A B C F D E B C D E F B C D E E D

2010/11/3

Graph Algorithms: Topological Sort


The topological sorting algorithm:

Graph Algorithms: Topological Sort

Find vertices with no predecessors: ? and initialize and maintain for each vertex its no. of predecessors.
1 0 A 2 D 2 E B A0 B 1 0 F C1 D 2 E 2 F 0 B 1 C C D E D E E A F Time for each vertex: O(|V|) A0 B C B 1 C1 D 2 E 2 F 0 B C D E E D D

Graph Algorithms: Topological Sort

Graph Algorithms: Topological Sort


The topological sorting algorithm: Time bound: Break down into total time to: 2 Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|)

Find vertices with no predecessors: ?


Total time: O(|V| ) A0 B C A F D E B 1 C1 D 2 E 2 F 0 B C D E E D
2

Graph Algorithms: Topological Sort


The topological sorting algorithm: Time bound: Break down into total time to: 2 Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|)
Total: O(|V| + |E|)
2

Graph Algorithms: Topological Sort


The topological sorting algorithm: Time bound: Break down into total time to: 2 Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|)
Total: O(|V| + |E|)
2

Too much!

2010/11/3

Graph Algorithms: Topological Sort


The topological sorting algorithm: We need a faster way to do this step: Find vertices with no predecessors.

Graph 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 0 B A 2 D 2 E 1 C 0 F B 1 C 1 D 2 E 2 F 0 B C D E E D

1 0

Graph 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.
1 0 A 2 D 2 E B A 0 B 1 0 F C 1 D 2 E 2 F 0 B D 1 C C D E E

Graph 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 No scan is the queue. required, so O(1).
0 B A 0 B 0 0 F C 1 D 1 E 2 F 0 1 C C D E Output: A E

1 D

2 E

Graph 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.
0 B A 0 B 0 C 1 D 1 1 D 2 E E 2 F 0 1 C C D E Output: AF E

Graph 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 0 B 0 C 0 D 1 1 D 2 E E 2 F 0 D E Output: AFB E 0 C

2010/11/3

Graph 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 0 B 0 C 0 D 0 0 D 1 E E 1 F 0 E

Graph 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 0 B 0 C 0 D 0 0 E E 0 F 0 Output: AFBCD

Output: AFBC

Graph 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 0 B 0 C 0 D 0 E 0 F 0 Output: AFBCDE

Graph 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(|V|+|E|)

Finished!

Linear in |V|+|E|. Much better!

You might also like