Partition and subset sum
🌍 In real life. Two flatmates share this week's chores, which take 3, 1, 4 and 2 hours. That is 10 hours in all, so the split is fair only if one of them gets chores adding up to exactly 5. Is there such a choice? Here 1 + 4 works. With chores of 2 and 4 hours (6 in all), no choice makes 3, so no fair split exists.
The idea in one sentence: subset sum asks whether some of the numbers add up to exactly a target; dp[i][s] is True when the first i numbers can make s, either without number i (dp[i − 1][s]) or with it (dp[i − 1][s − x]), which is the 0/1 knapsack of Lesson 58.3 with True and False instead of values.
Its words:
- subset: some of the numbers, each used at most once (none or all are allowed too)
- subset sum: does some subset add up to exactly the target?
- partition: can the numbers be split into two groups with equal sums? It is subset sum with half the total as the target
Step by step:
- make a
(n + 1) × (target + 1)table of False;dp[0][0] = True(choosing nothing makes 0) - for number i, value x, at every sum s:
dp[i][s] = dp[i − 1][s] or (x <= s and dp[i − 1][s − x]) - the answer is
dp[n][target]; walking back up tells which numbers were used
Watch the table answer for the chores, target 5:
When no subset works, every path to the corner stays False, and there is nothing to walk back:
Why it is right. A subset of the first i numbers that makes s either leaves number i out, and then the first i − 1 numbers make s, or uses it, and then the first i − 1 numbers make s − x. The two cells above cover both, so nothing is missed, and since each row reads only the row above, no number is used twice.
What it costs, by counting. One cell per number and sum: 100 numbers and a target of 5,000 fill 100 × 5,001, about 500,000 cells. Trying every subset instead means 2 to the power 100 of them. Like the knapsack, the table grows with the target, so a target in the billions is too big for it. In Big-O: O(n × target) time and memory, or O(target) memory with one row.
def can_make(nums, target):
n = len(nums)
dp = [[False] * (target + 1) for _ in range(n + 1)] # dp[i][s]: can the first i numbers make s?
dp[0][0] = True # choosing nothing makes 0
for i in range(1, n + 1):
x = nums[i - 1]
for s in range(target + 1):
dp[i][s] = dp[i - 1][s] # without number i
if x <= s and dp[i - 1][s - x]: # or with it
dp[i][s] = True
return dp[n][target]
def can_split(nums):
total = sum(nums)
if total % 2 == 1: # an odd total can't be halved
return False
return can_make(nums, total // 2) # one group making half leaves the other half
print(can_make([3, 1, 4, 2], 5), can_make([2, 4], 3))
print(can_split([3, 1, 4, 2]), can_split([2, 4]))Mistakes and edge cases.
- greedy, biggest first: for
[3, 3, 4]and 6 it takes the 4 and is stuck, though 3 + 3 works - forgetting
dp[0][0] = True: then nothing can ever be made - an odd total: it can't be split in two equal halves; check before building the table
- a target of 0: always True, by choosing nothing
- using a number twice: reading
dp[i][s − x](this row) would let 3 make 6 on its own as 3 + 3
Quiz
What does this print?
def can_make(nums, target): n = len(nums) dp = [[False] * (target + 1) for _ in range(n + 1)] # dp[i][s]: can the first i numbers make s? dp[0][0] = True # choosing nothing makes 0 for i in range(1, n + 1): x = nums[i - 1] for s in range(target + 1): dp[i][s] = dp[i - 1][s] # without number i if x <= s and dp[i - 1][s - x]: # or with it dp[i][s] = True return dp[n][target] print(can_make([3, 3, 4], 6), can_make([3, 3, 4], 5))How does partition become subset sum?
- Ask whether some numbers make exactly half the total
- Sort and split in the middle
- Ask whether the total is even
What is dp[0][s] for s > 0?
- False: with no numbers only 0 can be made
- True
- s
What does this print?
def can_make(nums, target): n = len(nums) dp = [[False] * (target + 1) for _ in range(n + 1)] # dp[i][s]: can the first i numbers make s? dp[0][0] = True # choosing nothing makes 0 for i in range(1, n + 1): x = nums[i - 1] for s in range(target + 1): dp[i][s] = dp[i - 1][s] # without number i if x <= s and dp[i - 1][s - x]: # or with it dp[i][s] = True return dp[n][target] def can_split(nums): total = sum(nums) if total % 2 == 1: # an odd total can't be halved return False return can_make(nums, total // 2) # one group making half leaves the other half print(can_split([1, 5, 11, 5]), can_split([1, 2, 3, 5]))can_make([3], 6) answers True. Which line is wrong?
def can_make(nums, target): n = len(nums) dp = [[False] * (target + 1) for _ in range(n + 1)] dp[0][0] = True for i in range(1, n + 1): x = nums[i - 1] for s in range(target + 1): dp[i][s] = dp[i - 1][s] if x <= s and dp[i][s - x]: dp[i][s] = True return dp[n][target]
Coding tasks
Write
can_make(nums, target): returnTrueif some of the numbers (each used at most once) add up to exactlytarget, elseFalse. All numbers are positive.Write
can_split(nums): returnTrueif the numbers can be split into two groups with equal sums, elseFalse. Writecan_makein your answer too.