Professional Documents
Culture Documents
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.
2010/11/3
B C A F D E A
B C F D E
B C A F D E A
B C F D E
2010/11/3
B C F D E A
B C F D E A
B C F D E A
B C A D E F
B C A D E F D E C A F B
2010/11/3
C A D E F B D E A F B C
A D E
C E
A E
2010/11/3
B C A F D E A F B C D E A
B C F D E A F B C D E
2010/11/3
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
Too much!
2010/11/3
1 0
1 D
2 E
2010/11/3
Output: AFBC
Finished!