computing binomial coeffecients 1
play

Computing binomial coeffecients, 1 if k = 0 or k = n ; 1, binom ( - PowerPoint PPT Presentation

Computing binomial coeffecients, 1 if k = 0 or k = n ; 1, binom ( n , k ) = Dynamic Programming binom ( n 1, k 1 ) + binom ( n 1, k ) , otherwise. DPV Chapter 6, Part 1 binom(5,3) Jim Royer binom(4,2) binom(4,3) binom(3,1)


  1. Computing binomial coeffecients, 1 � if k = 0 or k = n ; 1, binom ( n , k ) = Dynamic Programming binom ( n − 1, k − 1 ) + binom ( n − 1, k ) , otherwise. DPV Chapter 6, Part 1 binom(5,3) Jim Royer binom(4,2) binom(4,3) binom(3,1) binom(3,2) binom(3,2) binom(3,3) March 6, 2019 binom(2,0) binom(2,1) binom(2,1) binom(2,2) binom(2,1) binom(2,2) binom(1,0) binom(1,1) binom(1,0) binom(1,1) binom(1,0) binom(1,1) Dynamic Programming 1 / 19 Dynamic Programming 2 / 19 Computing binomial coeffecients, 2 Computing binomial coeffecients, 3 Before Memoization � function binom( n , k ) if k = 0 or k = n ; 1, binom ( n , k ) = if k = 0 or k = n then return 1 binom ( n − 1, k − 1 ) + binom ( n − 1, k ) , otherwise. else return binom( n − 1, k − 1 ) + binom( n − 1, k ) After Memoization After Memoization (Continued) function binom( n , k ) binom(5,3) function helper( m , ℓ ) for m ← 0, 1, . . . , n do if b [ m , ℓ ] = 0 then b [ m , 0 ] ← 1 ; b [ m , m ] ← 1 b [ m , ℓ ] ← helper( m − 1, ℓ − 1 ) binom(4,2) binom(4,3) for m ← 2, 3, . . . , n do +helper( m − 1, ℓ ) for ℓ ← 1, 2, . . . , m − 1 do return b [ m , ℓ ] b [ m , ℓ ] ← 0 binom(3,1) binom(3,2) binom(3,3) return helper( n , k ) Trace binom (5,3) binom(2,0) binom(2,1) binom(2,2) binom(1,0) binom(1,1) Dynamic Programming 3 / 19 Dynamic Programming 4 / 19

  2. Computing binomial coeffecients, 4 Computing binomial coeffecients, 5 Building the Table Directly function binom( n , k ) for m ← 0, 1, . . . , n do Going from a recursion to a table-building computation. b [ m , 0 ] ← 1 ; b [ m , m ] ← 1 for m = 2, 3, . . . , n do Step 1. Give a recursive definition. for ℓ = 1, 2, . . . , m − 1 do (For many problems, this is the hard part.) b [ m , ℓ ] ← b [ m − 1, ℓ − 1 ] + b [ m − 1, ℓ ] return b [ n , k ] Step 2. Memoize to exploit repeated subproblems. (If there are few repeated subproblems, Trace binom (5,3) then memoization will not help.) Step 3. Build the table directly to cut down overhead. (If the answer depends on a small part of the table, then the recursion can be faster.) Dynamic Programming 5 / 19 Dynamic Programming 6 / 19 Making Change—Again, 1 Making Change—Again, 2 The Making Change Problem (MCP) The Making Change Problem (MCP) Given: coin denominations d 1 < d 2 < · · · < d k and an amount a . Given: coin denominations d 1 < d 2 < · · · < d k and an amount a . Find: the smallest collection of coins that is worth amount a . Find: the smallest collection of coins that is worth amount a . mcn ( a ) ≡ the number of coins in an optimal solution to MCP for a Example � if a = 0; 0, mcn ( a ) = 1 + min { mcn ( a − d i ) d i ≤ a & 1 ≤ i ≤ k } , if a > 0. ◮ d 1 = 1, d 2 = 4, d 3 = 6; a = 8. ◮ The optimal choice is { 4, 4 } . mcn ( 0 ) = 0 = 0 Example ◮ The greedy algorithm produces { 6, 1, 1 } . mcn ( 1 ) = 1 + min { mcn ( 0 ) } = 1 d 1 = 1 mcn ( 2 ) = 1 + min { mcn ( 1 ) } = 2 The Optimal Substructure of MCP mcn ( 3 ) = 1 + min { mcn ( 2 ) } = 3 d 2 = 4 mcn ( 4 ) = 1 + min { mcn ( 3 ) , mcn ( 0 ) } = 1 If: an optimal solution of the MCP for a uses a d i -coin, d 3 = 6 mcn ( 5 ) = 1 + min { mcn ( 4 ) , mcn ( 1 ) } = 2 then: the rest of the coins give an optimal solution of the MCP for a − d i . mcn ( 6 ) = 1 + min { mcn ( 5 ) , mcn ( 2 ) , mcn ( 0 ) } = 1 a = 8 (Why?) mcn ( 7 ) = 1 + min { mcn ( 6 ) , mcn ( 3 ) , mcn ( 1 ) } = 2 mcn ( 8 ) = 1 + min { mcn ( 7 ) , mcn ( 4 ) , mcn ( 2 ) } = 2 Dynamic Programming 7 / 19 Dynamic Programming 8 / 19

  3. Making Change—Again, 3 Making Change—Again, 4 The Making Change Problem (MCP) � the number of coins in an optimal solution to MCP for a mcn ′ ( i , a ) ≡ Given: coin denominations d 1 < d 2 < · · · < d k and an amount a . using denominations d 1 , . . . , d i Find: the smallest collection of coins that is worth amount a .  0, if a = 0;   �  0, if a = 0; + ∞ , if i = 0 and a > 0;   mcn ( a ) =  mcn ′ ( i , a ) = mcn ′ ( i − 1, a ) , 1 + min { mcn ( a − d i ) d i ≤ a & 1 ≤ i ≤ k } , if a > 0. if 0 < a < d i ;  � �   mcn ′ ( i − 1, a ) , 1 + mcn ′ ( i , a − d i )  min , otherwise   function mcn( d 1 , . . . , d k ; a ) integer array num [ 0.. a ] function mcn’( d 1 , . . . , d k ; a ) num [ 0 ] ← 0 // Goal: num [ i , a ′ ] = mcn ( d 1 , . . . , d i ; a ′ ) for a ′ ← 1 to a do integer array num [ 0.. k , 0.. a ] // Trace mcn ( 1, 4, 6; 8 ) for a ′ ← 1 to a do num [ 0, a ′ ] ← 0 num [ a ′ ] ← ∞ // Trace mcn ′ ( 1, 4, 6; 8 ) for i ← 1 to k do for i ← 1 to k do if d i ≤ a ′ then num [ a ′ ] ← min ( num [ a ′ ] , 1 + num [ a ′ − d i ]) num [ i , 0 ] ← 0 for a ′ ← 1 to k do return num [ a ] if d i > a ′ then num [ i , a ′ ] ← num [ i − 1, a ′ ] else num [ i , a ′ ] ← min ( num [ i − 1, a ′ ] , 1 + num [ i , a ′ − d i ]) return num [ k , a ] Dynamic Programming 9 / 19 Dynamic Programming 10 / 19 Making Change—Again, 5 Longest Increasing Subsequences, 1 Reconstructing the Solution to the MCP � the min number of coins of denominations d 1 , . . . , d i need Definition Given: num [ i , a ′ ] = to make change for amount a ′ where 0 ≤ i ≤ k and 0 ≤ Suppose S = a 1 , . . . , a n is a sequence of numbers. a ′ ≤ a (a) A subsequence of S is a sequence of numbers a i 1 , a i 2 , . . . , a i k such that Find: What coins make up the optimal solution. 1 ≤ i 1 < i 2 < · · · < i k ≤ n . (b) Such a subsequence is increasing when a i 1 < a i 2 < · · · < a i k . function reconstruct( d 1 , . . . , d k ; a , num [ 0.. k , 0.. a ]) coins ← the empty list a ′ ← a ; i ← k Longest Increasing Subsequence Problem (LISP) while a ′ > 0 do if ( d i ≤ a ′ & num [ i , a ′ ] � = num [ i − 1, a ′ ] ) Given: A sequence of numbers. then Add i to the coins list; a ′ ← a ′ − d i Find: An increasing subsequence of maximal length. else i ← i − 1 Example return coins For S = 5, 2, 8, 6, 3, 6, 9, 7; a longest increasing subsequence is: 2, 3, 6, 9. Dynamic Programming 11 / 19 Dynamic Programming 12 / 19

  4. Longest Increasing Subsequences, 2 Longest Increasing Subsequences, 3 Longest Increasing Subsequence Problem (LISP) Longest Increasing Subsequence Problem (LISP) Given: A sequence of numbers. Find: A max-length increasing subsequence. Given: A sequence of numbers. Find: A max-length increasing subsequence. Given S = a 1 , . . . , a n , we can turn this into a graph problem as follows: G = ( V , E ) , where V = { 1, . . . , n } , E = { ( i , j ) i < j & a i < a j } . Let V = { 1, . . . , n } , E = { ( i , j ) i < j & a i < a j } , and G = ( V , E ) . G is a dag. (Why?) L ( j ) = the length of a longest increasing subseq. ending at j ∴ a longest increasing sequence in S ≡ a longest path in G . = 1 + max { L ( i ) ( i , j ) ∈ E } . Example (for S = 5, 2, 8, 6, 3, 6, 9, 7) Convention: max ( ∅ ) = 0. (So, for example, L ( 1 ) = 1 .) Example (for S = 5, 2, 8, 6, 3, 6, 9, 7) So the length of a LCS is 4 and the LCSs are: i 1 2 3 4 5 6 7 8 5 2 8 6 3 6 9 7 ◮ a 2 , a 5 , a 6 , a 8 = 2, 3, 6, 7 a i 5 2 8 6 3 6 9 7 L ( i ) 1 1 2 2 2 3 4 4 Note: prev [ 8 ] = 6, prev [ 6 ] = 5, prev [ 5 ] = 2, prev [ 2 ] = 0 prev 0 0 1 1 2 5 6 6 ◮ a 2 , a 5 , a 6 , a 7 = 2, 3, 6, 9 Image from DPV Dynamic Programming 13 / 19 Dynamic Programming 14 / 19 Optimal Substructure Longest Common Subsequences, 1 Problem: Longest common subsequence A problem has optimal substructure when an optimal solution is made up of optimal Given: Strings s [ 1.. m ] and t [ 1.. n ] . solutions to its subproblems. Find: The longest subsequence common to s and t . Example Examples ✓✏ ✓✏ ✓✏ ✓✏ ✓✏ ✓✏ (a) Shortest paths in a graph. s = a a z c b d (b) Making change. ✒✑ ✒✑ ✒✑ ✒✑ ✒✑ ✒✑ ❅ ❅ ❆ ❆ (c) ... ❅ ❅ ❆ ❆ ❆ ❅ ❅ ❆ ❆ ❅ ❅ ❆ Non-examples ❆ ❆ ❅ ❅ ❆ ❆ ❅ ❅ (a) Longest paths in a graph. ✓✏ ❆ ✓✏ ✓✏ ✓✏ ✓✏ ✓✏ ❆ ❅ ❅ (b) 3SAT t = a c a b b d ✒✑ ✒✑ ✒✑ ✒✑ ✒✑ ✒✑ (c) ... Credit: A. Blum, http://www.cs.cmu.edu/~avrim/451f09/lectures/lect1001.pdf Dynamic Programming 15 / 19 Dynamic Programming 16 / 19

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend