2019-10-05 01:14:13 -04:00
|
|
|
"""
|
2025-05-23 00:07:43 +03:00
|
|
|
The sum-of-subsets problem states that a set of non-negative integers, and a
|
2024-03-13 07:52:41 +01:00
|
|
|
value M, determine all possible subsets of the given set whose summation sum
|
|
|
|
|
equal to given M.
|
2019-07-05 14:18:36 +05:30
|
|
|
|
2024-03-13 07:52:41 +01:00
|
|
|
Summation of the chosen numbers must be equal to given number M and one number
|
|
|
|
|
can be used only once.
|
2019-10-05 01:14:13 -04:00
|
|
|
"""
|
2024-03-13 07:52:41 +01:00
|
|
|
|
2019-10-05 01:14:13 -04:00
|
|
|
|
2025-05-23 00:07:43 +03:00
|
|
|
def generate_sum_of_subsets_solutions(nums: list[int], max_sum: int) -> list[list[int]]:
|
|
|
|
|
"""
|
|
|
|
|
The main function. For list of numbers 'nums' find the subsets with sum
|
|
|
|
|
equal to 'max_sum'
|
|
|
|
|
|
|
|
|
|
>>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=9)
|
|
|
|
|
[[3, 4, 2], [4, 5]]
|
|
|
|
|
>>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=3)
|
|
|
|
|
[[3]]
|
|
|
|
|
>>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=1)
|
|
|
|
|
[]
|
|
|
|
|
"""
|
2019-07-05 14:18:36 +05:30
|
|
|
|
2021-09-07 13:37:03 +02:00
|
|
|
result: list[list[int]] = []
|
|
|
|
|
path: list[int] = []
|
2019-10-05 01:14:13 -04:00
|
|
|
num_index = 0
|
|
|
|
|
remaining_nums_sum = sum(nums)
|
|
|
|
|
create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
2020-11-29 18:19:50 +02:00
|
|
|
def create_state_space_tree(
|
2021-09-07 13:37:03 +02:00
|
|
|
nums: list[int],
|
2020-11-29 18:19:50 +02:00
|
|
|
max_sum: int,
|
|
|
|
|
num_index: int,
|
2021-09-07 13:37:03 +02:00
|
|
|
path: list[int],
|
|
|
|
|
result: list[list[int]],
|
2020-11-29 18:19:50 +02:00
|
|
|
remaining_nums_sum: int,
|
|
|
|
|
) -> None:
|
2019-10-05 01:14:13 -04:00
|
|
|
"""
|
2020-09-10 16:31:26 +08:00
|
|
|
Creates a state space tree to iterate through each branch using DFS.
|
|
|
|
|
It terminates the branching of a node when any of the two conditions
|
|
|
|
|
given below satisfy.
|
|
|
|
|
This algorithm follows depth-fist-search and backtracks when the node is not
|
|
|
|
|
branchable.
|
2019-07-05 14:18:36 +05:30
|
|
|
|
2025-05-23 00:07:43 +03:00
|
|
|
>>> path = []
|
|
|
|
|
>>> result = []
|
|
|
|
|
>>> create_state_space_tree(
|
|
|
|
|
... nums=[1],
|
|
|
|
|
... max_sum=1,
|
|
|
|
|
... num_index=0,
|
|
|
|
|
... path=path,
|
|
|
|
|
... result=result,
|
|
|
|
|
... remaining_nums_sum=1)
|
|
|
|
|
>>> path
|
|
|
|
|
[]
|
|
|
|
|
>>> result
|
|
|
|
|
[[1]]
|
2020-09-10 16:31:26 +08:00
|
|
|
"""
|
2025-05-23 00:07:43 +03:00
|
|
|
|
2019-10-05 01:14:13 -04:00
|
|
|
if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum:
|
|
|
|
|
return
|
|
|
|
|
if sum(path) == max_sum:
|
|
|
|
|
result.append(path)
|
|
|
|
|
return
|
2022-10-13 17:03:06 +01:00
|
|
|
for index in range(num_index, len(nums)):
|
2019-10-05 01:14:13 -04:00
|
|
|
create_state_space_tree(
|
|
|
|
|
nums,
|
|
|
|
|
max_sum,
|
2022-10-13 17:03:06 +01:00
|
|
|
index + 1,
|
2023-03-01 17:23:33 +01:00
|
|
|
[*path, nums[index]],
|
2019-10-05 01:14:13 -04:00
|
|
|
result,
|
2022-10-13 17:03:06 +01:00
|
|
|
remaining_nums_sum - nums[index],
|
2019-10-05 01:14:13 -04:00
|
|
|
)
|
|
|
|
|
|
2019-07-05 14:18:36 +05:30
|
|
|
|
2025-05-23 00:07:43 +03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
import doctest
|
2019-07-05 14:18:36 +05:30
|
|
|
|
2025-05-23 00:07:43 +03:00
|
|
|
doctest.testmod()
|