here is another dynamic programming question (Vazirani ch6)
Consider the following 3-PARTITION problem. Given integers a1...an, we want to determine whether it is possible to partition of {1...n} into three disjoint subsets I, J, K such that
sum(I) = sum(J) = sum(K) = 1/3*sum(ALL)
For example, for input (1; 2; 3; 4; 4; 5; 8) the answer is yes, because there is the partition (1; 8), (4; 5), (2; 3; 4). On the other hand, for input (2; 2; 3; 5) the answer is no. Devise and analyze a dynamic programming algorithm for 3-PARTITION that runs in time poly- nomial in n and (Sum a_i)
How can I solve this problem? I know 2-partition but still can't solve it
It's easy to generalize 2-sets solution for 3-sets case.
In original version, you create array of boolean
sumswheresums[i]tells whether sumican be reached with numbers from the set, or not. Then, once array is created, you just see ifsums[TOTAL/2]istrueor not.Since you said you know old version already, I'll describe only difference between them.
In 3-partition case, you keep array of boolean
sums, wheresums[i][j]tells whether first set can have sumiand second - sumj. Then, once array is created, you just see ifsums[TOTAL/3][TOTAL/3]istrueor not.If original complexity is
O(TOTAL*n), here it'sO(TOTAL^2*n).It may not be polynomial in the strictest sense of the word, but then original version isn't strictly polynomial too :)