0% found this document useful (0 votes)
2 views

Dynamic Programming

Dynamic programming paradigm matrix chain multiplication

Uploaded by

Shrishu Ranjan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Dynamic Programming

Dynamic programming paradigm matrix chain multiplication

Uploaded by

Shrishu Ranjan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

DYNAMIC PROGRAMMING

By

Prasanta K. Jana, IEEE Senior Member

Department of Computer Science and Engineering


Indian Institute of Technology (ISM), Dhanbad
E-mail: [email protected]
DYNAMIC PROGRAMMING

 What is Dynamic Programming (DP)?

 What are its application areas?

 Difference Between D&C and DP

 Similar to D&C, DP solves a problem by combining solutions of


subproblems
 Subproblems are independent for D&C so more works as it solves
common subproblems
 Subproblems are not independent rather overlapped in DP. So, DP
solves every subproblem just once and saves its answer in a table,
thereby avoiding recomputation of the answer every time the
subproblem is encountered.
MATRIX CHAIN MULTIPLICATION

Consider < A1 A2 A3 > chain where [A1]10 x 100, [A2]100 x 5, [A3]5 x 50.
 ((A1 A2) A3) requires 10 x 100 x 5 + 10 x 5 x 50 = 7500 scalar
multiplications.
 (A1(A2 A3)) requires 100 x 5 x 50 + 10 x 100 x 50 = 75000 scalar
multiplications.
So ((A1 A2) A3) is 10 times faster than (A1(A2 A3)).

Problem Statement: Given a chain < A1, A2, A2, …, An > of n matrices,
where Ai has dimension pi-1pi, for i = 1…, n, fully parenthesize the
product in a way that minimizes the number of scalar multiplications.

Brute force approach:

P(n)  Number of Alternative parenthesizations. Then,

1 , 𝑖𝑓 𝑛 = 1
P(n) = { 𝑛
∑𝑘=0 𝑃(𝑘)𝑃(𝑛 − 𝑘) , 𝑖𝑓 𝑛 >= 2

P(n) = C(n-1) is the solution of this recurrence equation where,


1
C(n) = (2𝑛
𝑛
)
𝑛+1

And C(n) = Ω (4n / n3/2)


Let Ai…j denote Ai A2 …Aj

Let m[i, j] be the minimum number of scalar multiplications needed to


compute the product Ai…j.

Time Complexity: O(n3)


An Illustration:
To be initially called with Print_Optimal-Parents(S, 1, n)

Hence the solution is ((A1(A2A3))((A4A5)A6))


Optimal Triangulation:
Exercise 1:
Consider the following matrix-chain multiplication: A17×6 A26×3 A33×10
A410×5 A55×8. Apply Dynamic programming (DP) to generate the DP
tables and compute the optimal sequence of parenthesization. Show each
step of the computation.
Solution:
ASSEMBLY LINE SCHEDULING PROBLEM
Background:
An automobile company produces a car in a factory that has two assembly lines,
shown in the following Figure.
An automobile chassis enters each assembly line, has parts added to it at a number
of stations, and a finished auto exits at the end of the line.

An Instance:
Problem Statement:

Si, j : jth station on line i ( where i is 1 or 2)


ti, j : time to transfer a chassis form line i gone through Si, j
ai, j : assembly time required at station Si, j
ei : time for the chassis to enter assembly line i
xi : an exit time for the completed auto to exit assembly line i
 S1, j performs same function as S2, j
 Time required at each station varies even between stations at the same
position on different lines
The problem is to determine which stations from line 1 and which stations from
line 2 so that total assembly time is minimized.
A recursive solution:
• Let fi [ j] denote the fastest possible time to get a chassis from the starting
point through station Si, j.
• Let f ∗ be the fastest time to get a chassis all the way through the factory.
• Then the chassis has to get all the way through station n on either line 1 or
line 2 and then to the factory exit.
• So, we have: f ∗ = min(f1[n] + x1, f2[n] + x2)
For j = 1
f1[1] = e1 + a1,1 And f2[1] = e2 + a2,1
For j = 2
f1[ 2] = f1[1] + a1,2 or f1[ 2] = f2[1] + t2, 1 + a1,2
And
f2[ 2] = f2[1] + a2,2 or f2[ 2] = f1[1] + t1, 1 + a2, 2

Thus, the fastest way through station S1, j is either

 The fastest way through station S1, j−1 and then directly through station S1, j,
OR
 The fastest way through station S2, j−1, a transfer from line 2 to line 1, and
then through station S1, j.

Using symmetric reasoning, the fastest way through station S2, j is either

 The fastest way through station S2, j−1 and then directly through station S2, j,
OR
 The fastest way through station S1, j−1, a transfer from line 1 to line 2, and
then through station S2, j.

Thus in General:
For j = 2, 3, . . . , n, we have,

f1[ j ] = f1[ j − 1] + a1, j or f1[ j ] = f2[ j − 1] + t2, j−1 + a1, j


And
f2[ j ] = f2[ j − 1] + a2, j or f2[ j ] = f1[ j − 1] + t1, j−1 + a2, j

Therefore, Recursive Equations:


Let li [ j] be the line number, 1 or 2, whose station j − 1 is used in a fastest way
through station Si, j.

Time Complexity: O(n)


An Illustration:

Procedure to print stations in decreasing order of station number

You might also like