L31: Dynamic Programming (L3) : Some Problems and Discussions
L31: Dynamic Programming (L3) : Some Problems and Discussions
(L3)
Some Problems and Discussions
DP : Dice Throw DP-3
https://www.geeksforgeeks.org/dice-throw-dp-30/
• Given n dice each with m faces, numbered from 1 to m,
find the number of ways to get sum X. X is the
summation of values on each face when all the dice are
thrown
• Input: m = 6, n = 3, x = 12
Output: 25
Explanation: There are 25 total ways to get the Sum 12 using
3 dices with faces from 1 to 6.
• Input: m = 2, n = 3, x = 6
Output: 1
Explanation: There is only 1 way to get the Sum 6 using 3
dices with faces from 1 to 2. All the dices will have to land on
2. 2
Systematically DP solution
1. Plan a brute-force/recursive solution
2. Make a recursive definition of the problem
3
Systematically DP solution
1. Plan ----
2. Make a recursive ------
3. Find whether it has overlapping sub-problems. If yes, it can be
solved using DP (“DP” means bottom-up DP )
4. Memoized solution generation
5. From memoized: generation of DP Solution (in bottom up fashion)
4
5
DP : Max Product Cutting DP-36
www.geeksforgeeks.org/maximum-product-cutting-dp-36/
Given a rope of length n meters, cut the rope in different
parts of integer lengths in a way that maximizes product of
lengths of all parts. You must make at least one cut.
Assume that the length of rope is more than 2 meters.
Input: n = 2
Output: 1 (Maximum obtainable product is 1*1)
Input: n = 3
Output: 2 (Maximum obtainable product is 1*2)
Input: n = 4
Output: 4 (Maximum obtainable product is 2*2
Input: n = 5
Output: 6 (Maximum obtainable product is 2*3)
Input: n = 10
6
Output: 36 (Maximum obtainable product is 3*3*4)
Systematically DP solution
This problem is similar to Rod Cutting Problem. We can get the
maximum product by making a cut at different positions and
comparing the values obtained after a cut. We can recursively call
the same function for a piece obtained after a cut.
7
Problem Understanding & Optimal Structure
• Let maxProd(n) be the maximum product for a rope of
length n.
• maxProd(n) can be written as following.
maxProd(n) = max(i*(n-i), maxProdRec(n-i)*i) for all i
in {1, 2, 3 .. n-1}
8
9
Partition a Set into Two Subsets of Equal Sum DP-18
https://www.geeksforgeeks.org/ partition-problem-dp-18/
Given an array, task is to check if it can be partitioned into
two parts such that the sum of elements in both parts is the
same.
Input: arr[] = [1, 5, 11, 5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11]
Input: arr[] = [1, 5, 3]
Output: false
Explanation: The array cannot be partitioned into equal sum sets
10
Problem Understanding & Optimal Structure
• Calculate the sum of the array. If the sum is odd, this
can’t be two subsets with an equal sum, so return false.
• If the sum of the array elements is even, calculate sum/2 &
find a subset of the array with a sum = orig_sum/2.
• Let isSubsetSum(arr, n, sum/2) be the function that returns
true if there is a subset of arr[0..n-1] with sum =orig_sum/2
The isSubsetSum problem : two subproblems:
• isSubsetSum() without considering last element (reducing n
to n-1)
• isSubsetSum() considering the last element (reducing
sum/2 by arr[n-1] and n to n-1)
• isSubsetSum ( ) = isSubsetSum (arr, n-1, sum/2) OR
isSubsetSum (arr, n-1, sum/2 – arr[n-1])
11
Systematically DP solution
1. Plan a brute-force/recursive solution
2. Make a recursive definition of the problem
12
Recursive Definition
• For last element decide: take in one set & noTake in other
• If the value of the last element > remaining sum then last
element is guaranteed to be ignored.
• If last element <= remaining sum, take or noTake
• if (arr[n-1] > sum)
return isSubsetSum(n-1, arr, sum);
else
return isSubsetSum(n-1, arr, sum) || isSubsetSum(n-1,
arr, sum - arr[n-1]);
• Make boundary checks and write recursive fn
• Then memoized & then bottom-up DP.
13
Bottom-up DP
• 2-d array: dp[n+1][sum+1]//dp[0][..] means size is zero
• Main code:
for (int i = 1; i <= n; i++)
{ for (int j = 1; j <= sum; j++)
{ if (j < arr[i - 1])
{ dp[i][j] = dp[i - 1][j]; }
else
{ dp[i][j] = dp[i - 1][j] || dp[i - 1][j - arr[i - 1]]; }
} }
Dp[0][j]=? (for all j)//size as 0
dp[i][0] = ? (for all i)//sum as zero
14