Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these.

Similar presentations


Presentation on theme: "COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these."— Presentation transcript:

1 COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these slides are taken from Monica Nicolescu, Univ. of Nevada, Reno, monica@cs.unr.edu

2 6/08/2004 Lecture 6COSC3101A2 Dynamic Programming An algorithm design technique (like divide and conquer) Divide and conquer –Partition the problem into independent subproblems –Solve the subproblems recursively –Combine the solutions to solve the original problem

3 6/08/2004 Lecture 6COSC3101A3 Dynamic Programming Applicable when subproblems are not independent –Subproblems share subsubproblems –A divide and conquer approach would repeatedly solve the common subproblems –Dynamic programming solves every subproblem just once and stores the answer in a table Used for optimization problems –A set of choices must be made to get an optimal solution –Find a solution with the optimal value (minimum or maximum) –There may be many solutions that return the optimal value: an optimal solution

4 6/08/2004 Lecture 6COSC3101A4 Dynamic Programming Algorithm 1.Characterize the structure of an optimal solution 2.Recursively define the value of an optimal solution 3.Compute the value of an optimal solution in a bottom-up fashion 4.Construct an optimal solution from computed information

5 6/08/2004 Lecture 6COSC3101A5 Assembly Line Scheduling Automobile factory with two assembly lines –Each line has n stations: S 1,1,..., S 1,n and S 2,1,..., S 2,n –Corresponding stations S 1, j and S 2, j perform the same function but can take different amounts of time a 1, j and a 2, j –Entry times e 1 and e 2 and exit times x 1 and x 2

6 6/08/2004 Lecture 6COSC3101A6 Assembly Line After going through a station, can either: –stay on same line at no cost, or –transfer to other line: cost after S i,j is t i,j, j = 1,..., n - 1

7 6/08/2004 Lecture 6COSC3101A7 Assembly Line Scheduling Problem: what stations should be chosen from line 1 and which from line 2 in order to minimize the total time through the factory for one car?

8 6/08/2004 Lecture 6COSC3101A8 One Solution Brute force –Enumerate all possibilities of selecting stations –Compute how long it takes in each case and choose the best one Problem: –There are 2 n possible ways to choose stations –Infeasible when n is large 10011 1 if choosing line 1 at step j (= n) 1234n 0 if choosing line 2 at step j (= 3)

9 6/08/2004 Lecture 6COSC3101A9 1. Structure of the Optimal Solution Let’s consider the fastest way possible to get from the starting point through station S 1,j –If j = 1 : determine how long it takes to get through S 1,1 –If j ≥ 2, have two choices of how to get to S 1, j : Through S 1, j - 1, then directly to S 1, j Through S 2, j - 1, then transfer over to S 1, j

10 6/08/2004 Lecture 6COSC3101A10 1. Structure of the Optimal Solution Suppose that the fastest way through S 1, j is through S 1, j – 1 –We must have taken a fastest way from entry through S 1, j – 1 –If there were a faster way through S 1, j - 1, we would use it instead Similarly for S 2, j – 1

11 6/08/2004 Lecture 6COSC3101A11 Optimal Substructure Generalization: an optimal solution to the problem find the fastest way through S 1, j contains within it an optimal solution to subproblems: find the fastest way through S 1, j - 1 or S 2, 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

12 6/08/2004 Lecture 6COSC3101A12 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 station j on both lines, j = 1, 2, …, n

13 6/08/2004 Lecture 6COSC3101A13 2. A Recursive Solution (cont.) f i [j] = the fastest time to get from the starting point through station S i,j j = 1 ( getting through station 1 ) f 1 [1] = e 1 + a 1,1 f 2 [1] = e 2 + a 2,1

14 6/08/2004 Lecture 6COSC3101A14 2. A Recursive Solution (cont.) Compute f i [j] for j = 2, 3, …,n, and i = 1, 2 Fastest way through S 1, j is either: –fastest way through S 1, j - 1 then directly through S 1, j, or f 1 [j] = f 1 [j - 1] + a 1,j –fastest way through S 2, j - 1, transfer from line 2 to line 1, then through S 1, j f 1 [j] = f 2 [j -1] + t 2,j-1 + a 1,j f 1 [j] = min(f 1 [j - 1] + a 1,j,f 2 [j -1] + t 2,j-1 + a 1,j ) a 1,j a 1,j-1 a 2,j-1 t 2,j-1 S 1,j S 1,j-1 S 2,j-1

15 6/08/2004 Lecture 6COSC3101A15 2. A Recursive Solution (cont.) f* = the fastest time to get through the entire factory f* = min (f 1 [n] + x 1, f 2 [n] + x 2 )

16 6/08/2004 Lecture 6COSC3101A16 2. A Recursive Solution (cont.) e 1 + a 1,1 if j = 1 f 1 [j] = min(f 1 [j - 1] + a 1,j,f 2 [j -1] + t 2,j-1 + a 1,j ) if j ≥ 2 e 2 + a 2,1 if j = 1 f 2 [j] = min(f 2 [j - 1] + a 2,j,f 1 [j -1] + t 1,j-1 + a 2,j ) if j ≥ 2

17 6/08/2004 Lecture 6COSC3101A17 3. Computing the Optimal Solution f* = min (f 1 [n] + x 1, f 2 [n] + x 2 ) f 1 [j] = min(f 2 [j - 1] + a 2,j,f 1 [j -1] + t 1,j-1 + a 2,j ) Solving top-down would result in exponential running time f 1 [j] f 2 [j] 12345 f 1 (5) f 2 (5) f 1 (4) f 2 (4) f 1 (3) f 2 (3) 2 times 4 times f 1 (2) f 2 (2) f 1 (1) f 2 (1)

18 6/08/2004 Lecture 6COSC3101A18 3. Computing the Optimal Solution For j ≥ 2, each value f i [j] depends only on the values of f 1 [j – 1] and f 2 [j - 1] Compute the values of f i [j] –in increasing order of j Bottom-up approach –First find optimal solutions to subproblems –Find an optimal solution to the problem from the subproblems f 1 [j] f 2 [j] 12345 increasing j

19 6/08/2004 Lecture 6COSC3101A19 Additional Information To construct the optimal solution we need the sequence of what line has been used at each station: –l i [j] – the line number (1, 2) whose station ( j - 1 ) has been used to get in fastest time through S i,j, j = 2, 3, …, n –l* - the line whose station n is used to get in the fastest way through the entire factory l 1 [j] l 2 [j] 2345 increasing j

20 6/08/2004 Lecture 6COSC3101A20 Example e 1 + a 1,1, if j = 1 f 1 [j] = min(f 1 [j - 1] + a 1,j,f 2 [j -1] + t 2,j-1 + a 1,j ) if j ≥ 2 f* = 35 [1] f 1 [j] f 2 [j] 12345 9 1216 [1] 18 [1] 20 [2] 22 [2] 24 [1] 25 [1] 32 [1] 30 [2]

21 6/08/2004 Lecture 6COSC3101A21 FASTEST-WAY( a, t, e, x, n ) 1. f 1 [1] ← e 1 + a 1,1 2. f 2 [1] ← e 2 + a 2,1 3. for j ← 2 to n 4. do if f 1 [j - 1] + a 1,j ≤ f 2 [j - 1] + t 2, j-1 + a 1, j 5. then f 1 [j] ← f 1 [j - 1] + a 1, j 6. l 1 [j] ← 1 7. else f 1 [j] ← f 2 [j - 1] + t 2, j-1 + a 1, j 8. l 1 [j] ← 2 9. if f 2 [j - 1] + a 2, j ≤ f 1 [j - 1] + t 1, j-1 + a 2, j 10. then f 2 [j] ← f 2 [j - 1] + a 2, j 11. l 2 [j] ← 2 12. else f 2 [j] ← f 1 [j - 1] + t 1, j-1 + a 2, j 13. l 2 [j] ← 1 Compute initial values of f 1 and f 2 Compute the values of f 1 [j] and l 1 [j] Compute the values of f 2 [j] and l 2 [j]

22 6/08/2004 Lecture 6COSC3101A22 FASTEST-WAY( a, t, e, x, n ) (cont.) 14.if f 1 [n] + x 1 ≤ f 2 [n] + x 2 15. then f* = f 1 [n] + x 1 16. l* = 1 17. else f* = f 2 [n] + x 2 18. l* = 2 Compute the values of the fastest time through the entire factory

23 6/08/2004 Lecture 6COSC3101A23 4. Construct an Optimal Solution Alg.: PRINT-STATIONS( l, n ) i ← l* print “line ” i “, station ” n for j ← n downto 2 do i ← l i [j] print “line ” i “, station ” j - 1 f 1 [j]/l 1 [j] f 2 [j]/l 2 [j] 12345 9 1216 [1] 18 [1] 20 [2] 22 [2] 24 [1] 25 [1] 32 [1] 30 [2] l* = 1 line 1, station 5 line 1, station 4 line 1, station 3 line 2, station 2 line 1, station 1

24 6/08/2004 Lecture 6COSC3101A24 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 –f 1 [j] = min(f 1 [j - 1] + a 1,j,f 2 [j -1] + t 2,j-1 + a 1,j ) 3.Compute the value of an optimal solution in a bottom-up 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

25 6/08/2004 Lecture 6COSC3101A25 Matrix-Chain Multiplication Problem: given a sequence  A 1, A 2, …, A n , compute the product: A 1  A 2  A n Matrix compatibility: C = A  B col A = row B row C = row A col C = col B A 1  A 2  A i  A i+1  A n col i = row i+1

26 6/08/2004 Lecture 6COSC3101A26 Matrix-Chain Multiplication In what order should we multiply the matrices? A 1  A 2  A n Parenthesize the product to get the order in which matrices are multiplied E.g.: A 1  A 2  A 3 = ((A 1  A 2 )  A 3 ) = (A 1  (A 2  A 3 )) Which one of these orderings should we choose? –The order in which we multiply the matrices has a significant impact on the cost of evaluating the product

27 6/08/2004 Lecture 6COSC3101A27 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] do C[i, j] = 0 for k  1 to columns[A] do C[i, j]  C[i, j] + A[i, k] B[k, j] rows[A] cols[B] i j j i A B C * = k k rows[A]  cols[A]  cols[B] multiplications

28 6/08/2004 Lecture 6COSC3101A28 Example A 1  A 2  A 3 A 1 : 10 x 100 A 2 : 100 x 5 A 3 : 5 x 50 1. ((A 1  A 2 )  A 3 ): A 1  A 2 = 10 x 100 x 5 = 5,000 (10 x 5) ((A 1  A 2 )  A 3 ) = 10 x 5 x 50 = 2,500 Total: 7,500 scalar multiplications 2. (A 1  (A 2  A 3 )): A 2  A 3 = 100 x 5 x 50 = 25,000 (100 x 50) (A 1  (A 2  A 3 )) = 10 x 100 x 50 = 50,000 Total: 75,000 scalar multiplications one order of magnitude difference!!

29 6/08/2004 Lecture 6COSC3101A29 Matrix-Chain Multiplication Given a chain of matrices  A 1, A 2, …, A n , where for i = 1, 2, …, n matrix A i has dimensions p i-1 x p i, fully parenthesize the product A 1  A 2  A n in a way that minimizes the number of scalar multiplications. A 1  A 2  A i  A i+1  A n p 0 x p 1 p 1 x p 2 p i-1 x p i p i x p i+1 p n-1 x p n

30 6/08/2004 Lecture 6COSC3101A30 1. The Structure of an Optimal Parenthesization Notation: A i…j = A i A i+1  A j, i  j For i < j: A i…j = A i A i+1  A j = A i A i+1  A k A k+1  A j = A i…k A k+1…j Suppose that an optimal parenthesization of A i…j splits the product between A k and A k+1, where i  k < j

31 6/08/2004 Lecture 6COSC3101A31 Optimal Substructure A i…j = A i…k A k+1…j The parenthesization of the “prefix” A i…k must be an optimal parentesization If there were a less costly way to parenthesize A i…k, we could substitute that one in the parenthesization of A i…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

32 6/08/2004 Lecture 6COSC3101A32 2. A Recursive Solution Subproblem: determine the minimum cost of parenthesizing A i…j = A i A i+1  A j for 1  i  j  n Let m[i, j] = the minimum number of multiplications needed to compute A i…j –Full problem (A 1..n ): m[1, n]

33 6/08/2004 Lecture 6COSC3101A33 2. A Recursive Solution (cont.) Define m recursively: –i = j: A i…i = A i  m[i, i] = 0, for i = 1, 2, …, n –i < j: assume that the optimal parenthesization splits the product A i A i+1  A j between A k and A k+1, i  k < j A i…j = A i…k A k+1…j m[i, j] = m[i, k] + m[k+1, j] + p i-1 p k p j A i…k A k+1…j A i…k A k+1…j

34 6/08/2004 Lecture 6COSC3101A34 2. A Recursive Solution (cont.) Consider the subproblem of parenthesizing A i…j = A i A i+1  A j for 1  i  j  n = A i…k A k+1…j for i  k < j m[i, j] = the minimum number of multiplications needed to compute the product A i…j m[i, j] = m[i, k] + m[k+1, j] + p i-1 p k p j min # of multiplications to compute A i…k # of multiplications to compute A i…k A k…j min # of multiplications to compute A k+1…j m[i, k]m[k+1,j] p i-1 p k p j

35 6/08/2004 Lecture 6COSC3101A35 2. A Recursive Solution (cont.) m[i, j] = m[i, k] + m[k+1, j] + p i-1 p k p j We do not know the value of k –There are j – i possible values for k: k = i, i+1, …, j-1 Minimizing the cost of parenthesizing the product A i A i+1  A j becomes: 0 if i = j m[i, j] = min {m[i, k] + m[k+1, j] + p i-1 p k p j } if i < j i  k<j

36 6/08/2004 Lecture 6COSC3101A36 Reconstructing the Optimal Solution Additional information to maintain: s[i, j] = a value of k at which we can split the product A i A i+1  A j in order to obtain an optimal parenthesization

37 6/08/2004 Lecture 6COSC3101A37 3. Computing the Optimal Costs 0 if i = j m[i, j] = min {m[i, k] + m[k+1, j] + p i-1 p k p j } if i < j i  k<j How many subproblems do we have? –Parenthesize A i…j for 1  i  j  n –One problem for each choice of i and j A recurrent algorithm may encounter each subproblem many times in different branches of the recursion  overlapping subproblems Compute a solution using a tabular bottom-up approach   (n 2 )

38 6/08/2004 Lecture 6COSC3101A38 3. Computing the Optimal Costs (cont.) 0 if i = j m[i, j] = min {m[i, k] + m[k+1, j] + p i-1 p k p j } if i < j i  k<j How do we fill in the tables m[1..n, 1..n] and s[1..n, 1..n] ? –Determine which entries of the table are used in computing m[i, j] –m[i, j] = cost of computing a product of j – i – 1 matrices –m[i, j] depends only on costs for products of fewer than j – i – 1 matrices A i…j = A i…k A k+1…j –Fill in m such that it corresponds to solving problems of increasing length

39 6/08/2004 Lecture 6COSC3101A39 3. Computing the Optimal Costs (cont.) 0 if i = j m[i, j] = min {m[i, k] + m[k+1, j] + p i-1 p k p j } if i < j i  k<j Length = 0: i = j, i = 1, 2, …, n Length = 1: j = i + 1, i = 1, 2, …, n-1 1 1 23n 2 3 n first second Compute rows from bottom to top and from left to right In a similar matrix s we keep the optimal values of k m[1, n] gives the optimal solution to the problem i j

40 6/08/2004 Lecture 6COSC3101A40 Example: min {m[i, k] + m[k+1, j] + p i-1 p k p j } m[2, 2] + m[3, 5] + p 1 p 2 p 5 m[2, 3] + m[4, 5] + p 1 p 3 p 5 m[2, 4] + m[5, 5] + p 1 p 4 p 5 1 1 236 2 3 6 i j 45 4 5 m[2, 5] = min Values m[i, j] depend only on values that have been previously computed k = 2 k = 3 k = 4

41 6/08/2004 Lecture 6COSC3101A41 Example Compute A 1  A 2  A 3 A 1 : 10 x 100 (p 0 x p 1 ) A 2 : 100 x 5 (p 1 x p 2 ) A 3 : 5 x 50 (p 2 x p 3 ) m[i, i] = 0 for i = 1, 2, 3 m[1, 2] = m[1, 1] + m[2, 2] + p 0 p 1 p 2 (A 1 A 2 ) = 0 + 0 + 10 *100* 5 = 5,000 m[2, 3] = m[2, 2] + m[3, 3] + p 1 p 2 p 3 (A 2 A 3 ) = 0 + 0 + 100 * 5 * 50 = 25,000 m[1, 3] = min m[1, 1] + m[2, 3] + p 0 p 1 p 3 = 75,000 (A 1 (A 2 A 3 )) m[1, 2] + m[3, 3] + p 0 p 2 p 3 = 7,500 ((A 1 A 2 )A 3 ) 0 0 0 1 1 2 2 3 3 5000 1 25000 2 7500 2

42 6/08/2004 Lecture 6COSC3101A42 l = 2 10*20*25 =5000 35*15*5= 2625 l =3 m[3,5] = min m[3,4]+m[5,5] + 15*10*20 =750 + 0 + 3000 = 3750 m[3,3]+m[4,5] + 15*5*20 =0 + 1000 + 1500 = 2500

43 6/08/2004 Lecture 6COSC3101A43 MATRIX-CHAIN-ORDER(p) 1.n  length[p] – 1 2.for i  1 to n 3. do m[i, i]  0 4.for l  2 to nl is the length of the chain 5. do for i  1 to n – l + 1 6. do j  i + l – 1 7. m[i, j]   8. for k  i to j – 1 9. do q  m[i, k] + m[k+1, j] + p i-1 p k p j 10. if q < m[i, j] 11. then m[i, j]  q 12. s[i, j]  k 13.return m, s Running time:  (n 3 ) Chains of length zero have cost 0 For a particular m[i, j], look at all possible choices for k and choose the one that gives the minimum cost

44 6/08/2004 Lecture 6COSC3101A44 4. Construct the Optimal Solution Store the optimal choice made at each subproblem s[i, j] = a value of k such that an optimal parenthesization of A i..j splits the product between A k and A k+1 s[1, n] is associated with the entire product A 1..n –The final matrix multiplication will be split at k = s[1, n] A 1..n = A 1..s[1, n]  A s[1, n]+1..n –For each subproduct recursively find the corresponding value of k that results in an optimal parenthesization

45 6/08/2004 Lecture 6COSC3101A45 4. Construct the Optimal Solution (cont.) s[i, j] = value of k such that the optimal parenthesization of A i A i+1  A j splits the product between A k and A k+1 33355- 3334- 333- 12- 1- - 1 1 236 2 3 6 i j 45 4 5 s[1, n] = 3  A 1..6 = A 1..3 A 4..6 s[1, 3] = 1  A 1..3 = A 1..1 A 2..3 s[4, 6] = 5  A 4..6 = A 4..5 A 6..6 A 1..n = A 1..s[1, n]  A s[1, n]+1..n

46 6/08/2004 Lecture 6COSC3101A46 4. Construct the Optimal Solution (cont.) 33355- 3334- 333- 12- 1- - 1 1 236 2 3 6 i j 45 4 5 PRINT-OPT-PARENS( s, i, j ) if i = j then print “A” i else print “(” PRINT-OPT-PARENS( s, i, s[i, j] ) PRINT-OPT-PARENS( s, s[i, j] + 1, j ) print “)”

47 6/08/2004 Lecture 6COSC3101A47 Example: A 1    A 6 33355- 3334- 333- 12- 1- - 1 1 236 2 3 6 i j 45 4 5 PRINT-OPT-PARENS( s, i, j ) if i = j then print “A” i else print “(” PRINT-OPT-PARENS( s, i, s[i, j] ) PRINT-OPT-PARENS( s, s[i, j] + 1, j ) print “)” P-O-P(s, 1, 6)s[1, 6] = 3 i = 1, j = 6 “(“ P-O-P (s, 1, 3) s[1, 3] = 1 i = 1, j = 3 “(“ P-O-P(s, 1, 1)  “A 1 ” P-O-P(s, 2, 3) s[2, 3] = 2 i = 2, j = 3 “(“ P-O-P (s, 2, 2)  “A 2 ” P-O-P (s, 3, 3)  “A 3 ” “)” (( ( A 4 A 5 ) A 6 ) )A1A1 (A2A2 A3A3 )) … ( s[1..6, 1..6]

48 6/08/2004 Lecture 6COSC3101A48 Elements of Dynamic Programming Optimal Substructure –An optimal solution to a problem contains within it an optimal solution to subproblems –Optimal solution to the entire problem is built in a bottom-up manner from optimal solutions to subproblems Overlapping Subproblems –If a recursive algorithm revisits the same subproblems over and over  the problem has overlapping subproblems

49 6/08/2004 Lecture 6COSC3101A49 Optimal Substructure - Examples Assembly line –Fastest way of going through a station j contains: the fastest way of going through station j-1 on either line Matrix multiplication –Optimal parenthesization of A i  A i+1  A j that splits the product between A k and A k+1 contains: an optimal solution to the problem of parenthesizing A i..k and A k+1..j

50 6/08/2004 Lecture 6COSC3101A50 Discovering Optimal Substructure 1.Show that a solution to a problem consists of making a choice that leaves one or more similar problems to be solved 2.Suppose that for a given problem you are given the choice that leads to an optimal solution 3.Given this choice determine which subproblems result 4.Show that the solutions to the subproblems used within the optimal solution must themselves be optimal Cut-and-paste approach

51 6/08/2004 Lecture 6COSC3101A51 Parameters of Optimal Substructure How many subproblems are used in an optimal solution for the original problem –Assembly line: –Matrix multiplication: How many choices we have in determining which subproblems to use in an optimal solution –Assembly line: –Matrix multiplication: One subproblem (the line that gives best time) Two choices (line 1 or line 2) Two subproblems (subproducts A i..k, A k+1..j ) j - i choices for k (splitting the product)

52 6/08/2004 Lecture 6COSC3101A52 Parameters of Optimal Substructure Intuitively, the running time of a dynamic programming algorithm depends on two factors: –Number of subproblems overall –How many choices we look at for each subproblem Assembly line –  (n) subproblems (n stations) –2 choices for each subproblem Matrix multiplication: –  (n 2 ) subproblems (1  i  j  n) –At most n-1 choices  (n) overall  (n 3 ) overall

53 6/08/2004 Lecture 6COSC3101A53 Readings Chapter 15


Download ppt "COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these."

Similar presentations


Ads by Google