introduction
play

Introduction CSCE423/823 CSCE423/823 Computer Science & - PDF document

Introduction CSCE423/823 CSCE423/823 Computer Science & Engineering 423/823 Dynamic programming is a technique for solving optimization Introduction Design and Analysis of Algorithms Introduction problems Rod Cutting Rod Cutting Key


  1. Introduction CSCE423/823 CSCE423/823 Computer Science & Engineering 423/823 Dynamic programming is a technique for solving optimization Introduction Design and Analysis of Algorithms Introduction problems Rod Cutting Rod Cutting Key element: Decompose a problem into subproblems , solve them Matrix-Chain Matrix-Chain Lecture 09 — Dynamic Programming (Chapter 15) Multiplication Multiplication recursively, and then combine the solutions into a final (optimal) Longest Longest Common Common solution Subsequence Subsequence Important component: There are typically an exponential number of Optimal Optimal Stephen Scott Binary Search Binary Search subproblems to solve, but many of them overlap Trees Trees (Adapted from Vinodchandran N. Variyam) ) Can re-use the solutions rather than re-solving them Number of distinct subproblems is polynomial 1 / 42 2 / 42 Rod Cutting Example: Rod Cutting (2) CSCE423/823 CSCE423/823 i 1 2 3 4 5 6 7 8 9 10 A company has a rod of length n and wants to cut it into smaller p i 1 5 8 9 10 17 17 20 24 30 Introduction Introduction rods to maximize profit Rod Cutting Rod Cutting Recursive Recursive Have a table telling how much they get for rods of various lengths: A Algorithm Algorithm Dynamic Dynamic Programming rod of length i has price p i Programming Algorithm Algorithm Reconstructing a Reconstructing a Solution The cuts themselves are free, so profit is based solely on the prices Solution Matrix-Chain Matrix-Chain charged for of the rods Multiplication Multiplication Longest If cuts only occur at integral boundaries 1 , 2 , . . . , n � 1 , then can Longest Common Common Subsequence make or not make a cut at each of n � 1 positions, so total number Subsequence of possible solutions is 2 n � 1 Optimal Optimal Binary Search Binary Search Trees Trees 3 / 42 4 / 42 Example: Rod Cutting (3) Recursive Cut-Rod( p, n ) CSCE423/823 CSCE423/823 Given a rod of length n , want to find a set of cuts into lengths i 1 , . . . , i k (where i 1 + · · · + i k = n ) and r n = p i 1 + · · · + p i k is maximized if n == 0 then Introduction Introduction For a specific value of n , can either make no cuts (revenue = p n ) or Rod Cutting Rod Cutting return 0 1 Recursive Recursive make a cut at some position i , then optimally solve the problem for Algorithm Algorithm 2 q = �1 Dynamic Dynamic lengths i and n � i : Programming Programming Algorithm Algorithm 3 for i = 1 to n do Reconstructing a Reconstructing a Solution Solution r n = max ( p n , r 1 + r n � 1 , r 2 + r n � 2 , . . . , r i + r n � i , . . . , r n � 1 + r 1 ) q = max ( q, p [ i ] + Cut-Rod ( p, n � i )) 4 Matrix-Chain Matrix-Chain Multiplication Notice that this problem has the optimal substructure property , in Multiplication 5 end Longest that an optimal solution is made up of optimal solutions to Longest 6 return q Common Common subproblems Subsequence Subsequence Can find optimal solution if we consider all possible subproblems Optimal Optimal Binary Search Binary Search Alternative formulation: Don’t further cut the first segment: Trees Trees What is the time complexity? r n = max 1  i  n ( p i + r n � i ) 5 / 42 6 / 42

  2. Time Complexity Time Complexity (2) CSCE423/823 CSCE423/823 Let T ( n ) be number of calls to Cut-Rod Recursion Tree for n = 4 Thus T (0) = 1 and, based on the for loop, Introduction Introduction n � 1 Rod Cutting Rod Cutting X T ( j ) = 2 n Recursive T ( n ) = 1 + Recursive Algorithm Algorithm Dynamic Dynamic Programming j =0 Programming Algorithm Algorithm Reconstructing a Reconstructing a Solution Solution Why exponential? Cut-Rod exploits the optimal substructure Matrix-Chain Matrix-Chain property, but repeats work on these subproblems Multiplication Multiplication Longest E.g. if the first call is for n = 4 , then there will be: Longest Common Common Subsequence 1 call to Cut-Rod (4) Subsequence Optimal 1 call to Cut-Rod (3) Optimal Binary Search Binary Search 2 calls to Cut-Rod (2) Trees Trees 4 calls to Cut-Rod (1) 8 calls to Cut-Rod (0) 7 / 42 8 / 42 Dynamic Programming Algorithm Memoized-Cut-Rod-Aux( p, n, r ) CSCE423/823 CSCE423/823 if r [ n ] � 0 then Introduction Introduction Can save time dramatically by remembering results from prior calls return r [ n ] // r initialized to all �1 1 Rod Cutting Rod Cutting if n == 0 then 2 Two general approaches: Recursive Recursive Algorithm Algorithm q = 0 3 Dynamic Top-down with memoization: Run the recursive algorithm as Dynamic 1 Programming Programming else 4 Algorithm Algorithm defined earlier, but before recursive call, check to see if the calculation q = �1 Reconstructing a Reconstructing a 5 Solution Solution has already been done and memoized for i = 1 to n do 6 Matrix-Chain Matrix-Chain q = Bottom-up : Fill in results for “small” subproblems first, then use 7 Multiplication 2 Multiplication max ( q, p [ i ] + Memoized-Cut-Rod-Aux ( p, n � i, r )) these to fill in table for “larger” ones Longest Longest end 8 Common Common Subsequence Typically have the same asymptotic running time Subsequence r [ n ] = q 9 Optimal Optimal return q 10 Binary Search Binary Search Trees Trees 9 / 42 10 / 42 Bottom-Up-Cut-Rod( p, n ) Time Complexity CSCE423/823 CSCE423/823 Subproblem graph for n = 4 Allocate r [0 . . . n ] Introduction Introduction r [0] = 0 1 Rod Cutting for j = 1 to n do Rod Cutting 2 Recursive q = �1 Recursive 3 Algorithm Algorithm Dynamic Dynamic for i = 1 to j do 4 Programming Programming Algorithm q = max ( q, p [ i ] + r [ j � i ]) Algorithm 5 Reconstructing a Reconstructing a Solution end Solution 6 Matrix-Chain r [ j ] = q Matrix-Chain 7 Multiplication Multiplication end 8 Longest Longest return r [ n ] Common 9 Common Subsequence Subsequence Optimal Optimal Binary Search Binary Search Trees Trees First solves for n = 0 , then for n = 1 in terms of r [0] , then for n = 2 in Both algorithms take linear time to solve for each value of n , so total terms of r [0] and r [1] , etc. time complexity is Θ ( n 2 ) 11 / 42 12 / 42

Recommend


More recommend