0% found this document useful (0 votes)
7 views14 pages

Dynamic Programming Applications Explained

Dynamic programming paradigm matrix chain multiplication

Uploaded by

Shrishu Ranjan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views14 pages

Dynamic Programming Applications Explained

Dynamic programming paradigm matrix chain multiplication

Uploaded by

Shrishu Ranjan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DYNAMIC PROGRAMMING

By

Prasanta K. Jana, IEEE Senior Member

Department of Computer Science and Engineering


Indian Institute of Technology (ISM), Dhanbad
E-mail: prasantajana@[Link]
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

Common questions

Powered by AI

The concept of overlapping subproblems in dynamic programming is crucial for both matrix chain multiplication and assembly line scheduling problems. In matrix chain multiplication, overlapping subproblems occur as different matrix multiplication orders share common subparts, which are solved and stored for reuse, minimizing redundant calculations. Similarly, in assembly line scheduling, the fastest completion times for stations on each line often depend on common previous stations and transfers across lines, leading to overlap. In both cases, dynamic programming leverages this overlap by solving each subproblem once, storing its solution, and using these stored results to build up the final solution efficiently, reducing the overall computational burden .

Insights gained from dynamic programming applications in real-world problems such as matrix chain multiplication and assembly line scheduling include the importance of identifying repeated subproblems and the efficiency gains from optimizing their solutions. Understanding the patterns of overlap in subproblems allows for the transformation of exponential problems into polynomial time solutions. Both applications highlight the need for structured problem decomposition and solution storage, enabling us to design algorithms that are both optimal and efficient. Furthermore, they demonstrate how thoughtful analysis of problem structure can lead to significant reductions in computational resource requirements, illustrating the broader impact of leveraging DP in diverse domains .

The recursive approach differs from the dynamic programming approach primarily in how subproblem solutions are reused. In solving optimization problems like assembly line scheduling, a pure recursive method calculates results independently each time they are needed, often leading to exponential time complexity due to repeated calculations. However, dynamic programming builds on recursion by storing and reusing solutions of overlapping subproblems through memoization or tabulation. This allows solutions to be computed once and efficiently retrieved, thereby reducing the time complexity to O(n) as with the assembly line scheduling problem .

The optimal arrangement of matrix multiplications dramatically affects computation time by minimizing the number of scalar multiplications required. The dynamic programming strategy used involves defining subproblems where m[i, j] represents the minimum number of scalar multiplications needed to compute the product of matrices from Ai to Aj. By filling a DP table with these values and backtracking, one can determine the order of multiplication that has the lowest cost. This process turns a potentially exponential time complexity problem into one that runs in cubic time, O(n^3).

The optimal parenthesization problem in matrix multiplication emphasizes computational efficiency by demonstrating how strategic planning of operations can drastically reduce computational workload. By determining the order in which multiplication of matrices should be performed to minimize total scalar multiplications, dynamic programming transforms a potentially high-cost brute force approach into a manageable one. Instead of evaluating all possible parenthesizations (which grows exponentially), dynamic programming divides the problem into smaller subproblems and solves each once, storing results for future use. This significantly enhances efficiency, with time complexity reduced to O(n^3).

Memoization plays a critical role in solving dynamic programming problems by storing the results of expensive function calls and reusing these results when the same inputs occur again. This improves computational efficiency by ensuring that each subproblem is only solved once and its result recorded, eliminating redundant calculations and thereby reducing the time complexity. In contrast to recomputing results multiple times as seen in naive approaches, memoization ensures that the solutions to subproblems can be retrieved in constant time .

The recurrence relation in assembly line scheduling illustrates that both station processing times (ai,j) and transfer times (ti,j) significantly impact assembly efficiency. The recurrence relations indicate that the optimal path through the assembly line for station j can either continue from the previous station on the same line (f1[j] or f2[j]), or switch from the other line to utilize a possibly faster path. This decision is based on comparing the sum of the station's processing time with the minimum cumulative time through all previous stations, considering potential line switches. This dynamic allocation ensures minimized total assembly time by intelligently choosing the faster routes through both processing and transfer options .

The fundamental distinction between Dynamic Programming (DP) and Divide and Conquer (D&C) lies in the nature of their subproblems. In D&C, subproblems are independent, allowing each to be solved separately and their solutions combined for the overall problem solution. This often involves redundant computations. In contrast, DP addresses subproblems that overlap, meaning common subproblems can recur multiple times. DP solves each subproblem only once and stores its result to avoid recomputation, thus optimizing the process by reducing redundant calculations .

The recursive formulation of subproblems in the matrix chain multiplication problem involves defining m[i, j] as the minimum number of scalar multiplications needed to compute the matrix product Ai...Aj. This is achieved by iterating through possible partition points to split the matrices into smaller products, computing m[i, k] + m[k+1, j] + pi-1 * pk * pj for each k. Using memoization, these calculations are stored in a table when first encountered, allowing the solution for each subproblem to be quickly retrieved and preventing redundant calculations. This approach enables the transformation of the problem from an exponential complexity to polynomial time, specifically O(n^3), as solutions to individual subproblems are reused in composing the final answer .

The main components in calculating the optimal assembly sequence include the time required at each station (ai,j), transfer time between lines (ti,j), entrance time (ei), and exit time (xi) for each line. Dynamic programming facilitates this calculation by using recursive relations to determine the fastest possible time (fi[j]) to get a chassis through each station, either directly using the previous station on the same line or via a transfer from the other line. The optimal solution is the minimum time to move through the final station on either line and exit, calculated by comparing exit times (min(f1[n] + x1, f2[n] + x2)). This structured approach efficiently finds the solution in linear time, O(n).

You might also like