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. A B C F D E
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. A B C F D EAD E F BC
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. A B C F D EAD E F BC 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. A B C F D FAD E E BC 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. 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 nonempty—why?) 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 nonempty—because the graph is acyclic.) A B C F D E
Graph Algorithms: Topological Sort The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. Select one of them. A B C F D E
Graph Algorithms: Topological Sort The topological sorting algorithm: Remove it, and its outgoing edges, and add it to the output. A B C F D E
Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge,... A B C F D E
Graph Algorithms: Topological Sort The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them,... A B C F D 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. A B C F D 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. A B C F D 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. AB C F D 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. AB C F D 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. ABCF D 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. ABCF D 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. ABCFD 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. ABCFD 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. ABCFDE
Graph Algorithms: Topological Sort The topological sorting algorithm: finished! ABCFDE A B C F D E
Graph Algorithms: Topological Sort The topological sorting algorithm: Time bound? ABCFDE A B C F 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: ? ABCFDE A B C F 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: ? ABCFDE A B C F 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|) ABCFDE A B C F D E
Graph Algorithms: Topological Sort Find vertices with no predecessors: ? A B C F D E ABCDEFABCDEF B D E E D C Assume an adjacency list representation:
Graph Algorithms: Topological Sort The topological sorting algorithm: …and initialize and maintain for each vertex its no. of predecessors. A B C F D E ABCDEFABCDEF B D E E D C
Graph Algorithms: Topological Sort Find vertices with no predecessors: ? A B C F D E Time for each vertex: O(|V|) ABCDEFABCDEF B D E E D C
Graph Algorithms: Topological Sort Find vertices with no predecessors: ? A B C F D E Total time: O(|V| ) 2 ABCDEFABCDEF B D E E D C
Graph 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
Graph 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 Total: O(|V| + |E|) 2
Graph 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 Total: O(|V| + |E|) 2 Too much!
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 B C F D E ABCDEFABCDEF B D E E D C
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 B C F D E ABCDEFABCDEF B D E E D C
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. B C D E ABCDEFABCDEF E E D C F Output: A No scan is required, so O(1).
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. B C D E ABCDEFABCDEF E E D C Output: A F
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. C D E ABCDEFABCDEF E E D Output: A F B
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. D E ABCDEFABCDEF E Output: A F B C
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. E ABCDEFABCDEF Output: A F B C D
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. Finished! ABCDEFABCDEF Output: A F B C D E
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|) Linear in |V|+|E|. Much better!