CS 33433 – Fall 2007 Topological Sort Carola Wenk 11/1/07 CS 3343 Analysis of Algorithms 1
Paths, Cycles, Connectivity Let G =( V , E ) be a directed (or undirected) graph • A path from v 1 to v k in G is a sequence of vertices v 1 , v 2 ,…, v k such that ( v i , v {i+1} ) ∈ E (or { v i , v {i+1} } ∈ E if G is undirected) for all i ∈ {1,…, k- 1}. • A path is simple if all vertices in the path are distinct. • A path v 1 , v 2 ,…, v k forms a cycle if v 1 = v k and k ≥ 2. • A graph with no cycles is acyclic . • An undirected acyclic graph is called a tree . (Trees do not have to have a root vertex specified.) • A directed acyclic graph is a DAG . (A DAG can have undirected cycles if the direction of the edges is not considered.) • An undirected graph is connected if every pair of vertices is connected by a path. A directed graph is strongly connected if for every pair u , v ∈ V there is a path from u to v and there is a path from v to u . • The (strongly) connected components of a graph are the equivalence classes of vertices under this reachability relation. CS 3343 Analysis of Algorithms 2 11/1/07
Topological Sort Topologically sort the vertices of a directed acyclic graph ( DAG ): • Determine f : V → {1, 2, …, | V | } such that ( u , v ) ∈ E ⇒ f ( u ) < f ( v ). 1 4 7 1 4 7 8 8 3 3 5 5 6 6 9 9 2 2 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 CS 3343 Analysis of Algorithms 3 11/1/07
Topological Sort Algorithm • Store vertices in a priority min-queue, with the in-degree of the vertex as the key • While queue is not empty • Extract minimum vertex v, and give it next number • Decrease keys of all adjacent vertices by 1 0 1 2 2 1 4 7 1 4 7 8 8 1 3 3 5 5 1 6 0 6 1 9 2 9 2 3 CS 3343 Analysis of Algorithms 4 11/1/07
Topological Sort Algorithm • Store vertices in a priority min-queue, with the in-degree of the vertex as the key • While queue is not empty • Extract minimum vertex v, and give it next number • Decrease keys of all adjacent vertices by 1 0 1 0 0 1 0 2 2 1 1 4 7 1 4 7 8 8 0 1 3 3 5 5 1 6 0 0 6 1 0 9 2 9 2 1 3 0 2 CS 3343 Analysis of Algorithms 5 11/1/07
Topological Sort Runtime Runtime: • O(|V|) to build heap + O(|E|) D ECREASE -K EY ops ⇒ O(|V| + |E| log |V|) with a binary heap ⇒ O(|V| + |E|) with a Fibonacci heap CS 3343 Analysis of Algorithms 6 11/1/07
Depth-First Search revisited DFS( G= ( V,E )) Mark all vertices in G as “unvisited” time=0; for each vertex v ∈ V do if v is unvisited DFS_rec( G , v ) DFS_rec( G, v ) visit v; d[v]=++time; //discover time for each w adjacent to v do if w is unvisited Add edge ( v , w ) to tree T DFS_rec( G , w ) f[v]=++time; //finish time CS 3343 Analysis of Algorithms 7 11/1/07
DFS Edge Classification • Edge ( u , v ) in depth-first forest: • Tree edge: v was discovered by by exploring edge ( u , v ) • Edge ( u , v ) not in depth-first forest: • Back edge: v is ancestor of u in depth-first forest • Forward edge: v is descendant of u in depth-first forest • Cross edge: Any other edge CS 3343 Analysis of Algorithms 8 11/1/07
DFS-Based Topological Sort Algorithm • Call DFS on the directed acyclic graph G =( V , E ) ⇒ Finish time for every vertex • Reverse the finish times (highest finish time becomes the lowest finish time,…) ⇒ Valid function f : V → {1, 2, …, | V |} such that ( u , v ) ∈ E ⇒ f ( u ) < f ( v ). Runtime: O(| V |+| E |) CS 3343 Analysis of Algorithms 9 11/1/07
DFS-Based Topological Sort • Run DFS: 2 /11 /6 1 3 /12 4 /5 14 /17 15/16 7 /10 8 /9 13 /18 • Reverse finish times: 5 8 4 9 2 3 6 7 1 CS 3343 Analysis of Algorithms 10 11/1/07
Topological Sort Runtime Runtime: • O(|V|) to build heap + O(|E|) D ECREASE -K EY ops ⇒ O(|V| + |E| log |V|) with a binary heap ⇒ O(|V| + |E|) with a Fibonacci heap • DFS-based algorithm: O(|V| + |E|) CS 3343 Analysis of Algorithms 11 11/1/07
Recommend
More recommend