The sum-of-subsets problem states that a set of non-negative integers, and a value M, determine all possible subsets of the given set whose summation sum equal to given M.
Summation of the chosen numbers must be equal to given number M and one number can be used only once.
|
Creates a state space tree to iterate through each branch using DFS. |
|
The main function. For list of numbers 'nums' find the subsets with sum |
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.
>>> 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]]
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)
[]