250 likes | 354 Views
Dynamic Programming. Dynamic Programming. Like divide-and-conquer algorithms: Combines solutions to subproblems Dynamic programming is useful when subproblems share subproblems Not necessary to recompute Save the answer in a table
E N D
Dynamic Programming • Like divide-and-conquer algorithms: • Combines solutions to subproblems • Dynamic programming is useful when subproblems share subproblems • Not necessary to recompute • Save the answer in a table • Dynamic programming applies to optimization problems (finding min or max)
Developement of DP • Characterize the structure of an optimal solution • Recursively define the value of an optimal solution • Compute the value of an optimal solution (bottom-up) • Construct an optimal solution from computed information (may be omitted)
The Assembly Line • Imagine two assembly lines, with n stations • The ith station performs the same thing, but in different times (denoted ai,j) • There is a time to transfer from one line to another costs ti,j • The problem: determine the fastest way through the factory • How many possibilities are there?
Example: Assembly Line Station S1,1 Station S1,2 Station S1,3 Station S1,n-1 Station S1,n a1,1 a1,2 a1,3 a1,n-1 a1,n e1 x1 t1,1 t1,2 t1,n-1 chassis enters completed auto exits t2,1 t2,2 t2,n-1 e2 x2 a2,1 a2,2 a3,3 a2,n-1 a2,n Station S2,1 Station S2,2 Station S2,3 Station S2,n-1 Station S2,n
Step 1: the structure • Because there are (2n) possibilities, this won’t work. • Consider the fastest possible way through station S1, j • If j = 1, then there is only one way • If j > 1, then there are two choices: • came from station S1, j -1 • came from station S2, j -1, incurring cost t2, j -1
Key Observation • The chassis must have taken the fastest way from the start to station S1, j -1 • Why? If there were a faster way, we could substitute it to yield a faster way: contradiction! • In other words, for it to be the fastest way, the subproblem had to be optimal. • Optimal substructure: a hallmark of dynamic programming
Thus... • The fastest way is either • the fastest way through station S1,j-1 and directly to S1, j • the fastest way through station S2,j-1 with a transfer, and then through S1, j • To find the fastest way through station j, we solve the subproblems
Step 2: A recursive solution • Let fi[j] denote the fastest time from the start to station Si,j • Fastest way through the factory f* is therefore min(f1[n]+x1, f2[n]+x2) • f1[j] = min(f1[j-1]+a1,j, f2[j-1]+t2,j-1+a1,j) • f2[j] = min(f2[j-1]+a2,j, f1[j-1]+t1,j-1+a2,j)
Step 3: Computing the fastest time • Top down yields a 2n solution. • As a note f1[1] is calculated 2n-1 times! • By computing the fi[j] values in increasing order j, time is reduced to (n)
Station S1,1 Station S1,2 Station S1,3 Station S1,4 Station S1,5 Station S1,6 7 9 3 8 4 4 2 3 2 3 4 3 1 chassis enters completed auto exits 2 1 1 2 2 4 2 8 5 6 5 7 4 Station S2,1 Station S2,2 Station S2,3 Station S2,4 Station S2,5 Station S2,6 f1[ j ] 9 35 18 20 24 32 f * = 38 f2[ j ] 12 37 16 22 25 30
Matrix-Chain Multiplication • Given a sequence of matrices, find the best way to parenthesize them. • For example: • (A1(A2(A3A4))) • (A1((A2A3)A4)) • ((A1A2)(A3A4)) • (A1(A2A3)A4) • (((A1A2)A3)A4) • There are actually (2n) different parenthesizations!
Why This is Important • The way that we parenthesize them dramatically affects the amount of work to do: • For Example: • A1=10100, A2=1005, A3=550 • ((A1A2)A3) • 101005 = 5000 (for A1A2) leaving a 105 plus • 10550 = 2500 = 7500 total ops • (A1(A2A3)) • 100550 = 25,000 (for A2A3) leaving a 10050 plus • 1010050 = 50,000 = 75,000 total ops
Notation • Let Ai..j denote the matrix results for AiAi+1...Aj • Must pick a split point i<k<j • for some k, we first compute Ai..k and Ak+1...j, and then multiply them together • How do you pick k? • Note the optimal substructure: • Ai..k must be optimal • If there was a less-costly way, we could cut-n-paste!
Building from the Ground Up 1 6 2 5 3 4 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 6 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4
Building from the Ground Up 1 6 2 5 3 4 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 6 15,750 2,625 750 1,000 5,000 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4
Building from the Ground Up 1 6 2 5 3 4 3 4 ? 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4 Note: Can be either (A1(A2A3)) or ((A1A2)A3) 15,750 + (30155) = 18,000 vs2,625 + (30355) = 7,875
Building from the Ground Up 1 6 2 5 3 4 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4
One Last Example 1 6 2 5 3 4 ? 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4
One Last Example 1 6 2 5 3 4 ? 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4 0 + 2,500 + (351520) vs
One Last Example 1 6 2 5 3 4 ? 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4 0 + 2,500 + (351520) vs 2,625 + 1000 + (35520) vs
One Last Example 1 6 2 5 3 4 ? 3 4 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4 0 + 2,500 + (351520) vs 2,625 + 1000 + (35520) vs 4,375 + 0 + (351020)
One Last Example 1 6 2 5 3 4 3 4 7,125 35x20 5 matrix Dimension A1 3035 A2 3515 A3 155 A4 510 A5 1020 A6 2025 2 4,375 35x10 2,500 15x20 3,500 5x25 7,875 30x5 6 15,750 30x15 2,625 35x5 75015x10 1,000 5x20 5,000 10x25 1 c 0 0 0 0 0 0 A1 A2 A3 A5 A6 A4 0 + 2,500 + (351520) vs 2,625 + 1000 + (35520) vs 4,375 + 0 + (351020) =7,125
Brain Teaser • Using this tabular method, can you: • create a dynamic merge-sort? • create a dynamic twelve days of Christmas? • *find the total number of ways to make change for n cents?
In Summary • Dynamic Programming exhibits optimal substructure • Overlapping Subproblems • Tables for holding intermediate results