A Comparison Divide & Conquer Dynamic Programming Algorithm View problem as collection of Recursive nature Independent overlapping typically sequential dependence Number of depends on partitioning factors typically small Preprocessing typically sort Characteristic running time Primarily for optimization problems Optimal substructure: optimal solution to problem contains within it optimal solutions to choice property: locally optimal produces globally optimal Heuristic version useful for bounding optimal value typically log function of n depends on number and difficulty of often dominated by nlogn sort 9/7/10 1 What is a Algorithm? Solves an optimization problem: the solution is best in some sense. Strategy: At each decision point, do what looks best locally Choice does not depend on evaluating potential future choices or solving Top-down algorithmic structure With each step, reduce problem to a smaller problem Optimal Substructure: optimal solution contains in it optimal solutions to Choice Property: locally best = globally best 9/7/10 What is a Algorithm? Solves an optimization problem: the solution is best in some sense. Strategy: At each decision point, do what looks best locally Choice does not depend on evaluating potential future choices or solving Top-down algorithmic structure With each step, reduce problem to a smaller problem Optimal Substructure: optimal solution contains in it optimal solutions to Choice Property: locally best = globally best 9/7/10 3 See also: http://en.wikipedia.org/wiki/_algorithm With a greedy algorithm for the N-queens problem. 9/7/10 1
Examples: Minimum Spanning Tree Minimum Spanning Forest Dijkstra Shortest Path Huffman Codes Fractional Knapsack Activity Selection Minimum Spanning Tree Invariant: Minimum weight spanning forest Becomes single tree at end Invariant: Minimum weight tree Spans all vertices at end A B 3 G 5 1 E 1 7 8 F D C for Undirected, Connected, Weighted Graph G=(V,E) 9/7/10 5 9/7/10 Single Source Shortest Paths: Dijkstra s Algorithm Huffman Codes A B 3 G 5 1 E 1 7 8 F D C 9/7/10 7 9/7/10 8
Fractional Knapsack : Example Activity Selection: Problem Instance Set S = {1,,..., n} of n activities Each activity i has: start time: s i finish time: f i si f i Activities i, j are compatible iff nonoverlapping: [s i, f i ) \Å [s j, f j ) =. Objective: select a maximum-sized set of mutually compatible activities 9/7/10 9 9/7/10 10 Algorithm Why does this all work? Algorithm: S = presort activities in S by nondecreasing finish time and renumber GREEDY-ACTIVITY-SELECTOR(S, f) n = S.length A = {1} j = 1 for i = to n return A» if s i f i» A = A [ {i}» j = i Why does it provide an optimal (maximal) solution? It is clear that it will provide a solution (a set of nonoverlapping activities), but there is no obvious reason to believe that it will provide a maximal set of activities. We start with a restatement of the problem in such a way that a dynamic programming solution can be constructed. This solution will be shown to be maximal. It will then be modified into a greedy solution in such a way that maximality will be preserved. 9/7/10 11 9/7/10 1 3
Consider the set S of activities, sorted in monotonically increasing order of finishing time: i s i f i 1 3 5 7 8 9 10 11 1 3 0 5 3 5 8 8 1 5 7 8 9 10 11 1 13 1 Where the activity a i corresponds to the time interval [s i, f i ). The subset {a 3, a 9, a 11 } consists of mutually compatible activities, but is not maximal. The subsets {a 1, a, a 8, a 11 }, and {a, a, a 9, a 11 } are also compatible and are both maximal. 9/7/10 13 First Step: find an optimal substructure: an optimal solution to a subproblem that can be extended to a full solution. Define an appropriate space of : S ij = {a k S: f i s k < f k s j }, the set of all those activities compatible with a i and a j and with those that finish no later than a i and start no earlier than a j. Add two activities: a 0 :[, 0); a n+1 :[, ); which come before and after, respectively, all activities in S = S 0,n+1. Assume the activities are sorted in non-decreasing order of finish: f 0 f 1 f n < f n+1. Then S ij is empty whenever i j. 9/7/10 1 We define the suproblems: find a maximum size subset of mutually compatible activities from S ij, 0 i < j n+1. What is the substructure? Substructure: suppose a solution to S ij contains some activity a k, f i s k < f k s j. a k generates two, S ik and S kj. The solution to S ij is the union of a solution to S ik, the singleton activity {a k }, and a solution to S kj. Any solution to a larger problem can be obtained by patching together solutions for smaller problems. Cardinality (=number of activities) is also additive. Optimal Substructure: assume A ij is an optimal solution to S ij, containing an activity a k. Then A ij contains a solution to S ik (the activities that end before the beginning of a k ) and a solution to S kj (the activities that begin after the end of a k ). If these solutions were not already maximal, then one could obtain a maximal solution for, say, S ik, and splice it with {a k } and the solution to S kj to obtain a solution for S ij of greater cardinality, thus violating the optimality condition assumed for A ij. 9/7/10 15 9/7/10 1
Use Optimal Substructure to construct an optimal solution: Any solution to a nonempty problem S ij includes some activity a k, and any optimal solution to S ij must contain optimal solutions to S ik and S kj. For each a k in S 0,n+1, find (recursively) optimal solutions of S 0k and S k,n+1, say A 0k and A k,n+1. Splice them together, along with a k, to form solutions A k. Take a maximal A k as an optimal solution A 0,n+1. We need to look at the recursion in more detail (cost??) Second Step: the recursive algorithm. Let c[i,j] denote the maximum number of compatible activities in S ij. It is easy to see that c[i,j] = 0, whenever i j - and S ij is empty. If, in a non-empty set of activities S ij, the activity a k occurs as part of a maximal compatible subset, this generates two, S ik and S kj, and the equation c[i,j] = c[i,k] + 1 + c[k,j], which tells us how the cardinalities are related. The problem is that k is not known a priori. Solution: c[i,j] = max i<k<j (c[i,k] + 1 + c[k,j]), if S ij. And one can write an easy bottom up (dynamic programming) algorithm to compute a maximal solution. 9/7/10 17 9/7/10 18 A better mousetrap. Recall S ij = {a k S: f i s k < f k s j }. Theorem 1.1. Consider any nonempty subproblem S ij, and let a m be the activity in S ij with the earliest finish time: f m = min{f k : a k S ij }. Then: 1. a m is used in some maximum size subset of mutually compatible activities of S ij (e.g., A ij = A im U {a m } U A mj ).. The subproblem S im is empty, so that choosing a m leaves the subproblem S mj as the only one that may be nonempty. Proof: ) if S im is nonempty then S ij must contain an activity (finishing) prior to a m. Contradiction. 1) Suppose A ij is a maximal subset. Either a m is in A ij, and we are done, or it is not. If not, let a k be the earliest finishing activity in A ij. It can be replaced by a m (since it finishes no earlier than a m ) thus giving a maximal subset containing a m. 9/7/10 19 Why is this a better mousetrap? The dynamic programming solution requires solving j - i 1 to solve S ij. Total Running Time??? Theorem 1.1 gives us conditions under which solving S ij requires solving ONE subproblem only, since the other implied by the dynamic programming recurrence relation are empty, or irrelevant (they might give us other maximal solutions, but we need just one) Lots less computation Another benefit comes from the observation that the problem can be solved in a top-down fashion: take the earliest finishing activity, a m, and you are left with the problem S m,n+1. It is easy to see that each activity needs to be looked at only once: linear time (after sorting). 9/7/10 0 5
The Algorithm: R_A_S(s, f, k, n) 1. m = k + 1. while m n and s[m] < f[k] // find first activity in S k to finish 3. do m = m + 1. if m n 5. then return {a m } + R_A_S(s, f, m, n). else return. The Recursive Activity Selector Algorithm: Cormen, Leiserson, Rivest, Stein: Fig. 1.1 The time is - fairly obviously Θ(n). 9/7/10 1 9/7/10 A Slightly Different Perspective. Rather than start from a dynamic programming approach, moving to a greedy strategy, start with identifying the characteristics of the greedy approach. Choice Property. Choice that looks best in the current problem will work: no need to look ahead to its implications for the solution. 1. Make a choice and leave a subproblem to solve.. Prove that the greedy choice is always safe - there is an optimal solution starting from a greedy choice. 3. Prove that, having made a greedy choice, the solution of the remaining subproblem can be added to the greedy choice providing a solution to the original problem. This is important because it reduces the amount of computational complexity we have to deal with. Can t expect this to hold all the time: maximization of a function with multiple relative maxima on an interval - the gradient method would lead to a greedy choice, but may well lead to a relative maximum that is far from the actual maximum. 9/7/10 3 9/7/10
Optimal Substructure. While the greedy choice property tells us that we should be able to solve the problem with little computation, we need to know that the solution can be properly reconstructed: an optimal solution contains optimal sub-solutions to the. Note: An induction can then be used to turn the construction around (bottom-up). In the case of a problem possessing only the Optimal Substructure property there is little chance that we will be able to find methods more efficient than dynamic programming (with memoization). Upper Bound Lower Bound 9/7/10 5 9/7/10 7