Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Lecture 6 - Dynamic Programming

Uploaded by

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

Lecture 6 - Dynamic Programming

Uploaded by

sorupr5
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

CSE 2201: Algorithm Analysis and Design

Dynamic Programming
Md Mehrab Hossain Opi
Introduction 2
• Like the divide-and-conquer method, DP solves problems by combining
the solutions to subproblems.
• Then what’s the difference?
• D&C partitioned a problem into disjoint subproblems.
• Solve the subproblems recursively, and then combine their solutions to
solve the original problem.
• Merge Sort / Quick Sort.

• But DP is used when the subproblems overlap.


• Subproblems share subproblems.
• Fibonacci Sequence.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Introduction 3
• A DP algorithm solves each subsubproblem just once.
• Saves its answer in a table.
• Thereby avoiding the work of recomputing the answer every time it solves
each subsubproblem.

• DP typically applies to optimization problems.


• Such problems can have many possible solutions.
• Each solution has a value, and you want to find a solution with the
optimal value.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Introduction 4
• To develop a DP algorithm, four steps are followed
1. Characterize the structure of an optimal solution.
2. Recursively define the value of an optimal solution.
3. Compute the value of an optimal solution, typically in a bottom-up
fashion.
4. Construct an optimal solution from computed information.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting 5
• Imagine you have a long rod that you want to sell.
• You can sell the rod as a whole or cut it into smaller pieces to make
more money.
• Each length of rod has a different price, so some pieces might be more
valuable per unit length than others.
• The goal is to figure out the best way to cut the rod (or not cut it at all)
to get the most money possible.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting 6
• You will be given the length of the rod .
• And an array , where is the price of rod with length
• You need to find the maximum amount of money you can earn by
optimally cutting the rod and selling the pieces.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Example 7
• Consider the length of the rod is meter.
• And the array P is as follows:

1 2 3 4
1 5 8 9

• What is the maximum amount of money you can earn?

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Example 8
• You can cut the rod in 8 different way.

• The maximum profit is 10.


• How can we solve it?

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem 9
• You can cut a rod with length in different ways.
• For each position , you can choose to cut or not cut.

• Suppose, in an optimal decomposition, we will cut the rod in k pieces.

• And we will get the revenue

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Another Example 10
• Consider the price array :
1 2 3 4 5 6 7 8 9 10
1 5 8 9 10 17 17 20 24 30

• Now we want to calculate revenue for each length from 1 to 10.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Another Example 11
1 2 3 4 5 6 7 8 9 10
1 5 8 9 10 17 17 20 24 30

• For , from solution 1 = 1 (no cuts)


• For from solution 2 = 2 (no cuts)
• For from solution 3 = 3 (no cuts)
• For from solution 4 = 2+2
• For from solution 5 = 2+3
• For from solution 6 = 6
• For from solution 7 = 1+6 or 7 = 2+2+3
• For from solution 8 = 2+6
• For from solution 9 = 3+6
• For from solution 10 = 10
CSE 2201: Algorithm Analysis and Design December 21, 2024
Rod Cutting Problem 12
• We can express the values for in terms of optimal revenues from
shorter rods:

• , corresponds to making no cuts at all.


• The other arguments correspond to the maximum revenue obtained by
making an initial cut of the rod
• into two pieces of size
• for each .
• These two pieces are cut further to get .

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem 13
• Solve the main problem by breaking it into smaller, similar
subproblems.
• After the first cut, each resulting piece becomes its own rod-cutting
problem.
• The optimal solution combines the best outcomes from each of these
subproblems to maximize revenue.
• The rod-cutting problem has optimal substructure
• The overall best solution includes the optimal solutions of smaller, related
subproblems, solved independently.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem 14
• A simpler recursive structure for the rod-cutting problem
• Cut a first piece of length from the left, leaving a remainder of length
• Only the remainder can be further divided, while the first piece remains
intact.
• Each decomposition of a rod of length is viewed as
• A first piece with size and revenue
• Followed by a remainder of size with corresponding revenue.

• Special case (no cuts): First piece is the full rod sizeand revenue ;
remainder is zero with revenue
• This approach simplifies the solution to include only one related
subproblem (the remainder) instead of two.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Recursive Solution 15
• The function takes an array and an integer as input.
• Returns the maximum revenue Algorithm Cut-Rod(p, n){
possible for a rod of length n. if then return 0;
• No revenue is possible for . q =
for to
• Otherwise, we check all possible way
divide the rod in two parts. return q
• And call the function for the remainder }
part.

• What will be the complexity of this program?

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Recursive Solution 16
• When n = 4 we will get the following recursion tree

• How many nodes are there?


• nodes and leaves.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Dynamic Programming 17
• Instead of saving the subproblems repeatedly, arrange for each
problem to be solved only once.
• Saves solutions of subproblems the first time they are solved to reuse
later, instead of recomputing.
• Involves extra memory to store solutions, representing a time-memory
trade-off.
• Reduces the rod-cutting problem from an exponential-time algorithm to
.
• Dynamic programming is efficient when:
• The number of distinct subproblems is polynomial in the input size.
• Each subproblem can be solved in polynomial time.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Dynamic Programming 18
• Two ways to implement the solution
• Top-Down with Memoization
• Bottom-up Method

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Top-Down Approach 19
• Write a recursive procedure to solve the problem naturally.
• Modify the procedure to save each subproblem's result (typically in an
array or hash table).
• Before computing, check if the subproblem has been solved previously
• If yes, return the saved result to save computation.
• If no, compute the result, then save it for future use.

• We say that the recursive procedure has been memorized


• it “remembers” what results it has computed previously.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Top-Down Approach 20
• We need a table to store the calculated values.
• In initialization we will declare an array toMemoized-Cut-Rod(p, n)
store all the values.
let be a new array
• To indicate a value is not calculated yet,
for to
we initialize each value with .

return Memoized-
Aux(p,n,r)

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Top-Down Approach 21

Memoized-Aux(p, n, r)
if //already have a solution
return r[n]
if n==0 then q = 0
else{
q =
for to n // is the position of first cut
q = max{q,p[i]+Memoized-Aux(p, n-i, r)
r[n] = q // remember the solution value for length n
return q
CSE 2201: Algorithm Analysis and Design December 21, 2024
Rod Cutting Problem – Bottom-Up Approach 22
• Relies on a natural order of subproblems, where each subproblem
depends only on smaller ones.
• Solve subproblems in ascending order of size, starting with the
smallest.
• Store each solution when it is first solved.
• For each subproblem, all smaller prerequisite subproblems are already
solved and stored.
• Ensures each subproblem is solved only once, with prerequisites ready
when needed.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Bottom-Up Approach 23

Bottom-Up-Cut-Rod(p, n, r)
let r[0:n] be a new array // will remember solution values
in r.
r[0] = 0
for j = 1 to n // for increasing rod length j
q =
for to j // i is the position of the first cut
q = max{q, p[i]+r[j-i]}
r[j] = q // remember the solution for length j
return r[n]

CSE 2201: Algorithm Analysis and Design December 21, 2024


Rod Cutting Problem – Dynamic Programming 24
• Both bottom-up and top-down approaches have the same asymptotic
running time:
• Bottom-Up Approach
• Has a doubly nested loop structure, where the inner loop forms an
arithmetic series.
• Results in total iterations.

• Top-Down (Memoized) Approach:


• Each recursive call to a previously solved subproblem returns immediately,
so each subproblem is solved only once.
• Solving a subproblem of size 𝑛 requires 𝑛 iterations in the loop.
• Across all recursive calls, these iterations also form an arithmetic series,
resulting in total iterations, similar to the bottom-up method.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Subproblem Graphs 25
• When you solve a DP problem, you need to understand the set of
subproblems involved and how subproblems depend on one another.
• We will use subproblem graph to visualize these information.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Subproblem Graphs 26
• A directed graph, one vertex for each subproblem.
• A directed edge from x to y exists if
determining an optimal solution for subproblem x involves
directly considering an optimal solution for subproblem y.
• Think of it as a reduced or collapsed version of the recursion
tree.
• The size of the subproblem graph can help you determine
the running time of the DP algorithm.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Reconstructing Solution 27
• So far, we have seen how to find the optimal value of a rod cutting
problem.
• How do we find the list of piece size?
• We need to extend our solution to store the choice we made.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Reconstructing Solution 28
Bottom-Up-Cut-Rod-Extended(p, n)
let r[0:n] and s[1:n] be a new array
r[0] = 0
for j = 1 to n // for increasing rod length j
q =
for to j // i is the position of the first cut
if q < p[i]+r[j-i]
q = max{q, p[i]+r[j-i]}
s[j] = i
r[j] = q // remember the solution for length j
return r and s

CSE 2201: Algorithm Analysis and Design December 21, 2024


Reconstructing Solution 29
Print-Cut-Rod-Solution(p, n)
(r, s) = Bottom-Up-Cut-Rod-Extended(p,n)
while n>0
print s[n]
n = n – s[n]

CSE 2201: Algorithm Analysis and Design December 21, 2024


Matrix-Chain Multiplication 30
• Suppose you have n matrices
• Matrices aren’t necessarily square.

• You need to compute the product of these n matrices.

• We will use the traditional method for multiplying the rectangular


matrices.
• Our target is to minimize the number of scalar multiplications.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Matrix-Chain Multiplication 31
• Suppose we have three matrix:

• If we perform we need scalar multiplication.


• T has a dimension of
• Then to perform we need multiplication.
• In total we need 7500 multiplication operations.
• If we put parenthesis to indicate the order of multiplication we can
write

CSE 2201: Algorithm Analysis and Design December 21, 2024


Matrix-Chain Multiplication 32
• Now consider this order .
• How many multiplication operations do we need?
• For we need multiplications.
• For we need another operations.
• In total 75000 operations.
• So, our first order of multiplication was 10x times faster.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Matrix-Chain Multiplication 33
• The problem can be stated as
Given a chain of n matrices, where, for , matrix has dimension , fully
parenthesize the product in a way that minimizes the number of scalar
multiplications. The input is the sequence of dimensions .

CSE 2201: Algorithm Analysis and Design December 21, 2024


Matrix-Chain Multiplication 34
• If the chain of matrices is then you can fully parenthesize the product
in five distinct ways:

CSE 2201: Algorithm Analysis and Design December 21, 2024


Counting the Number of Parenthesizations 35
• Let, be the number of possible number of parenthesizations of a
sequence of n matrices.
• When , the sequence consists of just one matrix.
• Only 1 possible way.

• When , a fully parenthesized matrix product is the product of two fully


parenthesized matrix subproduct.
• The split between the two subproducts may occur between the kth and
(k+1)th matrices for any .

• We get the recurrence relation

CSE 2201: Algorithm Analysis and Design December 21, 2024


Applying Dynamic Programming 36
• Let’s see how we can apply the four steps
1. Characterize the structure of an optimal solution.
2. Recursively define the value of an optimal solution.
3. Compute the value of an optimal solution.
4. Construct an optimal solution from computed information.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Applying Dynamic Programming 37
• Step 1: The structure of an optimal parenthesization
• Find the optical substructure
• Use it to construct an optimal solution to the problem from optimal
solutions to subproblems.
• Before finding the optimal solution let’s see some notations.
• Let, denote the matrix that results from evaluating the product .
• The product must split between and for some integer in the range .
• For some value of k, first compute and .
• And then multiply them together to produce .

CSE 2201: Algorithm Analysis and Design December 21, 2024


Applying Dynamic Programming 38
• The cost of parenthesizing this way is the cost of
• Computing matrix
• Computing matrix
• Plus, the cost of multiplying them.

• Let’s see the optimal substructure now.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Applying Dynamic Programming 39
• To optimally parenthesize , we must split the product between and .
• The optimal presentation of must also be optimal.
• If not, substituting a better presentation would lower the total cost.

• Similarly, must also be optimally parenthesized.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Applying Dynamic Programming 40
• Any non-trivial matrix-chain multiplication solutions requires splitting
into two subproblems.
• To find optimal solution:
• Split the problem at every possible position k between and .
• Solve each subproblems optimally and combine the solutions.

• Check all possible splits to ensure the minimum multiplication cost.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Applying Dynamic Programming 41
• Step 2: A recursive solution
• A subproblem is finding the minimum cost of parenthesizing .
• Input dimensions are and each subproblem is specified by a pair .
• Let represent the minimum scalar multiplications needed for
• The goal is to compute .

CSE 2201: Algorithm Analysis and Design December 21, 2024


Applying Dynamic Programming 42
• What will be the base case for our recursive solution?
• If , the subproblem is trivial (just one matrix), so .
• For
• Suppose the optimal solution splits between and .
• Then .
• Since the best k is unknown, try all possible k values between and .

• The recursive definition can be written as:

• Additionally, we will maintain array s, such that stores the index k that
gives the minimum cost for splitting
• This array will be used to reconstruct the solution.

CSE 2201: Algorithm Analysis and Design December 21, 2024


Applying Dynamic Programming 43
• A recursive algorithm would take exponential time.
• Similar to rod-cutting problem.

• If you look closely there are only unique subproblem for choice of
and .
• Overlapping subproblems make DP suitable for this problem.
• Let’s see the bottom-up approach.

CSE 2201: Algorithm Analysis and Design December 21, 2024


References 44
• Introduction to Algorithms, 4th ed.
– Cormen, Leiserson, Rivest, Stein.

CSE 2201: Algorithm Analysis and Design December 21, 2024


CSE 2201: Algorithm Analysis and Design December 21, 2024 45

Thank You.

You might also like