CS 401
Greedy Algorithms
Xiaorui Sun
1
CS 401 Greedy Algorithms Xiaorui Sun 1 Directed Acyclic Graphs - - PowerPoint PPT Presentation
CS 401 Greedy Algorithms Xiaorui Sun 1 Directed Acyclic Graphs (DAG) Def: A DAG is a directed acyclic graph, i.e., one that contains no directed cycles. Def: A topological order of a directed graph G = (V, E) is an ordering of its nodes as !
Xiaorui Sun
1
2
a DAG
2 3 6 5 4 7 1
a topological ordering of that DAG– all edges left-to-right
1 2 3 4 5 6 7
3
G has a topological order G is a DAG
Lemma: If ! is a DAG, then ! has a node with no incoming edges (i.e., a source).
Suppose that ! is a DAG and it has no source Pick any node ", and begin following edges backward from ". Since " has at least one incoming edge ($, ") we can walk backward to $. Then, since $ has at least one incoming edge (', $), we can walk backward to '. Repeat until we visit a node, say w, twice. Let C be the sequence of nodes encountered between successive visits to w. C is a cycle.
4
w x u v
C
w x u v
Lemma: If ! is a DAG, then ! has a topological order
Base case: true if " = 1. Hypothesis: Every DAG with " − 1 vertices has a topological ordering. Inductive Step: Given DAG with " > 1 nodes, find a source node '. ! − { ' } is a DAG, since deleting ' cannot create cycles. By hypothesis, ! − { ' } has a topological ordering. Place ' first in topological ordering; then append nodes of ! − {'} in topological order. This is valid since ' has no incoming edges.
5
Reminder: Always remove vertices/edges to use hypothesis
6
G has a topological order G is a DAG
7
2 3 6 5 4 7 1
8
2 3 6 5 4 7 1 1 2 3 4 5 6 7
Running time: O(n+m)
9
11
12
13
14
Easy to produce Fast running times Work only on certain classes of problems
Greedy algorithm stays ahead
some criterion that eventually implies optimality Structural: Come up with a lower bound Exchange Argument
loss in quality
Time 1 2 3 4 5 6 7 8 9 1 1 1
f g h e a b c d h e b
16
Time 1 2 3 4 5 6 7 8 9 10 11
f g h e a b c d h e b
17