Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling...

33
Dynamic Programming Dynamic Programming Dr. M. Sakalli, Marmara University Dr. M. Sakalli, Marmara University Matrix Chain Problem Matrix Chain Problem Assembly-line scheduling Assembly-line scheduling Elements of dynamic programming Elements of dynamic programming Picture reference to http://www.flickr.com/photos/7271221@N04/3408234040/sizes/l/in/photostream/ . Crane strokes

Transcript of Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling...

Page 1: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Dynamic ProgrammingDynamic ProgrammingDr. M. Sakalli, Marmara UniversityDr. M. Sakalli, Marmara University

Matrix Chain ProblemMatrix Chain Problem Assembly-line schedulingAssembly-line schedulingElements of dynamic programmingElements of dynamic programming

Picture reference to http://www.flickr.com/photos/7271221@N04/3408234040/sizes/l/in/photostream/ . Crane strokes

Page 2: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Dynamic Programming (proDynamic Programming (pro--gram)gram)o Like Divide and ConquerLike Divide and Conquer,, D DPP solves a problem by partitioning the problem solves a problem by partitioning the problem

into sub-problems and combines solutions. The into sub-problems and combines solutions. The differences are that: differences are that: • D&C is topD&C is top--down while DP is down while DP is bottom to top approach. But memoization will bottom to top approach. But memoization will

allow top to down method. allow top to down method. • The sub-problems areThe sub-problems are independent independent from each other from each other in the former case, win the former case, while hile tthheey y

are are not independentnot independent in the dynamic programming. in the dynamic programming. • Therefore, Therefore, DPDP algorithm solves every sub-problem just algorithm solves every sub-problem just ONCEONCE and and saves its saves its

answer in a Tanswer in a TAABLEBLE and then reuse it. Memoization. and then reuse it. Memoization.

o Optimization problemsOptimization problems: : MMany solutions possible solutionsany solutions possible solutions and each and each has a valuehas a value..• AA solution with solution with thethe optimal sub-solutions optimal sub-solutions.. Such a solution is called Such a solution is called an an optimal solution to the optimal solution to the

problem.problem. Not the opti Not the optimum. mum. Shortest path exampleShortest path example

o The development of a dp algorithm can be in four steps.The development of a dp algorithm can be in four steps.1.1. Characterize the structure of an optimal solution.Characterize the structure of an optimal solution.

2.2. Recursively define the value of an optimal solution.Recursively define the value of an optimal solution.

3.3. Compute the value of an optimal solution in a bottom-up fashion.Compute the value of an optimal solution in a bottom-up fashion.

4.4. Construct an optimal solution from computed information.Construct an optimal solution from computed information.

Dynamic ProgrammingDivide&Conquer

Page 3: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Assembly-Line SchedulingAssembly-Line Scheduling

o eeii time to enter time to enter

o xxii time to exit assembly lines time to exit assembly lines

o ttjj time to transfer from assembly time to transfer from assembly

o aajj processing time in each station. processing time in each station.

o Brute-force approacBrute-force approachh• Enumerate all possible sequences through lines Enumerate all possible sequences through lines ii {1, 2}{1, 2}, ,

• For each sequence of For each sequence of nn stations stations SSjj jj {1, n}{1, n}, compute the passing time. (the computation takes , compute the passing time. (the computation takes ((nn) time.)) time.)

• Record the sequence with smaller passing time. Record the sequence with smaller passing time. • However, there are too many possible sequences totaling 2However, there are too many possible sequences totaling 2nn

Page 4: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

o DP Step 1: DP Step 1: AnalyzeAnalyze tthe structure of the fastest way through the pathshe structure of the fastest way through the paths• Seeking an Seeking an ooptimal substructureptimal substructure: : The fastest possible way The fastest possible way (min{f*})(min{f*}) through through

a station a station SSi,j i,j contains contains the fastest way the fastest way from start to the station from start to the station SSi,ji,j trough either trough either

assasseemmbbly line ly line SS1, 1, jj-1-1 or or SS2, 2, jj-1-1. . – For For jj=1, there is only one possibility=1, there is only one possibility

– For For jj=2,3,…,=2,3,…,nn, two possibilities: from S, two possibilities: from S1, 1, jj-1-1 oror SS2, 2, jj-1-1

– from Sfrom S1, 1, jj-1-1, additional time a, additional time a1, 1, jj

– from Sfrom S2, 2, jj-1-1, additional time t, additional time t2, 2, jj-1-1 + a + a1,1,jj

– suppose the fastest way through Ssuppose the fastest way through S1, 1, jj is throughis through SS1, 1, jj-1-1, then the , then the chassis must have chassis must have

taken a fastest way from starting point through Staken a fastest way from starting point through S1,1,jj-1-1.. Why??? Why???

– Similar rendering for SSimilar rendering for S2, 2, jj-1-1..

o An optimal solution to a problem contains within it an optimal solution to sub-prbls.An optimal solution to a problem contains within it an optimal solution to sub-prbls.

o the fastest way through station Sthe fastest way through station S ii,,jj contains within it the fastest way through station Scontains within it the fastest way through station S1,1,jj--

11 oror SS2,2,jj-1-1 . .

o Thus can construct an optimal solution to a problem from the optimal solutions to Thus can construct an optimal solution to a problem from the optimal solutions to sub-problems.sub-problems.

Assembly-Line SchedulingAssembly-Line Scheduling

Page 5: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

)][,][min(2211

* xnfxnff

2if

,1if

2if

,1if

)]1[,]1[min(][

)]1[,]1[min(][

,21,11,22

1,22

2

,11,22,11

1,11

1

j

j

j

j

atjfajf

aejf

atjfajf

aejf

jjj

jjj

 

 

 

 o DP Step 2: A recursive solutionDP Step 2: A recursive solution

o DP Step 3: Computing the fastest times DP Step 3: Computing the fastest times in Θ(in Θ(nn) time. ) time.

Problem: Problem: rrii ((jj) = 2) = 2nn--jj. So . So ff11[1] is referred to 2[1] is referred to 2nn-1 -1 times. times.

Total references to all Total references to all ffii[[jj] is ] is (2(2nn). ).

1)()( 21 nrnr )1()1()()( 2121 jrjrjrjr

Page 6: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Running time: Running time: OO((nn).).

o Step 4: Construct the fastest way through the factoryStep 4: Construct the fastest way through the factory

Page 7: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

o Determining the fastest way through the factoryDetermining the fastest way through the factory

Page 8: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Matrix-chain MultiplicationMatrix-chain Multiplicationo Problem definition:Problem definition: Given a chain of matrices Given a chain of matrices AA11, , AA22, ..., , ..., AAnn, where matrix , where matrix AAii has dimension has dimension ppi-i-11××ppii, find the order of matrix multiplications minimizing the number of the total scalar multiplications to compute the , find the order of matrix multiplications minimizing the number of the total scalar multiplications to compute the final final product.product.

o Let Let AA be a [p be a [p, q] , q] matrix, and matrix, and BB be a [q be a [q, r], r] matrix. Then the complexity is p matrix. Then the complexity is pqrqr. .

o 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. 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. o 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.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(C(pp,,rr) = A() = A(pp,,qq) * B() * B(qq,,rr))•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]

Page 9: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Example given in classExample given in class

2x3 3x5 5x7 7x22x3 3x5 5x7 7x2 A1 A2 A3 A4 A1 A2 A3 A4

30 70

28

2

7030

12

3

Suppose we want to multiply a sequence Suppose we want to multiply a sequence of matrices, A1…A4 with dimensions. of matrices, A1…A4 with dimensions.

Remember: Matrix multiplication is not Remember: Matrix multiplication is not commutative.commutative.

1- Total # of multiplication for this 1- Total # of multiplication for this method is 30 + 70 +20 = 120 method is 30 + 70 +20 = 120

2- Above the total # of multiplications is 2- Above the total # of multiplications is 30 + 70 +28 = 128 30 + 70 +28 = 128

3- Below the total # of multiplications is 3- Below the total # of multiplications is 70 + 30 +12 = 112 70 + 30 +12 = 112

30

20

701

Page 10: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

PParenthesizationarenthesization o TThe aim as to fully parenthesize the product of matrices he aim as to fully parenthesize the product of matrices

minimizing scalar multiplications.minimizing scalar multiplications.

o For example, for the product For example, for the product AA11 AA22 AA33 AA44, a fully , a fully

parenthesizationparenthesization is (( is ((AA11 AA22) ) AA33) ) AA44. .

o A product of matrices is fully parenthesizedA product of matrices is fully parenthesized if it is either a single if it is either a single matrix, or a product of two fully parenthesized matrix matrix, or a product of two fully parenthesized matrix product, product, surrounded by parenthesessurrounded by parentheses. .

o Brute-force approachBrute-force approacha)a) Enumerate all possible parenthesizations.Enumerate all possible parenthesizations.

b)b) Compute the number of scalar multiplications of each parenthesization.Compute the number of scalar multiplications of each parenthesization.

c)c) Select the parenthesization needing the least number of scalar multiplications.Select the parenthesization needing the least number of scalar multiplications.

o The number of enumerated parenthesizations of a product of The number of enumerated parenthesizations of a product of n n matrices, denoted by matrices, denoted by PP((nn), is the sequence of ), is the sequence of CCatalan number atalan number growing as growing as ΩΩ(4(4nn//nn3/23/2) and solution to recurrence is ) and solution to recurrence is ΩΩ(2(2nn).).

1

1

)()(

1

)( n

k

knPkPnP

if n=1

if n≥2The Brute-force approach is inefficient.The Brute-force approach is inefficient.

3/ 2

2( 1)1 4( ) ( 1) ( )

1

nnP n C n

nn n

Page 11: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Catalan numbers: the number of ways in which Catalan numbers: the number of ways in which parenthesesparentheses can be can be placed in a sequence of numbers to be multiplied, two at a time placed in a sequence of numbers to be multiplied, two at a time

3 numbers: 3 numbers: (1 (2 3)), ((1 2) 3)(1 (2 3)), ((1 2) 3)

4 numbers: 4 numbers: (1 (2 (3 4))), (1 ((2 3) 4)), ((1 2) (3 4)), ((1 (2 3)) 4), (((1 2) 3) (1 (2 (3 4))), (1 ((2 3) 4)), ((1 2) (3 4)), ((1 (2 3)) 4), (((1 2) 3)

4)4)

5 numbers: 5 numbers: (1 (2 (3 (4 5)))), (1 (2 ((3 4) 5))), (1 ((2 3) (4 5))), (1 ((2 (3 4)) (1 (2 (3 (4 5)))), (1 (2 ((3 4) 5))), (1 ((2 3) (4 5))), (1 ((2 (3 4))

5)), 5)), (1 (((2 3) 4) 5)), ((1 2) (3 (4 5))), ((1 2) ((3 4) 5)), ((1 (2 3)) (4 (1 (((2 3) 4) 5)), ((1 2) (3 (4 5))), ((1 2) ((3 4) 5)), ((1 (2 3)) (4

5)), 5)), ((1 (2 (3 4))) 5), ((1 ((2 3) 4)) 5), (((1 2) 3) (4 5)), (((1 2) (3 4)) ((1 (2 (3 4))) 5), ((1 ((2 3) 4)) 5), (((1 2) 3) (4 5)), (((1 2) (3 4))

5), 5), (((1 (2 3)) 4) 5) ((((1 2) 3) 4) 5) (((1 (2 3)) 4) 5) ((((1 2) 3) 4) 5)

Page 12: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

With DPWith DP

o DP Step 1: structure of an optimal parenthesizationDP Step 1: structure of an optimal parenthesization• Let ALet Aii....jj ( (iijj) denote the matrix resulting from A) denote the matrix resulting from AiiAAii+1+1……AAj j

• AnyAny parenthesization of Aparenthesization of AiiAAii+1+1……AAj j must split the product between Amust split the product between Akk and A and Akk+1+1 for for

some some kk, (, (iikk<<jj). The cost =). The cost = # of computing A # of computing Ai..ki..k + + # of computing A# of computing Akk+1..+1..jj + + # A# Ai..k i..k A Akk+1..+1..j.j.

• If If kk is the position for an optimal parenthesization, the parenthesization of “prefix” is the position for an optimal parenthesization, the parenthesization of “prefix” subchain Asubchain AiiAAii+1+1……AAk k within this optimal parenthesization of Awithin this optimal parenthesization of A iiAAii+1+1……AAj j must must

be an optimal parenthesization of Abe an optimal parenthesization of AiiAAii+1+1……AAk. k.

• AAiiAAii+1+1……AAk k AAkk+1+1……AAj j

o (( ... )( ... ))A A A A A Ak k k n1 2 1 2

Optimal

Combine

Page 13: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

o DP Step 2: a recursive relationDP Step 2: a recursive relation

• Let m[Let m[ii,,jj] be the minimum number of multiplications ] be the minimum number of multiplications needed to compute the matrix Aneeded to compute the matrix AiiAAii+1+1……AAjj

o The lowest cost to compute The lowest cost to compute AA11 AA22 … … AAnn would be would be mm[1,[1,nn]]

o Recurrence:Recurrence:

0 0 if if ii = = jj

• m[m[ii,,jj] = ] =

minminiikk<<jj {m[ {m[ii,,kk] + m[] + m[k+k+1,1,jj] +] +ppii-1-1ppkkppjj } if } if ii<<jj ( (( (AAii … … AAkk) () (AAkk+1+1… … AAjj) ) (Split at ) ) (Split at kk))

Step 2: Recursively define the value of an optimal solutionStep 2: Recursively define the value of an optimal solution

m[i, k] m[k+1, j]

pi-1Xpk matrix pkXpj matrix

Reminder: the dimension of Ai is pi-1 X pi

Page 14: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Recursive (top-down) solutionRecursive (top-down) solutionusing the formula for m[i,j]:using the formula for m[i,j]:

RECURSIVE-MATRIX-CHAINRECURSIVE-MATRIX-CHAIN((p,p, i, ji, j))

1.1. ifif i=ji=j then returnthen return 0 0

2.2. mm[[i, ji, j] = ] = 3.3. for for k k ← 1 ← 1 toto j j − 1− 1

4.4. qq ←← RECURSIVE-MATRIX-CHAIN RECURSIVE-MATRIX-CHAIN ((p,p, ii , k, k))

5.5. + RECURSIVE-MATRIX-CHAIN (+ RECURSIVE-MATRIX-CHAIN (p,p, kk+1+1 , ,

jj))

6.6. + + pp[[ii-1]-1] p[p[kk]] pp[[jj]]

7.7. ifif qq < < mm[[i, ji, j] ] then then mm[[i, ji, j] ] ← ← qq

8.8. returnreturn mm[[i, ji, j] ]

1

1

(1) 1

( ) 1 ( ( ) ( ) 1)n

k

T

T n T k T n k

Complexity: for n > 1

Line 6

Line 1

Line 4 Line 5 Line 6

Page 15: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Complexity of the recursive solutionComplexity of the recursive solution

o Using inductive method to prove by using the Using inductive method to prove by using the substitution methodsubstitution method – we – we guess a solution and then prove by using mathematical induction that it is guess a solution and then prove by using mathematical induction that it is correct. correct.

o Prove that Prove that T(n) = T(n) = (2(2nn) ) that is T(n) ≥ 2that is T(n) ≥ 2n-1n-1 for all n ≥1. for all n ≥1.

o Induction Base: T(1) ≥1=2Induction Base: T(1) ≥1=200

o Induction Assumption: assume T(k) ≥ 2Induction Assumption: assume T(k) ≥ 2k-1k-1 for all 1 ≤ for all 1 ≤ kk < n < no Induction Step:Induction Step:

1 1

1 1

( ) 1 ( ( ) ( ) 1) 2 ( )n n

k i

T n T k T n k n T i

1 21

1 0

( ) 2 2 2 2n n

i i

i i

T n n n

1 12(2 1) 2 2 2n n nn n

Page 16: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

o Step 3, Computing the optimal costStep 3, Computing the optimal cost• If by recursive algorithm is exponential in n, If by recursive algorithm is exponential in n, (2(2nn), no better than ), no better than

brute-force.brute-force.

• But only have + But only have + nn = = ((nn22) subproblems. ) subproblems.

• Recursive behavior will encounter to revisit the same overlapping Recursive behavior will encounter to revisit the same overlapping subproblems many times.subproblems many times.

• If tabling the answers for subproblems, each subproblem is only If tabling the answers for subproblems, each subproblem is only solved once.solved once.

• The second hallmark of DP: The second hallmark of DP: overlapping subproblemsoverlapping subproblems and solve every and solve every subproblem just once.subproblem just once.

2

n

Page 17: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Step 3: Compute the value of an optimal solution bottom-upStep 3: Compute the value of an optimal solution bottom-upInput: Input: n; n; an array an array pp[0[0……nn] containing matrix dimensions] containing matrix dimensionsState: State: mm[1..[1..nn, 1.., 1..nn] for storing ] for storing mm[[ii, , jj] ]

ss[1..[1..nn, 1.., 1..nn] for storing the optimal ] for storing the optimal kk that was used to calculate that was used to calculate mm[[ii, , jj] ] Result: Minimum-cost table Result: Minimum-cost table mm and split table and split table ss

MATRIX-CHAIN-TABLEMATRIX-CHAIN-TABLE((pp, , nn))for for i i ← 1 to ← 1 to nn

mm[[i, ii, i]] ← 0 ← 0 for for l l ← 2 to ← 2 to nn

for for i i ← 1 to ← 1 to n-l+n-l+11jj ← ← ii++ll-1-1mm[[i, ji, j]] ← ← for for k k ← ← ii to to j-j-11

qq ← ← mm[[i, ki, k]] + m + m[[kk+1+1, j, j]] + p + p[[ii-1] -1] pp[[kk] ] pp[[jj]]if if qq < < mm[[i, ji, j]]

mm[[i, ji, j]] ← ← qqss[[i, ji, j]] ← ← kk

return return mm and and ss Takes O(Takes O(nn33) time) timeRequires Requires ((nn22) space) space

Page 18: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

chains of length 1chains of length 1

11 22 33 44

11 00

22 00

33 00

44 00

AA11 3030×1×1

AA22 11×40×40

AA33 4040×10×10

AA44 1010×25×25

ij

Page 19: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

chains of length 2chains of length 2

11 22 33 44

11 00 12001200

11

22 00 400400

22

33 00 1000010000

33

44 00

AA11 3030×1×1

AA22 11×40×40

AA33 4040×10×10

AA44 1010×25×25

ij

[1,2] [1,1] [2,2] 30 1 40 1200

[2,3] [2,2] [3,3] 1 40 10 400

[3,4] [3,3] [4,4] 40 10 25 10000

m m m

m m m

m m m

Page 20: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

chains of length 3chains of length 3

11 22 33 44

11 00 12001200

11

700700

11

22 00 400400

22

650650

33

33 00 1000010000

33

44 00

AA11 3030×1×1

AA22 11×40×40

AA33 4040×10×10

AA44 1010×25×25

[1,1] [2,3] 30 1 10 700[1,3] min 700

[1,2] [3,3] 30 40 10 13200

m mm

m m

i j

[2,2] [3,4] 1 40 25 11000[2,4] min 650

[2,3] [4,4] 1 10 25 650

m mm

m m

Page 21: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

chains of length 4chains of length 4

AA11 3030×1×1

AA22 11×40×40

AA33 4040×10×10

AA44 1010×25×25

[1,1] [2,4] 30 1 25 1400

[1,4] min [1,2] [3,4] 30 40 25 41200 1400

[1,3] [4,4] 30 10 25 8200

m m

m m m

m m

11 22 33 44

11 00 12001200

11

700700

11

14001400

11

22 00 400400

22

650650

33

33 00 1000010000

33

44 00

i j

Page 22: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Printing the solutionPrinting the solution

AA11 3030×1×1

AA22 11×40×40

AA33 4040×10×10

AA44 1010×25×25

22 33 44

11 11 11 11

22 22 33

33 33

i j

PRINT(s, 1, 4)

PRINT (s, 1, 1) PRINT (s, 2, 4)

PRINT (s, 2, 2)

PRINT (s, 2, 3) PRINT (s, 4, 4)

PRINT (s, 3, 3)

Output: (A1((A2A3)A4))

Page 23: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Step 4: Constructing an optimal solutionStep 4: Constructing an optimal solutiono Each entry Each entry ss[[ii, , jj ]= ]=kk shows where to split the product shows where to split the product AAii A Aii+1+1

… A… Ajj for the minimum cost: for the minimum cost:

AA11 … … AAnn == ( (( (AA11 … … AAss[[ii, , nn]]) () (AAs[s[ii, , nn]+1]+1… … AAnn) ) ) ) o To print the solution invoke the following function with (To print the solution invoke the following function with (ss, ,

1, 1, nn) as the parameter:) as the parameter:PRINT-OPTIMAL-PARENSPRINT-OPTIMAL-PARENS((s,s, i, ji, j))

1.1. ifif i=ji=j then then print print ““AA””ii

2.2. else else print print ““((””3.3. PRINT-OPTIMAL-PARENSPRINT-OPTIMAL-PARENS((s,s, i, si, s[[ii, , jj])])4.4. PRINT-OPTIMAL-PARENSPRINT-OPTIMAL-PARENS((s,s, ss[[ii, , jj]+1]+1, j, j))5.5. print print ““))””

Page 24: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

SupposeSuppose

AA11 A A22 A Aii……………….……………….AArr

PP11xPxP2 2 PP22xPxP3 3 PPiixPxPii+1 ………. +1 ………. PPrrxPxPrr+1+1

Assume Assume

mmijij = the # of multiplication needed to multiply A = the # of multiplication needed to multiply Aii A A ii+1+1......A......Ajj

Initial valueInitial value mmiiii = m= mjjjj = 0 = 0

Final ValueFinal Value m m11rr

AA11……..A……..Aii………A………Akk A Akk+1+1………..A………..Ajj

mmijij = m = mikik + m + mkk+1+1,, jj + P + Pii P Pkk+1+1 P Pjj+1+1

k could be i <= k <= j-1k could be i <= k <= j-1

We know the range of k but don’t know the exact value of kWe know the range of k but don’t know the exact value of k

Page 25: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

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

A1 A2 A3 A4

2x5 5x3 3x7 7x2

P1 = 2, P2 = 5, P3 = 3, P4 = 7, P5 = 2

j-i = 0 j-i = 1 j-i = 2 j-i = 3

m11 = 0 m12 = 30 m13 = 72 m14 = 84

m22=0 m23=105 m24=72

m33=0 m34=42 m44=0

mijij = min(mikik + mkk+1,+1,ii + PiiPkk+1+1Pjj+1+1) for i <= k <= j-1m1212 = min(m11 + m22 + P1P2P3) for 1 <= k <= 1 = min( 0 + 0 + 2x5x3)

mm(1,1)(1,1) mm(1,2)(1,2) mm(1,3)(1,3) mm(1,4)(1,4) mm(1,5)(1,5) mm(1,6)(1,6)

mm(2,2)(2,2) mm(2,3)(2,3) mm(2,4)(2,4) mm(2,5)(2,5) mm(2,6)(2,6)

mm(3,3)(3,3) mm(3,4)(3,4) mm(3,5)(3,5) mm(3,6)(3,6)

mm(4,4)(4,4) mm(4,5)(4,5) mm(4,6)(4,6)

mm(5,5)(5,5) mm(5,6)(5,6)

mm(6,6)(6,6)

Page 26: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

mmijij = min(m = min(mikik+ m+ mk+1, jk+1, j+ P+ PiiPPk+1k+1PPj+1j+1) for i<= k<= j-1) for i<= k<= j-1

mm1313 = min(m= min(m1k1k + m + mk+1,3k+1,3 + P + P11PPk+1k+1PP44)for 1<=k<=2)for 1<=k<=2

= min(m= min(m1111 + m + m2323 + P + P11PP22PP4 4 , m, m1212 + m + m3333 + P + P11PP33PP4 4 ))

= min( 0+105+2x5x7, 30 + 0 + 2x3x7)= min( 0+105+2x5x7, 30 + 0 + 2x3x7)

= min( 105+70, 30 + 42) = 72= min( 105+70, 30 + 42) = 72

mm2424= min(m= min(m2k2k + m + mk+1,3k+1,3 + P + P22PPk+1k+1PP44)for 2<=k<=3)for 2<=k<=3

= min(m= min(m2222 + m + m3434 + P + P22PP33PP5 5 , m, m2323 + m + m4444 + P + P22PP44PP5 5 ))

= min( 0+42+5x3x2, 105+ 0 + 5x7x2)= min( 0+42+5x3x2, 105+ 0 + 5x7x2)

= min( 42 + 30, ….) = 72 = min( 42 + 30, ….) = 72

mm1414= min(m= min(m1k1k + m + mk+1,4k+1,4 + P + P11PPk+1k+1PP55)for 1<=k<=3)for 1<=k<=3

= min(m= min(m1111 + m + m2424 + P + P11PP22PP5 5 , m, m1212 + m + m3434 + P + P11PP33PP5 5 , m, m1313 + m + m4444 + +

PP11PP44PP5 5 ))

= min( 72+2x5x2, 30+42+ 2x3x2, 72+2x7x2)= min( 72+2x5x2, 30+42+ 2x3x2, 72+2x7x2)

= min( 72+20, 30+42+12, 72 + 28) = min( 92, 84, 100) = 84= min( 72+20, 30+42+12, 72 + 28) = min( 92, 84, 100) = 84

Page 27: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.
Page 28: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

LOOKUP-CHAIN(LOOKUP-CHAIN(pp,,ii,,jj))

1.1. if if mm[[ii,,jj]<]< then return then return mm[[ii,,jj]]

2.2. if if ii==jj then then mm[[ii,,jj] ] 00

3.3. else for else for kkii to to jj-1-1

4.4. do do qq LOOKUP-CHAIN( LOOKUP-CHAIN(pp,,ii,,kk)+ )+

5.5. LOOKUP-CHAIN(LOOKUP-CHAIN(pp,,kk+1,+1,jj)+)+ppii-1-1ppkkppjj

6.6. if if qq< < mm[[ii,,jj] then ] then mm[[ii,,jj] ] qq

7.7. return return mm[[ii,,jj]]

3..33..3

1..31..33..43..41..21..22..42..41..11..1 4..44..4

2..32..33..43..42..22..2 4..44..4 2..22..21..11..1 4..44..43..33..3 1..11..1 2..32..3 1..21..2 3..33..3

1..41..4

2..22..24..44..43..33..3 2..22..2 3..33..3 1..11..1 2..22..2

Memoized Matrix ChainMemoized Matrix Chain

Page 29: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

For a DP to be applicable an optmztn prbl must have: For a DP to be applicable an optmztn prbl must have: 1.1. Optimal substructureOptimal substructure

• An optimal solution to the problem contains within it optimal An optimal solution to the problem contains within it optimal solutions to subproblems.solutions to subproblems.

2.2. Overlapping (dependencies) subproblemsOverlapping (dependencies) subproblems

• The space of subproblems must be small; i.e., the same subproblems The space of subproblems must be small; i.e., the same subproblems are encountered over and over.are encountered over and over.

DP step3. DP step3. Memoization: Memoization: T(n)=O(n3), PSpace(n)=(n2)o A top-down variation of dynamic programmingA top-down variation of dynamic programmingo Idea: remember the solution to subproblems as they are solved in the Idea: remember the solution to subproblems as they are solved in the

simple recursive algorithm but may be quite costly simple recursive algorithm but may be quite costly o DP is considered better when all subproblems must be calculated, because DP is considered better when all subproblems must be calculated, because

there is no overhead for recursion. Lookup-Chain(p, i, j)there is no overhead for recursion. Lookup-Chain(p, i, j)LookUp-TableLookUp-Table(p,i,j)(p,i,j) Initialize Initialize all all m[m[ii,,jj]] to to ifif m[ m[ii,,jj] < ] < then then returnreturn mm[[ii,,jj] ] ifif i i ==jj then then m[i,j] m[i,j] 0 0 else for else for k k ← 1 ← 1 toto j j − 1− 1 qq ←← LookUp-Table LookUp-Table ((p,p, ii , k, k))

+ LookUp-Table (+ LookUp-Table (p,p, kk+1+1 , j, j) + ) + pp[[ii-1]-1] p[p[kk]] pp[[jj]] ifif qq < < mm[[i, ji, j] ] then then mm[[i, ji, j] ] ← ← qq

returnreturn mm[[i, ji, j] ]

Page 30: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Elements of DPElements of DPo Optimal substructureOptimal substructure

– A problem exhibits A problem exhibits optimal substructureoptimal substructure if an optimal solution to the problem if an optimal solution to the problem contains within its optimal solution to subproblems.contains within its optimal solution to subproblems.

o Overlapping subproblemsOverlapping subproblems

• When a recursive algorithm revisits the same problem over and over again, When a recursive algorithm revisits the same problem over and over again, that is the optimization problem has that is the optimization problem has overlapping subproblemsoverlapping subproblems..

o SubtletiesSubtleties

• Better to not assume that optimal substructure applies in general. Two Better to not assume that optimal substructure applies in general. Two examples in a directed graph examples in a directed graph GG = ( = (VV, , EE) and vertices ) and vertices uu, , vv VV..

• Unweighted shortest path:Unweighted shortest path:

– Find a path from Find a path from uu to to vv consisting of the fewest edges. Good for Dynamic consisting of the fewest edges. Good for Dynamic programming.programming.

• Unweighted longest simple path:Unweighted longest simple path:

– Find a simple path from Find a simple path from u u to to vv consisting of the most edges. Not good for consisting of the most edges. Not good for Dynamic programming.Dynamic programming.

Page 31: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

o The running timeThe running time of a dynamic-programming algorithm of a dynamic-programming algorithm depends on the product of two factors.depends on the product of two factors.

• The number of subproblems overallThe number of subproblems overall * the number of choices * the number of choices for for each subproblemeach subproblem. = . = Sum of entire choices. Sum of entire choices.

• Assembly line scheduling Assembly line scheduling – ΘΘ((nn) subproblems · 2 choices = ) subproblems · 2 choices = ΘΘ((nn) )

• Matrix chain multiplication Matrix chain multiplication – ΘΘ((nn22) ) subproblems subproblems · (· (nn-1) choices = -1) choices = OO((nn33))

Page 32: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Principle of Optimality (Optimal Substructure)Principle of Optimality (Optimal Substructure)The principle of optimality applies to a The principle of optimality applies to a problem problem (not an algorithm)(not an algorithm)A large number of optimization problems satisfy this principle.A large number of optimization problems satisfy this principle.

Principle of optimality: Given an optimal sequence of decisions or choices, Principle of optimality: Given an optimal sequence of decisions or choices, each subsequence must also be optimal.each subsequence must also be optimal.

Principle of optimality - shortest path problemPrinciple of optimality - shortest path problemProblem: Given a graph Problem: Given a graph GG and vertices and vertices ss and and tt, find a shortest path in , find a shortest path in GG from from

ss to to tt

Theorem: A subpath P’ (from s’ to t’) of a shortest path P is a shortest path 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’. from s’ to t’ of the subgraph G’ induced by P’. SubpathsSubpaths are paths that are paths that start or end at an intermediate vertex of P.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 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 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 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.assumption that P is a shortest path from s to t.

Page 33: Dynamic Programming Dr. M. Sakalli, Marmara University Matrix Chain Problem Assembly-line scheduling Elements of dynamic programming Picture reference.

Principle of Optimality Principle of Optimality

P’ must be a shortest path from c to e in G’, otherwise P cannot be a shortest 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.path from a to e in G.

P’={(c.d), (d,e)}

P={ (a,b), (b,c) (c.d), (d,e)}

a b c d

f

e

G’

G

3 1

3

5

6

107

13

A

C

D

B

Longest A to B

Longest C to BLongest A to C

Problem: What is the longest simple route between City A and B? Problem: What is the longest simple route between City A and B?

• Simple = never visit the same spot twice.Simple = never visit the same spot twice.

o The longest simple route (solid line) has city C as an intermediate city.The longest simple route (solid line) has city C as an intermediate city.

o Does not consist of the longest simple route from A to C and the longest 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 Optimalitysimple route from C to B. Therefore does not satisfy the Principle of Optimality