Dynamic Programming | Set 18 (Partition problem) - GeeksforGeeks
Partition problem is to determine whether a given set can be partitioned into two subsets such that the sum of elements in both subsets is same.
Following are the two main steps to solve this problem:
1) Calculate sum of the array. If sum is odd, there can not be two subsets with equal sum, so return false.
2) If sum of array elements is even, calculate sum/2 and find a subset of array with sum equal to sum/2.
Following is the recursive property of the second step mentioned above.
Let isSubsetSum(arr, n, sum/2) be the function that returns true if
there is a subset of arr[0..n-1] with sum equal to sum/2
The isSubsetSum problem can be divided into two subproblems
a) isSubsetSum() without considering last element
(reducing n to n-1)
b) isSubsetSum considering the last element
(reducing sum/2 by arr[n-1] and n to n-1)
If any of the above the above subproblems return true, then return true.
isSubsetSum (arr, n, sum/2) = isSubsetSum (arr, n-1, sum/2) ||
isSubsetSum (arr, n-1, sum/2 - arr[n-1])
Dynamic Programming Solution
The problem can be solved using dynamic programming when the sum of the elements is not too big. We can create a 2D array part[][] of size (sum/2)*(n+1). And we can construct the solution in bottom up manner such that every filled entry has following property
part[i][j] = true if a subset of {arr[0], arr[1], ..arr[j-1]} has sum
equal to i, otherwise false
bool
findPartiion (
int
arr[],
int
n)
{
int
sum = 0;
int
i, j;
// Caculcate sun of all elements
for
(i = 0; i < n; i++)
sum += arr[i];
if
(sum%2 != 0)
return
false
;
bool
part[sum/2+1][n+1];
// initialize top row as true
for
(i = 0; i <= n; i++)
part[0][i] =
true
;
// initialize leftmost column, except part[0][0], as 0
for
(i = 1; i <= sum/2; i++)
part[i][0] =
false
;
// Fill the partition table in botton up manner
for
(i = 1; i <= sum/2; i++)
{
for
(j = 1; j <= n; j++)
{
part[i][j] = part[i][j-1];
if
(i >= arr[j-1])
part[i][j] = part[i][j] || part[i - arr[j-1]][j-1];
}
}
return
part[sum/2][n];
}
https://shanzi.gitbooks.io/algorithm-notes/content/problem_solutions/partition_problem.html
This is a NP-Complete problem and similar to knapsack problem, it has a pseudo-polynomial DP solution for integers when the problem's scale is not too large.
Recursive Solution
bool
isSubsetSum (
int
arr[],
int
n,
int
sum)
{
// Base Cases
if
(sum == 0)
return
true
;
if
(n == 0 && sum != 0)
return
false
;
// If last element is greater than sum, then ignore it
if
(arr[n-1] > sum)
return
isSubsetSum (arr, n-1, sum);
/* else, check if sum can be obtained by any of the following
(a) including the last element
(b) excluding the last element
*/
return
isSubsetSum (arr, n-1, sum) || isSubsetSum (arr, n-1, sum-arr[n-1]);
}
bool
findPartiion (
int
arr[],
int
n)
{
// Calculate sum of the elements in array
int
sum = 0;
for
(
int
i = 0; i < n; i++)
sum += arr[i];
// If sum is odd, there cannot be two subsets with equal sum
if
(sum%2 != 0)
return
false
;
// Find if there is subset with sum equal to half of total sum
return
isSubsetSum (arr, n, sum/2);
}
Also refer to Subset sum problem1) Calculate sum of the array. If sum is odd, there can not be two subsets with equal sum, so return false.
2) If sum of array elements is even, calculate sum/2 and find a subset of array with sum equal to sum/2.
The problem can be solved using dynamic programming when the sum of the elements is not too big. We can create a 2D array part[][] of size (sum/2)*(n+1). And we can construct the solution in bottom up manner such that every filled entry has following property
bool
findPartiion (
int
arr[],
int
n)
{
int
sum = 0;
int
i, j;
// Caculcate sun of all elements
for
(i = 0; i < n; i++)
sum += arr[i];
if
(sum%2 != 0)
return
false
;
bool
part[sum/2+1][n+1];
// initialize top row as true
for
(i = 0; i <= n; i++)
part[0][i] =
true
;
// initialize leftmost column, except part[0][0], as 0
for
(i = 1; i <= sum/2; i++)
part[i][0] =
false
;
// Fill the partition table in botton up manner
for
(i = 1; i <= sum/2; i++)
{
for
(j = 1; j <= n; j++)
{
part[i][j] = part[i][j-1];
if
(i >= arr[j-1])
part[i][j] = part[i][j] || part[i - arr[j-1]][j-1];
}
}
return
part[sum/2][n];
}
This is a NP-Complete problem and similar to knapsack problem, it has a pseudo-polynomial DP solution for integers when the problem's scale is not too large.
Recursive Solution
bool
isSubsetSum (
int
arr[],
int
n,
int
sum)
{
// Base Cases
if
(sum == 0)
return
true
;
if
(n == 0 && sum != 0)
return
false
;
// If last element is greater than sum, then ignore it
if
(arr[n-1] > sum)
return
isSubsetSum (arr, n-1, sum);
/* else, check if sum can be obtained by any of the following
(a) including the last element
(b) excluding the last element
*/
return
isSubsetSum (arr, n-1, sum) || isSubsetSum (arr, n-1, sum-arr[n-1]);
}
bool
findPartiion (
int
arr[],
int
n)
{
// Calculate sum of the elements in array
int
sum = 0;
for
(
int
i = 0; i < n; i++)
sum += arr[i];
// If sum is odd, there cannot be two subsets with equal sum
if
(sum%2 != 0)
return
false
;
// Find if there is subset with sum equal to half of total sum
return
isSubsetSum (arr, n, sum/2);
}
Read full article from Dynamic Programming | Set 18 (Partition problem) - GeeksforGeeks