









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










9/5/ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Algorithm Design and Analysis LECTURE 6
Notes
a
x ≤ "x# < x + 1
Lemma : G is strongly connected if and only if for any node s , every other node t has paths to and from s. Theorem. Can determine if G is strongly connected in O(m + n) time. Pf. Pick any node s. Run BFS from s in G. Run BFS from s in Grev. Return true iff all nodes reached in both BFS executions. Correctness follows immediately from the previous lemma. reverse orientation of every edge in G strongly connected not strongly connected
Def. An DAG is a directed graph that contains no directed cycles. Ex. Precedence constraints: edge (vi, vj) means vi must precede vj. Def. A topological order of a directed graph G = (V, E) is an ordering of its nodes as v 1 , v 2 , …, vn so that for every edge (vi, vj) we have i < j. a DAG a topological ordering v 2 v 3 v 6 v 5 v 4 v 7 v 1 v 1 v 2 v 3 v 4 v 5 v 6 v 7
Lemma. If G has a topological order, then G is a DAG. Pf. (by contradiction) Suppose that G has a topological order v 1 , …, vn and that G also has a directed cycle C. Let's see what happens. Let vi be the lowest-indexed node in C, and let vj be the node just before vi; thus (vj, vi) is an edge. By our choice of i, we have i < j. On the other hand, since (vj, vi) is an edge and v 1 , …, vn is a topological order, we must have j < i, a contradiction. ▪ v 1 vi vj vn the supposed topological order: v 1 , …, vn the directed cycle C
Lemma. If G has a topological order, then G is a DAG. Q. Does every DAG have a topological ordering? Q. If so, how do we compute one?
Lemma. If G is a DAG, then G has a topological ordering. Pf. (by induction on n) Base case: true if n = 1. Given DAG on n > 1 nodes, find a node v with no incoming edges. G - { v } is a DAG, since deleting v cannot create cycles. By inductive hypothesis, G - { v } has a topological ordering. Place v first in topological ordering; then append nodes of G - { v } in topological order. This is valid since v has no incoming edges. ▪ DAG v
Theorem. Algorithm finds a topological order in O(m + n) time. Pf. Maintain the following information:
9/5/ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Greedy Algorithms
9/5/ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Interval Scheduling Problem
j
9/5/ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Greedy: Counterexamples
Next lecture
9/5/ A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne