1 / 20

CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming

CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming. Objectives: Present the Dynamic Programming paradigm Review the Matrix Chain-Product problem Discuss the General Technique Solve the 0-1 Knapsack Problem Introduce pseudo-polynomial-time algorithms. Dynamic Programming.

mkerns
Télécharger la présentation

CSC401 – Analysis of Algorithms Chapter 5 -- 3 Dynamic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC401 – Analysis of Algorithms Chapter 5 -- 3Dynamic Programming Objectives: Present the Dynamic Programming paradigm Review the Matrix Chain-Product problem Discuss the General Technique Solve the 0-1 Knapsack Problem Introduce pseudo-polynomial-time algorithms CSC401: Analysis of Algorithms

  2. Dynamic Programming • Dynamic Programmingis a general algorithm design technique • Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems • “Programming” here means “planning” • Main idea: • solve several smaller (overlapping) subproblems • record solutions in a table so that each subproblem is only solved once • final state of the table will be (or contain) solution CSC401: Analysis of Algorithms<footer>

  3. Example: Fibonacci numbers • Recall definition of Fibonacci numbers: • f(0) = 0 • f(1) = 1 • f(n) = f(n-1) + f(n-2) • Computing the nth Fibonacci number recursively (top-down): • f(n) • f(n-1) + f(n-2) • f(n-2) + f(n-3) f(n-3) + f(n-4) • ...... • Time efficiency: T(n) = T(n-1)+T(n-2)+1 O(2n) CSC401: Analysis of Algorithms<footer>

  4. Example: Fibonacci numbers • Computing the nth fibonacci number using bottom-up iteration: • f(0) = 0 • f(1) = 1 • f(2) = 0+1 = 1 • f(3) = 1+1 = 2 • f(4) = 1+2 = 3 • f(5) = 2+3 = 5 • f(n-2) = • f(n-1) = • f(n) = f(n-1) + f(n-2) • Time efficiency: O(n) CSC401: Analysis of Algorithms<footer>

  5. Examples of Dynamic Programming Algorithms • Computing binomial coefficients • Optimal chain matrix multiplication • Constructing an optimal binary search tree • Warshall’s algorithm for transitive closure • Floyd’s algorithms for all-pairs shortest paths • Some instances of difficult discrete optimization problems: • travelling salesman • knapsack CSC401: Analysis of Algorithms<footer>

  6. f B j e e A C d i i,j d f Optimal chain matrix multiplication • Review: Matrix Multiplication. • C = A×B • Aisd × eandBise × f • O(def)time CSC401: Analysis of Algorithms<footer>

  7. Matrix Chain-Product: Compute A=A0*A1*…*An-1 Ai is di × di+1 Problem: How to parenthesize? Example B is 3 × 100 C is 100 × 5 D is 5 × 5 (B*C)*D takes 1500 + 75 = 1575 operations B*(C*D) takes 1500 + 2500 = 4000 operations Matrix Chain-Products CSC401: Analysis of Algorithms<footer>

  8. Matrix Chain-Product Algorithm: Try all possible ways to parenthesize A=A0*A1*…*An-1 Calculate number of operations for each one Pick the best one Running time: The number of paranethesizations is equal to the number of binary trees with n nodes This is exponential! It is called the Catalan number, and it is almost 4n. This is a terrible algorithm! A Brute-force Approach CSC401: Analysis of Algorithms<footer>

  9. Idea #1: repeatedly select the product that uses (up) the most operations. Counter-example: A is 10 × 5 B is 5 × 10 C is 10 × 5 D is 5 × 10 Greedy idea #1 gives (A*B)*(C*D), which takes 500+1000+500 = 2000 operations A*((B*C)*D) takes 500+250+250 = 1000 operations Greedy Approaches • Idea #2: repeatedly select the product that uses the fewest operations. • Counter-example: • A is 101 × 11 • B is 11 × 9 • C is 9 × 100 • D is 100 × 99 • Greedy idea #2 gives A*((B*C)*D)), which takes 109989+9900+108900=228789 operations • (A*B)*(C*D) takes 9999+89991+89100=189090 operations • The greedy approach is not giving us the optimal value. CSC401: Analysis of Algorithms<footer>

  10. A “Recursive” Approach • Define subproblems: • Find the best parenthesization of Ai*Ai+1*…*Aj. • Let Ni,j denote the number of operations done by this subproblem. • The optimal solution for the whole problem is N0,n-1. • Subproblem optimality: The optimal solution can be defined in terms of optimal subproblems • There has to be a final multiplication (root of the expression tree) for the optimal solution. • Say, the final multiply is at index i: (A0*…*Ai)*(Ai+1*…*An-1). • Then the optimal solution N0,n-1 is the sum of two optimal subproblems, N0,i and Ni+1,n-1 plus the time for the last multiply. • If the global optimum did not have these optimal subproblems, we could define an even better “optimal” solution. CSC401: Analysis of Algorithms<footer>

  11. A Characterizing Equation • The global optimal has to be defined in terms of optimal subproblems, depending on where the final multiply is at. • Let us consider all possible places for that final multiply: • Recall that Ai is a di × di+1 dimensional matrix. • So, a characterizing equation for Ni,j is the following: • Note that subproblems are not independent--the subproblems overlap. CSC401: Analysis of Algorithms<footer>

  12. A Dynamic Programming Algorithm • Since subproblems overlap, we don’t use recursion. • Instead, we construct optimal subproblems “bottom-up.” • Ni,i’s are easy, so start with them • Then do length 2,3,… subproblems, and so on. • Running time: O(n3) AlgorithmmatrixChain(S): Input:sequence S of n matrices to be multiplied Output:number of operations in an optimal paranethization of S fori 1 to n-1 do Ni,i0 forb 1 to n-1 do fori 0 to n-b-1 do ji+b Ni,j+infinity fork i to j-1 do Ni,jmin{Ni,j , Ni,k +Nk+1,j +di dk+1 dj+1} CSC401: Analysis of Algorithms<footer>

  13. answer N n-1 … 0 1 2 j 0 1 … i n-1 A Dynamic Programming Algorithm • The bottom-up construction fills in the N array by diagonals • Ni,j gets values from pervious entries in i-th row and j-th column • Filling in each entry in the N table takes O(n) time. • Total run time: O(n3) • Getting actual parenthesization can be done by remembering “k” for each N entry CSC401: Analysis of Algorithms<footer>

  14. The General Dynamic Programming Technique • Applies to a problem that at first seems to require a lot of time (possibly exponential), provided we have: • Simple subproblems: the subproblems can be defined in terms of a few variables, such as j, k, l, m, and so on. • Subproblem optimality: the global optimum value can be defined in terms of optimal subproblems • Subproblem overlap: the subproblems are not independent, but instead they overlap (hence, should be constructed bottom-up). CSC401: Analysis of Algorithms<footer>

  15. The 0/1 Knapsack Problem • Given: A set S of n items, with each item i having • bi - a positive benefit • wi - a positive weight • Goal: Choose items with maximum total benefit but with weight at most W. • If we are not allowed to take fractional amounts, then this is the 0/1 knapsack problem. • In this case, we let Tdenote the set of items we take • Objective: maximize • Constraint: CSC401: Analysis of Algorithms<footer>

  16. Example • Given: A set S of n items, with each item i having • bi - a positive benefit • wi - a positive weight • Goal: Choose items with maximum total benefit but with weight at most W. “knapsack” • Solution: • 5 (2 in) • 3 (2 in) • 1 (4 in) Items: 1 2 3 4 5 Weight: 4 in 2 in 2 in 6 in 2 in 9 in Benefit: $20 $3 $6 $25 $80 CSC401: Analysis of Algorithms<footer>

  17. A 0/1 Knapsack Algorithm, First Attempt • Sk: Set of items numbered 1 to k. • Define B[k] = best selection from Sk. • Problem: does not have subproblem optimality: • Consider S={(3,2),(5,4),(8,5),(4,3),10,9)} weight-benefit pairs Best for S4: Best for S5: CSC401: Analysis of Algorithms<footer>

  18. A 0/1 Knapsack Algorithm, Second Attempt • Sk: Set of items numbered 1 to k. • Define B[k,w] = best selection from Sk with weight exactly equal to w • Good news: this does have subproblem optimality: • I.e., best subset of Sk with weight exactly w is either the best subset of Sk-1 w/ weight w or the best subset of Sk-1 w/ weight w-wk plus item k. CSC401: Analysis of Algorithms<footer>

  19. The 0/1 Knapsack Algorithm • Recall definition of B[k,w]: • Since B[k,w] is defined in terms of B[k-1,*], we can reuse the same array • Running time: O(nW). • Not a polynomial-time algorithm if W is large • This is a pseudo-polynomial time algorithm Algorithm01Knapsack(S,W): Input:set S of items w/ benefit biand weight wi; max. weight W Output:benefit of best subset with weight at most W forw 0to W do B[w]0 fork 1 to n do forwWdownto wkdo ifB[w-wk]+bk > B[w]then B[w]B[w-wk]+bk CSC401: Analysis of Algorithms<footer>

  20. Pseudo-Polynomial Time Algorithms • An algorithm is a pseudo-polynomial time algorithm, if the running time of the algorithm depends on the magnitude of a number given in the input, not its encoding size • Example: The 0/1 Knapsack algorithm depends on W • Not a true polynomial-time algorithm • But usually it’s better than the brute-force algorithm CSC401: Analysis of Algorithms<footer>

More Related