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

Dynamic Programming 6

Dynamic programming is an optimization technique for solving problems with optimal substructure and overlapping subproblems. It works by storing results of subproblems to avoid recomputing them. Examples include the Fibonacci series, shortest path algorithms like Bellman-Ford and Floyd-Warshall, and matrix chain multiplication. For matrix chain multiplication, the dynamic programming approach finds the minimum number of multiplications needed to multiply matrices by iteratively computing pairwise multiplications and building up to longer sequences.

Uploaded by

avikantk94
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)
26 views

Dynamic Programming 6

Dynamic programming is an optimization technique for solving problems with optimal substructure and overlapping subproblems. It works by storing results of subproblems to avoid recomputing them. Examples include the Fibonacci series, shortest path algorithms like Bellman-Ford and Floyd-Warshall, and matrix chain multiplication. For matrix chain multiplication, the dynamic programming approach finds the minimum number of multiplications needed to multiply matrices by iteratively computing pairwise multiplications and building up to longer sequences.

Uploaded by

avikantk94
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
You are on page 1/ 18

Dynamic Programming

November 2022

CSE201
Dynamic Programming

Characteristics
Dynamic programming is applicable when the problem satisfies the
following properties.
1 Optimal substructure: The problem can be broken into
sub-problems and the optimal solutions of the subproblems can be
combined to obtain the optimal solution to the main problem.
2 Overlapping sub-problem: The solutions to the smaller subproblems
can be reused repeatedly to obtain the solutions to the larger
sub-problems rather than always generating new subproblems.

(November 2022) Dynamic Programming CSE201 2 / 18


Dynamic programming

Dynamic Programming is mainly an optimization over recursion where


the recursive solution makes repeated calls to the same input.
The idea is to store the results of subproblems so that we do not
have to re-compute them when needed later
(Memoization/Tabulation).
We shall discuss Tabulation based bottom-up schemes in this course.

(November 2022) Dynamic Programming CSE201 3 / 18


Fibonacci series

The Fibonacci numbers are the numbers in the following integer


sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, · · ·
Input: 1, Output: 1
Input: 9, Output: 34
Input: 10, Output: 55
The series is defined by the recurrence relation

Fn = Fn−1 + Fn−2

The Nth Fibonacci Number can be found using the recurrence


relation:
if n = 0, then return 0
if n = 1, then return 1
for n > 1, return Fn−1 + Fn−2

(November 2022) Dynamic Programming CSE201 4 / 18


Fibonacci series

Fibo(n)
Begin
if n ≤ 1 then
Return n;
else
Return Fibo(n-1) + Fibo(n-2);
endif
End

(November 2022) Dynamic Programming CSE201 5 / 18


Fibonacci series

In the recursive solution to finding the nth Fibonacci series, we need


to recompute some of the subproblems multiple times.
This can be avoided if the results are computed once and stored for
future reference.
Fibo(n)
Begin
fibo[n+1] // Array of n+1 size
fibo[0]=0, fibo[1]=1
for (i = 2; i <= n; i++)
fibo[i] = fibo[i - 1] +
fibo[i - 2];
endfor
return fibo[n]
The intermediate values are stored in the array, so recomputation is
not required.

(November 2022) Dynamic Programming CSE201 6 / 18


Single-source shortest path algorithm (Bellman-Ford
algorithm)
It begins with a starting vertex and calculates the distances between other vertices that a
single edge can reach.
It then iteratively searches for a shorter path with two or more edges.
Optimal substructure: Each step uses the shortest path estimates with fewer edges to
obtain optimal solutions with a larger number of edges.
Overlapping subproblem: Precomputed solutions with fewer edges are stored and reused
to compute solutions with a larger number of edges.

(November 2022) Dynamic Programming CSE201 7 / 18


All pair shortest path (Floyd Warshall algorithm)
Initialize the solution matrix where each entry corresponds to the distances between vertex
pairs that a single edge can reach.
Iteratively pick all vertices one by one and update all shortest paths which include the
picked vertex as an intermediate vertex in the shortest path.
Optimal substructure: The optimal path estimates through already picked vertices can be
used to construct the optimal path estimates through the later vertices.
Overlapping subproblem: The path estimates through already picked vertices are stored
and reused to construct the path estimates through the later vertices.

(November 2022) Dynamic Programming CSE201 8 / 18


Matrix Chain Multiplication

Definition
Given the dimension of a sequence of matrices, the task is to find the
most efficient way to multiply these matrices together such that the
total number of element multiplications is minimum.

Example
Input: arr[] = {40, 20, 30, 10, 30}
There are 4 matrices of dimensions A40×20 , B20×30 , C30×10 , D10×30 .
The minimum number of multiplications is obtained by putting
parenthesis in the following way: (A(BC))D
The minimum is 20*30*10 + 40*20*10 + 40*10*30 = 26000

(November 2022) Dynamic Programming CSE201 9 / 18


Dynamic Programming solution

Solution idea
1 Initialization: Find the number of multiplications required to
multiply two adjacent matrices.
2 Iteration: Use these values to find the minimum multiplication
required for matrices in a range of length 3 and further use those
values for ranges with higher lengths.
3 Termination: Build on the answer in this manner till the range
becomes [0, N-1].

(November 2022) Dynamic Programming CSE201 10 / 18


Algorithm steps

arr[]: Array to store the dimensions of the matrices (dimension of the


i th matrix is (arr[i-1] * arr[i]))
dp[][]: A temporary array to store the intermediate results.

(November 2022) Dynamic Programming CSE201 11 / 18


Algorithm

(November 2022) Dynamic Programming CSE201 12 / 18


Example
arr[] = [4, 10, 3, 12, 20, 7]
Initilize dp[i][i]=0
Initialize rest of the entries of dp[][] to infinity(∞).

m14x10
m210x3
m33x12
m412x20
m520x7
(November 2022) Dynamic Programming CSE201 13 / 18
Example

m (1,2) = m1 x m2 = 4 x 10 x 3 = 120
m (2, 3) = m2 x m3 = 10 x 3 x 12 = 360
m (3, 4) = m3 x m4 = 3 x 12 x 20 = 720
m (4,5) = m4 x m5 = = 12 x 20 x 7 = 1680

(November 2022) Dynamic Programming CSE201 14 / 18


Example

m[1,3] = m1 m2 m3

m[2,4] = m2 m3 m4

m[3,5] = m3 m4 m5

(November 2022) Dynamic Programming CSE201 15 / 18


Example

m[1,4] = m1 m2 m3 m4

m[2,5] = m2 m3 m4 m5

(November 2022) Dynamic Programming CSE201 16 / 18


Example

m[1,5] = m1 m2 m3 m4 m5

(November 2022) Dynamic Programming CSE201 17 / 18


Example (Traceback)

(November 2022) Dynamic Programming CSE201 18 / 18

You might also like