CS 3343 Analysis of Algorithms 1 11/1/07
Topological Sort Carola Wenk 11/1/07 CS 3343 Analysis of - - PowerPoint PPT Presentation
Topological Sort Carola Wenk 11/1/07 CS 3343 Analysis of - - PowerPoint PPT Presentation
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
SLIDE 1
SLIDE 2
CS 3343 Analysis of Algorithms 2 11/1/07
Paths, Cycles, Connectivity
Let G=(V,E) be a directed (or undirected) graph
- A path from v1 to vk in G is a sequence of vertices v1, v2,…,vk such that
(vi,v{i+1})∈E (or {vi,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 v1, v2,…,vk forms a cycle if v1=vk 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.
SLIDE 3
CS 3343 Analysis of Algorithms 3 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).
3 3 5 5 6 6 4 4 2 2 7 7 9 9 8 8 1 1 3 3 5 5 6 6 4 4 2 2 7 7 9 9 8 8 1 1
SLIDE 4
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
3 3 5 5 6 6 4 4 2 2 7 7 9 9 8 8 1 1
2 2 1 1 3 1 1
SLIDE 5
CS 3343 Analysis of Algorithms 5 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
3 3 5 5 6 6 4 4 2 2 7 7 9 9 8 8 1 1
2 2 1 1 3 1 1 1 2 1 1
SLIDE 6
CS 3343 Analysis of Algorithms 6 11/1/07
Topological Sort Runtime
Runtime:
- O(|V|) to build heap + O(|E|) DECREASE-KEY ops
⇒ O(|V| + |E| log |V|) with a binary heap ⇒ O(|V| + |E|) with a Fibonacci heap
SLIDE 7
CS 3343 Analysis of Algorithms 7 11/1/07
Depth-First Search revisited
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 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)
SLIDE 8
CS 3343 Analysis of Algorithms 8 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
SLIDE 9
CS 3343 Analysis of Algorithms 9 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|)
SLIDE 10
CS 3343 Analysis of Algorithms 10 11/1/07
DFS-Based Topological Sort
- Run DFS:
1 2 3 4 /5 /6 7 8 /9 /10 /11 13 14 15/16 /17 /18 /12
- Reverse finish times:
9 8 6 7 5 3 2 1 4
SLIDE 11
CS 3343 Analysis of Algorithms 11 11/1/07
Topological Sort Runtime
Runtime:
- O(|V|) to build heap + O(|E|) DECREASE-KEY ops
⇒ O(|V| + |E| log |V|) with a binary heap ⇒ O(|V| + |E|) with a Fibonacci heap
- DFS-based algorithm: O(|V| + |E|)