Algo Ramblings: Balanced Partitions
Suppose you are given an array of n integers {a1,……, an} between 0 and M. Give an algorithm for dividing these integers into two sets x and y such that the difference of the sum of the integers in each set, is minimized.
Complexity to split an array into two balanced partitions is O(N^2 *K) with space complexity of O(N^2), where K will be the max value array can have.
http://algorithmsandme.blogspot.com/2014/04/balanced-partition-problem.html
In table generated above, table[i][j] gives true if there is subset of integers from 0 to jth index which add up to sum i.
Let's half it and find a subset which has sum as close as possible to this half. That will give us other subset which is least greater than half of sum of all elements of array and that will be minimum difference possible between two subsets. :) Our expression would be
http://www.cs.cornell.edu/~wdtseng/icpc/notes/dp3.pdf
After the algorithm has finished, T[x] will be true if and only if there is a subset of the numbers that has sum x.
Once we have that, we can simply return T[N/2]. If it is true, then there is a subset that adds up to
half the total sum.
To build this array, we set every entry to false to start with. Then we set T[0] to true.
boolean[] sol = new boolean[sum / 2 + 1];
sol[0] = true;// empty array
for (int i : array) {
for (int j = sum / 2; j >= i; j--) {
if (sol[j - i])
sol[j] = true;
}
}
int halfsumcloser = sum / 2;
for (; !sol[halfsumcloser]; halfsumcloser--)
;
System.out.println("The Minimum sum After partioning the array is :"
+ ((sum - halfsumcloser) - halfsumcloser));
Partition: optimized version
bool partition( vector< int > C ) {
// compute the total sum and sort C
int n = C.size();
int N = 0;
for( int i = 0; i < n; i++ ) N += C[i];
sort( C.begin(), C.end() );
// initialize the table
T[0] = true;
for( int i = 1; i <= N; i++ ) T[i] = false;
int R = 0; // rightmost true entry
// process the numbers one by one
for( int i = 0; i < n; i++ ) {
for( int j = R; j >= 0; j )
if( T[j] ) T[j + C[i]] = true;
R = min( N / 2, R + C[i] );
}
return T[N / 2];
}
MinimumSubarrayDifference.java
public static int minimizeDifference(List<Integer> A) {
int sum = 0;
for (int a : A) {
sum += a;
}
Set<Integer> isOk = new HashSet<>();
isOk.add(0);
for (int item : A) {
for (int v = sum / 2; v >= item; --v) {
if (isOk.contains(v - item)) {
isOk.add(v);
}
}
}
// Finds the first i from middle where isOk[i] == true.
for (int i = sum / 2; i > 0; --i) {
if (isOk.contains(i)) {
return (sum - i) - i;
}
}
return sum; // One thief takes all.
}
Similar problem at Dynamic Programming | Set 18 (Partition problem)
Read full article from Algo Ramblings: Balanced Partitions:
Suppose you are given an array of n integers {a1,……, an} between 0 and M. Give an algorithm for dividing these integers into two sets x and y such that the difference of the sum of the integers in each set, is minimized.
Complexity to split an array into two balanced partitions is O(N^2 *K) with space complexity of O(N^2), where K will be the max value array can have.
http://algorithmsandme.blogspot.com/2014/04/balanced-partition-problem.html
In table generated above, table[i][j] gives true if there is subset of integers from 0 to jth index which add up to sum i.
Let's half it and find a subset which has sum as close as possible to this half. That will give us other subset which is least greater than half of sum of all elements of array and that will be minimum difference possible between two subsets. :) Our expression would be
After the algorithm has finished, T[x] will be true if and only if there is a subset of the numbers that has sum x.
Once we have that, we can simply return T[N/2]. If it is true, then there is a subset that adds up to
half the total sum.
To build this array, we set every entry to false to start with. Then we set T[0] to true.
boolean[] sol = new boolean[sum / 2 + 1];
sol[0] = true;// empty array
for (int i : array) {
for (int j = sum / 2; j >= i; j--) {
if (sol[j - i])
sol[j] = true;
}
}
int halfsumcloser = sum / 2;
for (; !sol[halfsumcloser]; halfsumcloser--)
;
System.out.println("The Minimum sum After partioning the array is :"
+ ((sum - halfsumcloser) - halfsumcloser));
Partition: optimized version
bool partition( vector< int > C ) {
// compute the total sum and sort C
int n = C.size();
int N = 0;
for( int i = 0; i < n; i++ ) N += C[i];
sort( C.begin(), C.end() );
// initialize the table
T[0] = true;
for( int i = 1; i <= N; i++ ) T[i] = false;
int R = 0; // rightmost true entry
// process the numbers one by one
for( int i = 0; i < n; i++ ) {
for( int j = R; j >= 0; j )
if( T[j] ) T[j + C[i]] = true;
R = min( N / 2, R + C[i] );
}
return T[N / 2];
}
EPI Problem: Divide the spoils fairly
MinimumSubarrayDifference.javapublic static int minimizeDifference(List<Integer> A) {
int sum = 0;
for (int a : A) {
sum += a;
}
Set<Integer> isOk = new HashSet<>();
isOk.add(0);
for (int item : A) {
for (int v = sum / 2; v >= item; --v) {
if (isOk.contains(v - item)) {
isOk.add(v);
}
}
}
// Finds the first i from middle where isOk[i] == true.
for (int i = sum / 2; i > 0; --i) {
if (isOk.contains(i)) {
return (sum - i) - i;
}
}
return sum; // One thief takes all.
}
Similar problem at Dynamic Programming | Set 18 (Partition problem)
Read full article from Algo Ramblings: Balanced Partitions: