0% found this document useful (0 votes)
15 views29 pages

lecture-5

The document discusses the Optimal Substructure Property, which states that optimal solutions to problems can be constructed from optimal solutions to subproblems, a key concept in dynamic programming. It specifically examines the 0-1 Knapsack Problem and the Matrix Chain Multiplication problem, detailing their recursive formulations and optimal substructure characteristics. The document also highlights the limitations of greedy approaches and emphasizes the importance of dynamic programming for solving these types of problems efficiently.

Uploaded by

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

lecture-5

The document discusses the Optimal Substructure Property, which states that optimal solutions to problems can be constructed from optimal solutions to subproblems, a key concept in dynamic programming. It specifically examines the 0-1 Knapsack Problem and the Matrix Chain Multiplication problem, detailing their recursive formulations and optimal substructure characteristics. The document also highlights the limitations of greedy approaches and emphasizes the importance of dynamic programming for solving these types of problems efficiently.

Uploaded by

ompatel1233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

BITS, PILANI – K. K.

BIRLA GOA CAMPUS

Design & Analysis of Algorithms


(CS F364)

Lecture No. 5

1
Optimal Substructure Property
• Why does the solution work?
Optimal Substructure Property/ Principle of
Optimality
• The optimal solution to the original problem
incorporates optimal solutions to the subproblems.
This is a hallmark of problems amenable to dynamic
programming.
• Not all problems have this property.
How to prove Optimal Substructure Property?
Generally by Cut-Paste Argument or By Contradiction
Dynamic Programming Algorithm
The dynamic-programming algorithm can be broken into
a sequence of four steps.
1. Characterize the structure of an optimal solution.
Optimal Substructure Property
2. Recursively define the value of an optimal solution.
3. Compute the value of an optimal solution in a
bottom-up fashion.
Overlapping subproblems
4. Construct an optimal solution from computed
information.
(not always necessary)
The 0-1 Knapsack Problem
Given: A set S of n items, with each item i having
wi - a positive “weight”
vi - a positive “benefit”
Goal:
Choose items with maximum total benefit but with weight at
most W.
And we are not allowed to take fractional amounts
In this case, we let T denote the set of items we take
∑ 𝒗𝒊
Objective : maximize 𝒊 ∈𝑻
∑ 𝒘 𝒊 ≤𝑾
Constraint : 𝒊 ∈𝑻
Greedy approach
Possible greedy approach:
Approach1: Pick item with largest value first
Approach2: Pick item with least weight first
Approach3: Pick item with largest value per
weight first
None of the above approaches work
Exercise:
Prove by giving counterexamples.
Recursive Formulation
Very similar to Coin Change Problem
Sk: Set of items numbered 1 to k.
Define B[k,w] to be the best selection from Sk with weight
at most w
 B[k  1, w] if wk  w
B[k , w] 
max{B[k  1, w], B[k  1, w  wk ]  bk } else

Our goal is to find B[n, W], where n is the total number of


items and W is the maximal weight the knapsack can carry.
This does have Optimal Substructure property.
Exercise – Prove Optimal Substructure property.
Optimal Substructure
Consider a most valuable load L where WL ≤ W
Suppose we remove item j from this optimal load L
The remaining load
must be a most valuable load weighing at most

pounds that the thief can take from

That is should be an optimal solution to the


0-1 Knapsack Problem(, )
The 0-1 Knapsack Problem
Exercise
Overlapping Subproblems
Pseudo- code
for w = 0 to W
B[0,w] = 0
for i = 1 to n
B[i,0] = 0 Running Time – O(nW)
for i = 1 to n
for w = 0 to W
if <= w // item i can be part of the solution
if + B[i-1,w-] > B[i-1,w]
B[i,w] = + B[i-1,w- ]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // > w
The 0-1 Knapsack Problem
How to find actual Knapsack Items?
if B[i,k] ≠ B[i−1,k] then
mark the ith item as in the knapsack
i = i−1, k= k-
else
i = i−1 // Assume the ith item is not in the
knapsack
Optimal Substructure- Alternate
Alternate attempt to characterize a sub-problem as follows:
Let Ok be the optimal subset of elements from {I0, I1, …, Ik}.
Example: Let W=20
Item Weight Value
I0 3 10
I1 8 4
I2 9 9
I3 8 11
• The best set of items from {I0, I1, I2} is {I0, I1, I2}
• BUT the best set of items from {I0, I1, I2, I3} is {I0, I2, I3}.
Note
1. Optimal solution, {I0, I2, I3} of S3 for does NOT contain the optimal solution,
{I0, I1, I2} of S2
2. {I0, I2, I3} build's upon the solution, {I0, I2}, which is really the optimal subset of
{I0, I1, I2} with weight 12 or less.
Optimal Substructure- Alternate
Observe
Optimal subset from the elements {I0, I1, …, Ik+1}
may not correspond to the optimal subset of
elements from {I0, I1, …, Ik}
That means, the solution to the optimization problem
for Sk+1 might NOT contain the optimal solution of
problem Sk.
where Sk: Set of items numbered 1 to k.
Subset Sum

Subset Sum Problem (Decision Problem)


Given a set X = {x1, x2, . . . , xn} of positive integers and a
target value T, we wish to find whether there is a subset
of X with sum exactly equal to T.
Subset Sum Problem (Optimization Problem)
Given a set X = {x1, x2, . . . , xn} of positive integers and a
target value T, we wish to find a subset S of X that
maximizewhile keeping≤ T
Subset Sum
OPT(j, t) : the optimal value for subproblems
consisting of the first j integers for every 0 ≤ t ≤ T
OPT(j, t) = max{OPT(j-1, t), xj + OPT(j-1, t - xj)}
Exercise
1. Optimal Substructure Property
2. Overlapping Subproblems
Re Cap - Recursive Formulation
Coin Change Problem
c[i][j] = min{c[i-1][j], 1 + c[i][ j-d[i]]}

0-1 Knapsack problem

 B[k  1, w] if wk  w
B[k , w] 
max{B[k  1, w], B[k  1, w  wk ]  bk } else
Subset Sum problem
OPT(j, t) = max{OPT(j-1, t), xj + OPT(j-1, t - xj)}

Optimal Substructure - Cut Paste Argument


Dynamic Programming
Two Problems
Problem 1: Coin Change Problem
Problem 2: 0 – 1 Knapsack Problem
First Step:
Brute Force Solution
- Exponential Running Time
First Attempt:
“Greedy Strategy”
- Show “Greedy Strategy” fails by giving
counterexample
Observations
Question 1
How many subproblems are used in an optimal solution for
the original problem?
• Coin Change Problem
Which?
Two
• 0-1 Knapsack Problem
Which?
Two
Question 2
How many choices we have in determining which
subproblems to use in an optimal solution?
• Coin ChangeWhy?
Problem
Two
• 0-1 Knapsack Problem?
Matrix Chain Multiplication
Recalling Matrix Multiplication
If A is p x q matrix and B is q x r matrix
then the product C = AB is a p x r matrix given by

where 1≤ i ≤ p and 1≤ j ≤ r
OR
Dot product of ith row of A with jth column of B
Properties
• Matrix multiplication is associative
i.e., A1(A2A3) = (A1A2 )A3
So parenthesization does not change result
• It may appear that the amount of work done
won’t change if you change the parenthesization
of the expression
• But that is not the case!
Example
• Let us use the following example:
– Let A be a 2x10 matrix
– Let B be a 10x50 matrix
– Let C be a 50x20 matrix
• Consider computing A(BC):
Total multiplications = 10000 + 400 = 10400
• Consider computing (AB)C:
Total multiplications = 1000 + 2000 = 3000
Substantial difference in the cost for computing
Matrix Chain Multiplication
• Thus, our goal today is:
• Given a chain of matrices to multiply,
determine the fewest number of multiplications
necessary to compute the product.
• Let dixdi+1 denote the dimensions of matrix Ai.
• Let A = A0 A1 ... An-1
• Let Ni,j denote the minimal number of multiplications
necessary to find the product: Ai Ai+1 ... Aj.
• To determine the minimal number of multiplications
necessary N0,n-1 to find A,
• That is, determine how to parenthisize the multiplications
Matrix Chain Multiplication
1st Approach –
Brute Force
• Given the matrices A1,A2,A3,A4 Assume the dimensions of
A1=d0×d1 etc
• Five possible parenthesizations of these arrays, along with
the number of multiplications:
(A1A2)(A3A4):d0d1d2+d2d3d4+d0d2d4
((A1A2)A3)A4:d0d1d2+d0d2d3+d0d3d4
(A1(A2A3))A4:d1d2d3+d0d1d3+d0d3d4
A1((A2A3)A4):d1d2d3+d1d3d4+d0d1d4
A1(A2(A3A4)):d2d3d4+d1d2d4+d0d1d4
Matrix Chain Multiplication
Questions?
• How many possible parenthesization?
• At least lower bound?
The number of parenthesizations is atleast Ω(2n)
Exercise: Prove
The exact number is given by the recurrence relation

Because, the original product can be split into two parts


In (n-1) places.
Each split is to be parenthesized optimally
Matrix Chain Multiplication
Solution to the recurrence is the famous Catalan
Numbers
T(n) = Ω(4n/3n/2)

Question : Any better approach?


Greedy Approach??
Dynamic Programming
Matrix Chain Multiplication
Step1:
Optimal Substructure Property
If a particular parenthesization of the whole product is
optimal,
then any sub-parenthesization in that product is optimal as
well.
What does it mean?
– If (A (B ((CD) (EF)) ) ) is optimal
– Then (B ((CD) (EF)) ) is optimal as well
How to Prove?
Matrix Chain Multiplication
• Cut - Paste Argument
– Because if it wasn't,
and say ( ((BC) (DE)) F) was better,
then it would also follow that
(A ( ((BC) (DE)) F) ) was better than
(A (B ((CD) (EF)) ) ),
– contradicting its optimality!
Matrix Chain Multiplication
Step 2:
Recursive Formulation
Let M[i,j] represent the minimum number of
multiplications required for matrix product Ai ×⋯× Aj,
For 1≤i≤j<n
High-Level Parenthesization for Ai..j
Notation: Ai..j = Ai x ….x Aj
For any optimal multiplication sequence,
at the last step we are multiplying two matrices
Ai..k and Ak+1..j for some k, i.e.,
Ai..j = (Ai x ….x Ak) (Ak+1 x ….x Aj) = Ai..k Ak+1..j
Matrix Chain Multiplication
Thus,
M[i,j]=M[i,k]+M[k+1,j]+ di-1dkdj
Thus the problem of determining the optimal sequence
of multiplications is broken down to the following
question?
How do we decide where to split the chain?
OR (what is k)?
Answer:
Search all possible values of k & take the minimum of it.
Matrix Chain Multiplication
Therefore,

Step3:
Compute the value of an optimal solution in a
bottom-up fashion

You might also like