Search code examples
python-3.xalgorithmdynamic-programming

Array partition using dynamic programming


What modification should I apply to the dynamic programming implementation of two partition problem to solve the following task:

You are given an array of positive integers as input, denote it C. The program should decide if it is possible to partition the array into two equal sum subsequences. You are allowed to remove some elements from the array, but not all, in order to make such partition feasible.

Example:

Suppose the input is 4 5 11 17 9. Two partition is possible if we remove 11 and 17. My question is what adjustments to my two partition implementation I should make to determine if two partition is possible (may or may not require to remove some elements) or output that two partition is impossible even if some elements are removed. The program should run in O(sum^2 * C) time.

Here is my two partition implementation in Python:

def two_partition(C):
    n = len(C)
    s = sum(C)
    
    if s % 2 != 0: return False
    
    T = [[False for _ in range(n + 1)] for _ in range(s//2 + 1)]
    for i in range(n + 1): T[0][i] = True
    
    for i in range(1, s//2 + 1):
        for j in range(1, n + 1):
            T[i][j] = T[i][j-1]
            if i >= C[j-1]:
                T[i][j] = T[i][j] or T[i-C[j-1]][j-1]
                   
    return T[s // 2][n]

For example, with input [2, 3, 1] the expected output is {2,1} and {3}. This makes it is possible to partition the array into two equal subsets. We don't need to remove any elements in this case. In the above example of 4 5 11 17 9, the two subsets are possible if we remove 11 and 17. This leaves {4,5} and {9}.


Solution

  • Create a 3 dimensional array indexed by sum of 1st partition, sum of 2nd partition and number of elements. T[i][j][k] if only true if it's possible to have two disjoint subsets with sum i & j respectively within the first k elements.

    To calculate it, you need to consider three possibilities for each element. Either it's present in first set, or second set, or it's removed entirely. Doing this in a loop for each combination of sum possible generates the required array in O(sum ^ 2 * C).

    To find the answer to your question, all you need to check is that there is some sum i such that T[i][i][n] is true. This implies that there are two distinct subsets both of which sum to i, as required by the question.

    If you need to find the actual subsets, doing so is easy using a simple backtracking function. Just check which of the three possibilities are possible in the back_track functions and recurse.

    Here's a sample implementation:

    def back_track(T, C, s1, s2, i):
        if s1 == 0 and s2 == 0: return [], []
        if T[s1][s2][i-1]:
            return back_track(T, C, s1, s2, i-1)
        elif s1 >= C[i-1] and T[s1 - C[i-1]][s2][i-1]:
            a, b = back_track(T, C, s1 - C[i-1], s2, i-1)
            return ([C[i-1]] + a, b)
        else:
            a, b = back_track(T, C, s1, s2 - C[i-1], i-1)
            return (a, [C[i-1]] + b)
    
    def two_partition(C):
        n = len(C)
        s = sum(C)
    
        T = [[[False for _ in range(n + 1)] for _ in range(s//2 + 1)] for _ in range(s // 2 + 1)]
        for i in range(n + 1): T[0][0][i] = True
    
        for s1 in range(0, s//2 + 1):
            for s2 in range(0, s//2 + 1):
                for j in range(1, n + 1):
                    T[s1][s2][j] = T[s1][s2][j-1]
                    if s1 >= C[j-1]:
                        T[s1][s2][j] = T[s1][s2][j] or T[s1-C[j-1]][s2][j-1]
                    if s2 >= C[j-1]:
                        T[s1][s2][j] = T[s1][s2][j] or T[s1][s2-C[j-1]][j-1]
        for i in range(1, s//2 + 1):
            if T[i][i][n]:
                return back_track(T, C, i, i, n)
        return False
    
    print(two_partition([4, 5, 11, 9]))
    print(two_partition([2, 3, 1]))
    print(two_partition([2, 3, 7]))