Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 14 Dynamic Programming • An algorithm design technique for optimization problems (similar to divide and conquer) • Divide and conquer – Partition the problem into independent subproblems – Solve the subproblems recursively – Combine the solutions to solve the original problem CS 477/677 - Lecture 14 2 Dynamic Programming • Used for optimization problems – Find a solution with the optimal value (minimum or maximum) – A set of choices must be made to get an optimal solution – There may be many solutions that return the optimal value: we want to find one of them CS 477/677 - Lecture 14 3 Assembly Line Scheduling • Automobile factory with two assembly lines – Each line has n stations: S1,1, . . . , S1,n and S2,1, . . . , S2,n – Corresponding stations S1, j and S2, j perform the same function but can take different amounts of time a1, j and a2, j – Times to enter are e1 and e2 and times to exit are x1 and x2 CS 477/677 - Lecture 14 4 Assembly Line • After going through a station, the car can either: – stay on same line at no cost, or – transfer to other line: cost after Si,j is ti,j , i = 1, 2, j = 1, . . . , n-1 CS 477/677 - Lecture 14 5 Assembly Line Scheduling • Problem: What stations should be chosen from line 1 and what from line 2 in order to minimize the total time through the factory for one car? CS 477/677 - Lecture 14 6 One Solution • Brute force – Enumerate all possibilities of selecting stations – Compute how long it takes in each case and choose the best one 1 2 3 4 n 1 0 0 1 1 0 if choosing line 2 at step j (= 3) 1 if choosing line 1 at step j (= n) – There are 2n possible ways to choose stations – Infeasible when n is large CS 477/677 - Lecture 14 7 1. Structure of the Optimal Solution • How do we compute the minimum time of going through the station? CS 477/677 - Lecture 14 8 1. Structure of the Optimal Solution • Let’s consider all possible ways to get from the starting point through station S1,j – We have two choices of how to get to S1, j: • Through S1, j - 1, then directly to S1, j • Through S2, j - 1, then transfer over to S1, j S1,j-1 S1,j a1,j-1 a1,j t2,j-1 a2,j-1 S2,j-1 CS 477/677 - Lecture 14 9 1. Structure of the Optimal Solution • Suppose that the fastest way through S1, j is through S1, j – 1 – We must have taken the fastest way from entry through S1, j – 1 – If there were a faster way through S1, j - 1, we would use it instead • Similarly for S2, j – 1 S1,j-1 S1,j a1,j-1 a1,j t2,j-1 a2,j-1 S2,j-1 CS 477/677 - Lecture 14 10 Optimal Substructure • Generalization: an optimal solution to the problem find the fastest way through S1, j contains within it an optimal solution to subproblems: find the fastest way through S1, j - 1 or S2, j - 1. • This is referred to as the optimal substructure property • We use this property to construct an optimal solution to a problem from optimal solutions to subproblems CS 477/677 - Lecture 14 11 2. A Recursive Solution • Define the value of an optimal solution in terms of the optimal solution to subproblems • Assembly line subproblems – Finding the fastest way through each station j on each line i (i = 1,2, j = 1, 2, …, n) CS 477/677 - Lecture 14 12 2. A Recursive Solution • f* = the fastest time to get through the entire factory • fi[j] = the fastest time to get from the starting point through station Si,j f* = min (f1[n] + x1, f2[n] + x2) CS 477/677 - Lecture 14 13 2. A Recursive Solution • fi[j] = the fastest time to get from the starting point through station Si,j • j = 1 (getting through station 1) f1[1] = e1 + a1,1 f2[1] = e2 + a2,1 CS 477/677 - Lecture 14 14 2. A Recursive Solution • Compute fi[j] for j = 2, 3, …,n, and i = 1, 2 • Fastest way through S1, j is either: – the way through S1, j - 1 then directly through S1, j, or f1[j - 1] + a1,j – the way through S2, j - 1, transfer from line 2 to line 1, then through S1, j f2[j -1] + t2,j-1 + a1,j S1,j-1 S1,j a1,j-1 a1,j t2,j-1 f1[j] = min(f1[j - 1] + a1,j ,f2[j -1] + t2,j-1 + a1,j) a2,j-1 S2,j-1 CS 477/677 - Lecture 14 15 2. A Recursive Solution f1[j] = f2[j] = e1 + a1,1 if j = 1 min(f1[j - 1] + a1,j ,f2[j -1] + t2,j-1 + a1,j) if j ≥ 2 e2 + a2,1 if j = 1 min(f2[j - 1] + a2,j ,f1[j -1] + t1,j-1 + a2,j) if j ≥ 2 CS 477/677 - Lecture 14 16 3. Computing the Optimal Value f* = min (f1[n] + x1, f2[n] + x2) f1[j] = min(f1[j - 1] + a1,j ,f2[j -1] + t2,j-1 + a1,j) 1 2 3 4 5 f1[j] f1(1) f1(2) f1(3) f1(4) f1(5) f2[j] f2(1) f2(2) f2(3) f2(4) f2(5) 4 times 2 times • Solving top-down would result in exponential running time CS 477/677 - Lecture 14 17 3. Computing the Optimal Value • For j ≥ 2, each value fi[j] depends only on the values of f1[j – 1] and f2[j - 1] • Compute the values of fi[j] – in increasing order of j increasing j 1 2 3 4 5 f1[j] f2[j] • Bottom-up approach – First find optimal solutions to subproblems – Find an optimal solution to the problem from the subproblems CS 477/677 - Lecture 14 18 4. Construct the Optimal Solution • We need the information about which line has been used at each station: – li[j] – the line number (1, 2) whose station (j - 1) has been used to get in fastest time through Si,j, j = 2, 3, …, n – l* – the line number (1, 2) whose station n has been used to get in fastest time through the exit point increasing j 2 3 4 5 l1[j] l2[j] CS 477/677 - Lecture 14 19 FASTEST-WAY(a, t, e, x, n) 1. f1[1] ← e1 + a1,1 2. f2[1] ← e2 + a2,1 Compute initial values of f1 and f2 3. for j ← 2 to n 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. do if f1[j - 1] + a1,j ≤ f2[j - 1] + t2, j-1 + a1, j then f1[j] ← f1[j - 1] + a1, j l1[j] ← 1 else f1[j] ← f2[j - 1] + t2, j-1 + a1, j Compute the values of f1[j] and l1[j] l1[j] ← 2 if f2[j - 1] + a2, j ≤ f1[j - 1] + t1, j-1 + a2, j then f2[j] ← f2[j - 1] + a2, j l2[j] ← 2 else f2[j] ← f1[j - 1] + t1, j-1 + a2, j l2[j] ← 1 CS 477/677 - Lecture 14 Compute the values of f2[j] and l2[j] 20 FASTEST-WAY(a, t, e, x, n) (cont.) 14. if f1[n] + x1 ≤ f2[n] + x2 15. 16. 17. 18. then f* = f1[n] + x1 l* = 1 else f* = f2[n] + x2 Compute the values of the fastest time through the entire factory l* = 2 CS 477/677 - Lecture 14 21 Example e1 + a1,1, f1[j] = min(f1[j - 1] + a1,j ,f2[j -1] + t2,j-1 + a1,j) 1 2 3 4 5 f1[j] 9 18[1] 20[2] 24[1] 32[1] f2[j] 12 16[1] 22[2] 25[1] 30[2] CS 477/677 - Lecture 14 if j = 1 if j ≥ 2 f* = 35[1] 22 4. Construct an Optimal Solution Alg.: PRINT-STATIONS(l, n) i ← l* print “line ” i “, station ” n for j ← n downto 2 do i ←li[j] print “line ” i “, station ” j - 1 line 1, station 5 line 1, station 4 line 1, station 3 line 2, station 2 line 1, station 1 1 2 3 4 5 f1[j] l1[j] 9 18[1] 20[2] 24[1] 32[1] f2[j] l2[j] 12 16[1] 22[2] 25[1] 30[2] CS 477/677 - Lecture 14 l* = 1 23 Dynamic Programming Algorithm 1. Characterize the structure of an optimal solution – Fastest time through a station depends on the fastest time on previous stations 2. Recursively define the value of an optimal solution – f1[j] = min(f1[j - 1] + a1,j ,f2[j -1] + t2,j-1 + a1,j) 3. Compute the value of an optimal solution in a bottomup fashion – Fill in the fastest time table in increasing order of j (station #) 4. Construct an optimal solution from computed information – Use an additional table to help reconstruct the optimal solution CS 477/677 - Lecture 14 24 Matrix-Chain Multiplication Problem: given a sequence ⟨A1, A2, …, An⟩ of matrices, compute the product: A1 ∙ A2 ∙∙∙ An • Matrix compatibility: C=A∙B colA = rowB rowC = rowA colC = colB A1 ∙ A2 ∙ Ai ∙ Ai+1 ∙∙∙ An coli = rowi+1 CS 477/677 - Lecture 14 25 Matrix-Chain Multiplication • In what order should we multiply the matrices? A1 ∙ A2 ∙∙∙ An • Matrix multiplication is associative: • E.g.: A1 ∙ A2 ∙ A3 = ((A1 ∙ A2) ∙ A3) = (A1 ∙ (A2 ∙ A3)) • Which one of these orderings should we choose? – The order in which we multiply the matrices has a significant impact on the overall cost of executing the entire chain of multiplications CS 477/677 - Lecture 14 26 MATRIX-MULTIPLY(A, B) if columns[A] ≠ rows[B] then error “incompatible dimensions” else for i ← 1 to rows[A] do for j ← 1 to columns[B] rows[A] ∙ cols[A] ∙ cols[B] multiplications do C[i, j] = 0 for k ← 1 to columns[A] do C[i, j] ←C[i, j] + A[i, k] B[k, j] k j i rows[A] * A j cols[B] cols[B] = i k B CS 477/677 - Lecture 14 C rows[A] 27 Example A1 ∙ A2 ∙ A3 • A1: 10 x 100 • A2: 100 x 5 • A3: 5 x 50 1. ((A1 ∙ A2) ∙ A3): A1 ∙ A2 takes 10 x 100 x 5 = 5,000 (its size is 10 x 5) ((A1 ∙ A2) ∙ A3) takes 10 x 5 x 50 = 2,500 Total: 7,500 scalar multiplications 2. (A1 ∙ (A2 ∙ A3)): A2 ∙ A3 takes 100 x 5 x 50 = 25,000 (its size is 100 x 50) (A1 ∙ (A2 ∙ A3)) takes 10 x 100 x 50 = 50,000 Total: 75,000 scalar multiplications one order of magnitude difference!! CS 477/677 - Lecture 14 28 Matrix-Chain Multiplication • Given a chain of matrices ⟨A1, A2, …, An⟩, where for i = 1, 2, …, n matrix Ai has dimensions pi-1x pi, fully parenthesize the product A1 ∙A2 ∙∙∙An in a way that minimizes the number of scalar multiplications. A1 ∙ A2 p0 x p1 p1 x p2 ∙∙∙ Ai ∙ Ai+1 pi-1 x pi ∙∙∙ An pi x pi+1 CS 477/677 - Lecture 14 pn-1 x pn 29 1. The Structure of an Optimal Parenthesization • Notation: Ai…j = Ai Ai+1 ∙∙∙Aj, i ≤ j • For i < j: Ai…j = Ai Ai+1 ∙∙∙ Aj = Ai Ai+1 ∙∙∙ Ak Ak+1 ∙∙∙ Aj = Ai…k Ak+1…j • Suppose that an optimal parenthesization of Ai…j splits the product between Ak and Ak+1, where i≤k<j CS 477/677 - Lecture 14 30 Optimal Substructure Ai…j = Ai…k Ak+1…j • The parenthesization of the “prefix” Ai…k must be an optimal parentesization • If there were a less costly way to parenthesize Ai…k, we could substitute that one in the parenthesization of Ai…j and produce a parenthesization with a lower cost than the optimum ⇒ contradiction! • An optimal solution to an instance of the matrix-chain multiplication contains within it optimal solutions to subproblems CS 477/677 - Lecture 14 31 2. A Recursive Solution • Subproblem: determine the minimum cost of parenthesizing Ai…j = Ai Ai+1 ∙∙∙Aj for 1 ≤ i ≤ j ≤ n • Let m[i, j] = the minimum number of multiplications needed to compute Ai…j – Full problem (A1..n): m[1, n] – i = j: Ai…i = Ai ⇒ m[i, i] = 0, for i = 1, 2, …, n CS 477/677 - Lecture 14 32 2. A Recursive Solution Consider the subproblem of parenthesizing Ai…j = Ai Ai+1 ∙∙∙Aj = Ai…k Ak+1…j m[i, k] for 1 ≤ i ≤ j ≤ n pi-1pkpj for i ≤ k < j m[k+1,j] • Assume that the optimal parenthesization splits the product Ai Ai+1 ∙∙∙ Aj at k (i ≤ k < j) m[i, j] = m[i, k] min # of multiplications to compute Ai…k + m[k+1, j] + pi-1pkpj min # of multiplications # of multiplications to compute Ak+1…j to compute Ai…kAk…j CS 477/677 - Lecture 14 33 Readings • Chapters 14, 15 CS 477/677 - Lecture 14 34
© Copyright 2026 Paperzz