Documente Academic
Documente Profesional
Documente Cultură
CSOR W4246
Eleni Drinea
Computer Science Department
Columbia University
Thursday, September 24, 2015
Outline
1 Recap
2 Applications of DFS
Cycle detection
Topological sorting
Strongly connected components
Today
1 Recap
2 Applications of DFS
Cycle detection
Topological sorting
Strongly connected components
1. Applications of BFS
I
I
2. DFS
I
Today
1 Recap
2 Applications of DFS
Cycle detection
Topological sorting
Strongly connected components
Proof.
If (u, v) is a back edge, together with the path on the DFS tree
from v to u, it forms a cycle.
Conversely, suppose G has a cycle. Let v be the first vertex
from the cycle discovered by DFS(G). Let (u, v) be the
preceding edge in the cycle. Since there is a path from v to
every vertex in the cycle, all vertices in the cycle are now
discovered and fully explored before v is popped from the
stack. Hence the interval of u is contained in the interval of v.
By Claim 1, (u, v) is a back edge.
Definition 1.
A topological ordering of G is an ordering of its nodes as
1, 2, . . . , n such that for every edge (i, j), we have i < j.
A DAG (top left), its topological sort (top right) and a drawing
emphasizing the topological sort (bottom).
Claim 2.
If G has a topological ordering, then G is a DAG.
Proof: By contradiction (exercise).
A visualization of the proof is provided by the linearized graph
of the previous slide: vertices appear in increasing order, edges
go from left to right, hence no cycles.
Is the converse true: does every DAG have a topological
ordering? And how can we find it?
an outgoing edge?
an incoming edge?
Fact 3.
Every DAG has at least one source and at least one sink.
Fact 4.
Let G0 be the graph after a source node and its adjacent edges
have been removed. Then G0 is a DAG.
Proof: removing edges from G cannot yield a cycle!
This gives rise to a recursive algorithm for finding the
topological order of a DAG. Its correctness can be shown by
induction (use Facts 3, 4 to show induction step).
TopologicalOrder(G)
1. Find a source vertex s and order it first.
2. Delete s and its adjacent edges from G; let G0 be the new
graph.
3. TopologicalOrder(G0 )
4. Append the order found after s.
The same reasoning shows that the task w with the second
largest f inish has no incoming edges from any other task
except (maybe) task v. Hence it is safe to perform w
second.
C1
1
3
C2
6
4
C3
Proof.
We will use Lemma 6 below. Let G be a directed graph. The
meta-graph of its SCCs is a DAG. For an SCC C, let
f inish(C) = max f inish(v)
vC
Lemma 6.
Let Ci , Cj be SCCs in G. Suppose there is an edge (u, v) E
such that u Ci and v Cj . Then f inish(Ci ) > f inish(Cj ).
Gr is useful again
Remark 1.
1. Running time: O(n + m) why?
2. Equivalently, we can (i) run DFS(G), compute f inish times;
(ii) run DFS(Gr ) by decreasing order of f inish. Why?
1 (1,8)
2 (2,5)
3 (3,4)
5
4 (6,7)
(9,14)
6 (10,13)
7 (11,12)
(14)
v
(13)
v
(4)
v
v (5)
v (7)
v (12)
Lemma 7.
Let Ci , Cj be SCCs in G. Suppose there is an edge (u, v) E
such that u Ci and v Cj . Then f inish(Ci ) > f inish(Cj ).
Proof of Lemma 6