Download presentation
0
Assembly-line scheduling Elements of dynamic programming
Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference to Crane strokes
1
Dynamic Programming (pro-gram)
Like Divide and Conquer, DP solves a problem by partitioning the problem into sub-problems and combines solutions. The differences are that: D&C is top-down while DP is bottom to top approach. But memoization will allow top to down method. The sub-problems are independent from each other in the former case, while they are not independent in the dynamic programming. Therefore, DP algorithm solves every sub-problem just ONCE and saves its answer in a TABLE and then reuse it. Memoization. Optimization problems: Many solutions possible solutions and each has a value. A solution with the optimal sub-solutions. Such a solution is called an optimal solution to the problem. Not the optimum. Shortest path example The development of a dp algorithm can be in four steps. Characterize the structure of an optimal solution. Recursively define the value of an optimal solution. Compute the value of an optimal solution in a bottom-up fashion. Construct an optimal solution from computed information. Divide&Conquer Dynamic Programming
2
Assembly-Line Scheduling
ei time to enter xi time to exit assembly lines tj time to transfer from assembly aj processing time in each station. Brute-force approach Enumerate all possible sequences through lines i {1, 2}, For each sequence of n stations Sj j {1, n}, compute the passing time. (the computation takes (n) time.) Record the sequence with smaller passing time. However, there are too many possible sequences totaling 2n List all possible sequences, For each sequence of n stations, compute the passing time. (the computation takes (n) time.) Record the sequence with smaller passing time. However, there are total 2n possible sequences.
3
Assembly-Line Scheduling
DP Step 1: Analyze the structure of the fastest way through the paths Seeking an optimal substructure: The fastest possible way (min{f*}) through a station Si,j contains the fastest way from start to the station Si,j trough either assembly line S1, j-1 or S2, j-1. For j=1, there is only one possibility For j=2,3,…,n, two possibilities: from S1, j-1 or S2, j-1 from S1, j-1, additional time a1, j from S2, j-1, additional time t2, j-1 + a1,j suppose the fastest way through S1, j is through S1, j-1, then the chassis must have taken a fastest way from starting point through S1,j Why??? Similar rendering for S2, j-1. An optimal solution to a problem contains within it an optimal solution to sub-prbls. the fastest way through station Si,j contains within it the fastest way through station S1,j-1 or S2,j-1 . Thus can construct an optimal solution to a problem from the optimal solutions to sub-problems.
4
DP Step 3: Computing the fastest times in Θ(n) time.
DP Step 2: A recursive solution DP Step 3: Computing the fastest times in Θ(n) time. Problem: ri (j) = 2n-j. So f1[1] is referred to 2n-1 times. Total references to all fi[j] is (2n). Step 2: A recursive solution fi[j] (i=1,2 and j=1,2,…, n) denoting the fastest possible time through Si,j. f* denoting the fastest time for. f* = min(f1[n] +x1, f2[n] +x2) f1[1]=e1+a1,1, fastest time to get through S1,1 f1[j]=min(f1[j-1]+a1,j, f2[j-1]+ t2,j-1+ a1,j) Similarly to f2[j]. Recursive solution: f1[j]= e1+a1, if j=1 min(f1[j-1]+a1,j, f2[j-1]+ t2,j-1+ a1,j) if j>1 f2[j]= e2+a2, if j=1 min(f2[j-1]+a2,j, f1[j-1]+ t1,j-1+ a2,j) if j>1 Tabulate optimal values to the subproblems once calculated fi[j] (i=1,2; j=1,2,…,n). Keep the trace of the fastest way: li[j] recording the line number (1 or 2), whose station j-1 is used in one of the fastest way through Si,j. l* is the line whose station n is used in a fastest way through the factory. Step 3: Computing the fastest time One option for a recursive algorithm. Let ri(j) be the number of references made to fi[j] r1(n) = r2(n) = 1 r1(j) = r2(j) = r1(j+1)+ r2(j+1) ri (j) = 2n-j. So f1[1] is referred to 2n-1 times. Total references to all fi[j] is (2n). Thus, the running time is exponential. Non-recursive algorithm.
5
Running time: O(n). Step 4: Construct the fastest way through the factory
6
Determining the fastest way through the factory
7
Matrix-chain Multiplication
Problem definition: Given a chain of matrices A1, A2, ..., An, where matrix Ai has dimension pi-1×pi, find the order of matrix multiplications minimizing the number of the total scalar multiplications to compute the final product. Let A be a [p, q] matrix, and B be a [q, r] matrix. Then the complexity is pqr. In the matrix-chain multiplication problem, the actually matrices are not multiplied, the aim is to determine an order for multiplying matrices that has the lowest cost. Then, the time invested in determining optimal order must worth more than paid for by the time saved later on when actually performing the matrix multiplications. C(p,r) = A(p,q) * B(q,r) for i1 to p for j1 to r C[i,j]=0 for i=1 to p for j=1 to r for k=1 to q C[i,j] = C[i,j] + A[i,k]*B[k,j]
8
Example given in class 2x3 3x5 5x7 7x2 A1 A2 A3 A4 Suppose we want to multiply a sequence of matrices, A1…A4 with dimensions. Remember: Matrix multiplication is not commutative. 1- Total # of multiplication for this method is = 120 2- Above the total # of multiplications is = 128 3- Below the total # of multiplications is = 112 30 20 70 1 30 70 28 2 70 30 12 3
9
The aim as to fully parenthesize the product of matrices minimizing scalar multiplications.
For example, for the product A1 A2 A3 A4, a fully parenthesization is ((A1 A2) A3) A4. A product of matrices is fully parenthesized if it is either a single matrix, or a product of two fully parenthesized matrix product, surrounded by parentheses. Brute-force approach Enumerate all possible parenthesizations. Compute the number of scalar multiplications of each parenthesization. Select the parenthesization needing the least number of scalar multiplications. The number of enumerated parenthesizations of a product of n matrices, denoted by P(n), is the sequence of Catalan number growing as Ω(4n/n3/2) and solution to recurrence is Ω(2n). Parenthesization Different parenthesizations will have different number of multiplications for product of multiple matrices Example: A(10,100), B(100,5), C(5,50) If ((A B) C), 10 100 5 +10 5 50 =7500 If (A (B C)), 10 100 5 50=75000 The first way is ten times faster than the second !!! Denote A1, A2, …,An by < p0,p1,p2,…,pn> i.e, A1(p0,p1), A2(p1,p2), …, Ai(pi-1,pi),… An(pn-1,pn) Intuitive brute-force solution: Counting the number of parenthesizations by exhaustively checking all possible parenthesizations. Let P(n) denote the number of alternative parenthesizations of a sequence of n matrices: P(n) = if n=1 k=1n-1 P(k)P(n-k) if n2 The solution to the recursion is (2n). So brute-force will not work. if n=1 if n≥2 The Brute-force approach is inefficient.
10
3 numbers: 4 numbers: 5 numbers:
Catalan numbers: the number of ways in which parentheses can be placed in a sequence of numbers to be multiplied, two at a time 3 numbers: (1 (2 3)), ((1 2) 3) 4 numbers: (1 (2 (3 4))), (1 ((2 3) 4)), ((1 2) (3 4)), ((1 (2 3)) 4), (((1 2) 3) 4) 5 numbers: (1 (2 (3 (4 5)))), (1 (2 ((3 4) 5))), (1 ((2 3) (4 5))), (1 ((2 (3 4)) 5)), (1 (((2 3) 4) 5)), ((1 2) (3 (4 5))), ((1 2) ((3 4) 5)), ((1 (2 3)) (4 5)), ((1 (2 (3 4))) 5), ((1 ((2 3) 4)) 5), (((1 2) 3) (4 5)), (((1 2) (3 4)) 5), (((1 (2 3)) 4) 5) ((((1 2) 3) 4) 5)
11
With DP DP Step 1: structure of an optimal parenthesization
Let Ai..j (ij) denote the matrix resulting from AiAi+1…Aj Any parenthesization of AiAi+1…Aj must split the product between Ak and Ak+1 for some k, (ik<j). The cost = # of computing Ai..k + # of computing Ak+1..j + # Ai..k Ak+1..j. If k is the position for an optimal parenthesization, the parenthesization of “prefix” subchain AiAi+1…Ak within this optimal parenthesization of AiAi+1…Aj must be an optimal parenthesization of AiAi+1…Ak. AiAi+1…Ak Ak+1…Aj Step 1: Characterize the structure of an optimal solution Denote Ai..j = Ai Ai+1 … Aj An optimal parenthesization of the product A1A2…An splits the product between Ak and Ak+1 for some integer k where 1 ≤ k < n ( (A1 … Ak) (Ak+1… An) ) (Split at k) First compute matrices A1..k and Ak+1..n ; then multiply them to get the final matrix A1..n The cost of optimal parenthesization is the cost of computing matrix A1..k, plus the cost of computing Ak+1..n, plus the cost of multiplying them together Optimal substructure Suppose that the optimal solution is to split at k: ( (A1 … Ak) (Ak+1… An) ) Then: the parenthesizations of A1A2…Ak and Ak+1Ak+2…An within this solution must be optimal why? That is, an optimal solution to the problem contains within it the optimal solution to subproblems The idea: find an optimal solution to subproblems and use it to get a solution to the problem In order to find an optimal solution to the problem, we need to check all possible k values
12
Reminder: the dimension of Ai is pi-1 X pi
Step 2: Recursively define the value of an optimal solution DP Step 2: a recursive relation Let m[i,j] be the minimum number of multiplications needed to compute the matrix AiAi+1…Aj The lowest cost to compute A1 A2 … An would be m[1,n] Recurrence: 0 if i = j m[i,j] = minik<j {m[i,k] + m[k+1,j] +pi-1pkpj } if i<j ( (Ai … Ak) (Ak+1… Aj) ) (Split at k) m[i, k] m[k+1, j] pi-1Xpk matrix pkXpj matrix Reminder: the dimension of Ai is pi-1 X pi
13
Recursive (top-down) solution
using the formula for m[i,j]: RECURSIVE-MATRIX-CHAIN(p, i, j) if i=j then return 0 m[i, j] = for k ← 1 to j − 1 q ← RECURSIVE-MATRIX-CHAIN (p, i , k) + RECURSIVE-MATRIX-CHAIN (p, k+1 , j) + p[i-1] p[k] p[j] if q < m[i, j] then m[i, j] ← q return m[i, j] Line 1 Line 6 Line 4 Line 5 Line 6 Complexity: for n > 1
14
Complexity of the recursive solution
Using inductive method to prove by using the substitution method – we guess a solution and then prove by using mathematical induction that it is correct. Prove that T(n) = (2n) that is T(n) ≥ 2n-1 for all n ≥1. Induction Base: T(1) ≥1=20 Induction Assumption: assume T(k) ≥ 2k-1 for all 1 ≤ k < n Induction Step:
15
Step 3, Computing the optimal cost
If by recursive algorithm is exponential in n, (2n), no better than brute-force. But only have n = (n2) subproblems. Recursive behavior will encounter to revisit the same overlapping subproblems many times. If tabling the answers for subproblems, each subproblem is only solved once. The second hallmark of DP: overlapping subproblems and solve every subproblem just once. 2 n æ è ç ö ø ÷
16
Takes O(n3) time Requires (n2) space
Step 3: Compute the value of an optimal solution bottom-up Input: n; an array p[0…n] containing matrix dimensions State: m[1..n, 1..n] for storing m[i, j] s[1..n, 1..n] for storing the optimal k that was used to calculate m[i, j] Result: Minimum-cost table m and split table s MATRIX-CHAIN-TABLE(p, n) for i ← 1 to n m[i, i] ← 0 for l ← 2 to n for i ← 1 to n-l+1 j ← i+l-1 m[i, j] ← for k ← i to j-1 q ← m[i, k] + m[k+1, j] + p[i-1] p[k] p[j] if q < m[i, j] m[i, j] ← q s[i, j] ← k return m and s Takes O(n3) time Requires (n2) space
17
chains of length 1 A1 30×1 A2 1×40 A3 40×10 A4 10×25 j 1 2 3 4 i
18
chains of length 2 j i 1 2 3 4 A1 30×1 A2 1×40 A3 40×10 A4 10×25 1200
1200 400 10000 A1 30×1 A2 1×40 A3 40×10 A4 10×25 i
19
chains of length 3 j i 1 2 3 4 A1 30×1 A2 1×40 A3 40×10 A4 10×25 1200
1200 700 400 650 10000 i A1 30×1 A2 1×40 A3 40×10 A4 10×25
20
chains of length 4 j i 1 2 3 4 A1 30×1 A2 1×40 A3 40×10 A4 10×25 1200
1200 700 1400 400 650 10000 i A1 30×1 A2 1×40 A3 40×10 A4 10×25
21
Printing the solution j i Output: (A1((A2A3)A4)) 2 3 4 1 A1 30×1 A2
1×40 A3 40×10 A4 10×25 PRINT(s, 1, 4) PRINT (s, 1, 1) PRINT (s, 2, 4) PRINT (s, 2, 3) PRINT (s, 4, 4) Output: (A1((A2A3)A4)) PRINT (s, 2, 2) PRINT (s, 3, 3)
22
Step 4: Constructing an optimal solution
Each entry s[i, j ]=k shows where to split the product Ai Ai+1 … Aj for the minimum cost: A1 … An = ( (A1 … As[i, n]) (As[i, n]+1… An) ) To print the solution invoke the following function with (s, 1, n) as the parameter: PRINT-OPTIMAL-PARENS(s, i, j) if i=j then print “A”i else print “(” PRINT-OPTIMAL-PARENS(s, i, s[i, j]) PRINT-OPTIMAL-PARENS(s, s[i, j]+1, j) print “)”
23
Suppose A1 A2 Ai……………….Ar P1xP2 P2xP3 PixPi+1 ………. PrxPr+1 Assume
mij = the # of multiplication needed to multiply Ai A i Aj Initial value mii = mjj = 0 Final Value m1r A1……..Ai………Ak Ak+1………..Aj mij = mik + mk+1, j + Pi Pk+1 Pj+1 k could be i <= k <= j-1 We know the range of k but don’t know the exact value of k
24
Thus mij = min(mik + mk+1, j + Pi Pk+1 Pj+1) for i <= k <= j-1
recurrence index : (j-1) - i + 1=(j-i) Example: Calculate m14 for A A2 A3 A4 2x5 5x3 3x7 7x2 P1 = 2, P2 = 5, P3 = 3, P4 = 7, P5 = 2 j-i = j-i = j-i = j-i = 3 m11 = m12 = m13 = m14 = 84 m22= m23= m24=72 m33= m34= m44=0 mij = min(mik + mk+1,i + PiPk+1Pj+1) for i <= k <= j-1 m12 = min(m11 + m22 + P1P2P3) for 1 <= k <= 1 = min( x5x3) m(1,1) m(1,2) m(1,3) m(1,4) m(1,5) m(1,6) m(2,2) m(2,3) m(2,4) m(2,5) m(2,6) m(3,3) m(3,4) m(3,5) m(3,6) m(4,4) m(4,5) m(4,6) m(5,5) m(5,6) m(6,6)
25
mij = min(mik+ mk+1, j+ PiPk+1Pj+1) for i<= k<= j-1
m13 = min(m1k + mk+1,3 + P1Pk+1P4)for 1<=k<=2 = min(m11 + m23 + P1P2P4 , m12 + m33 + P1P3P4 ) = min( x5x7, x3x7) = min( , ) = 72 m24= min(m2k + mk+1,3 + P2Pk+1P4)for 2<=k<=3 = min(m22 + m34 + P2P3P5 , m23 + m44 + P2P4P5 ) = min( x3x2, x7x2) = min( , ….) = 72 m14= min(m1k + mk+1,4 + P1Pk+1P5)for 1<=k<=3 = min(m11 + m24 + P1P2P5 , m12 + m34 + P1P3P5 , m13 + m44 + P1P4P5 ) = min( 72+2x5x2, x3x2, 72+2x7x2) = min( 72+20, , ) = min( 92, 84, 100) = 84
27
Memoized Matrix Chain 3..3 1..3 3..4 1..2 2..4 1..1 4..4 2..3 2..2 1..4 LOOKUP-CHAIN(p,i,j) if m[i,j]< then return m[i,j] if i=j then m[i,j] 0 else for ki to j-1 do q LOOKUP-CHAIN(p,i,k)+ LOOKUP-CHAIN(p,k+1,j)+pi-1pkpj if q< m[i,j] then m[i,j] q return m[i,j]
28
For a DP to be applicable an optmztn prbl must have:
Optimal substructure An optimal solution to the problem contains within it optimal solutions to subproblems. Overlapping (dependencies) subproblems The space of subproblems must be small; i.e., the same subproblems are encountered over and over. DP step3. Memoization: T(n)=O(n3), PSpace(n)=(n2) A top-down variation of dynamic programming Idea: remember the solution to subproblems as they are solved in the simple recursive algorithm but may be quite costly DP is considered better when all subproblems must be calculated, because there is no overhead for recursion. Lookup-Chain(p, i, j) LookUp-Table(p,i,j) Initialize all m[i,j] to if m[i,j] < then return m[i,j] if i =j then m[i,j] 0 else for k ← 1 to j − 1 q ← LookUp-Table (p, i , k) + LookUp-Table (p, k+1 , j) + p[i-1] p[k] p[j] if q < m[i, j] then m[i, j] ← q return m[i, j]
29
Elements of DP Optimal substructure
A problem exhibits optimal substructure if an optimal solution to the problem contains within its optimal solution to subproblems. Overlapping subproblems When a recursive algorithm revisits the same problem over and over again, that is the optimization problem has overlapping subproblems. Subtleties Better to not assume that optimal substructure applies in general. Two examples in a directed graph G = (V, E) and vertices u, v V. Unweighted shortest path: Find a path from u to v consisting of the fewest edges. Good for Dynamic programming. Unweighted longest simple path: Find a simple path from u to v consisting of the most edges. Not good for Dynamic programming.
30
Θ(n) subproblems · 2 choices = Θ(n)
The running time of a dynamic-programming algorithm depends on the product of two factors. The number of subproblems overall * the number of choices for each subproblem. = Sum of entire choices. Assembly line scheduling Θ(n) subproblems · 2 choices = Θ(n) Matrix chain multiplication Θ(n2) subproblems · (n-1) choices = O(n3)
31
Principle of Optimality (Optimal Substructure)
The principle of optimality applies to a problem (not an algorithm) A large number of optimization problems satisfy this principle. Principle of optimality: Given an optimal sequence of decisions or choices, each subsequence must also be optimal. Principle of optimality - shortest path problem Problem: Given a graph G and vertices s and t, find a shortest path in G from s to t Theorem: A subpath P’ (from s’ to t’) of a shortest path P is a shortest path from s’ to t’ of the subgraph G’ induced by P’. Subpaths are paths that start or end at an intermediate vertex of P. Proof: If P’ was not a shortest path from s’ to t’ in G’, we can substitute the subpath from s’ to t’ in P, by the shortest path in G’ from s’ to t’. The result is a shorter path from s to t than P. This contradicts our assumption that P is a shortest path from s to t.
32
Principle of Optimality
P’={(c.d), (d,e)} P={ (a,b), (b,c) (c.d), (d,e)} a b c d f e G’ G 3 1 5 6 10 7 13 P’ must be a shortest path from c to e in G’, otherwise P cannot be a shortest path from a to e in G. A C D B Longest A to B Longest C to B Longest A to C Problem: What is the longest simple route between City A and B? Simple = never visit the same spot twice. The longest simple route (solid line) has city C as an intermediate city. Does not consist of the longest simple route from A to C and the longest simple route from C to B. Therefore does not satisfy the Principle of Optimality
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.