I have a list of numbers. I also have a certain sum. The sum is made from a few numbers from my list (I may/may not know how many numbers it's made from). Is there a fast algorithm to get a list of possible numbers? Written in Python would be great, but pseudo-code's good too. (I can't yet read anything other than Python :P )
Example
list = [1,2,3,10]
sum = 12
result = [2,10]
NOTE: I do know of Algorithm to find which numbers from a list of size n sum to another number (but I cannot read C# and I'm unable to check if it works for my needs. I'm on Linux and I tried using Mono but I get errors and I can't figure out how to work C# :(
AND I do know of algorithm to sum up a list of numbers for all combinations (but it seems to be fairly inefficient. I don't need all combinations.)
This problem reduces to the 0-1 Knapsack Problem, where you are trying to find a set with an exact sum. The solution depends on the constraints, in the general case this problem is NP-Complete.
However, if the maximum search sum (let's call it
S
) is not too high, then you can solve the problem using dynamic programming. I will explain it using a recursive function and memoization, which is easier to understand than a bottom-up approach.Let's code a function
f(v, i, S)
, such that it returns the number of subsets inv[i:]
that sums exactly toS
. To solve it recursively, first we have to analyze the base (i.e.:v[i:]
is empty):S == 0: The only subset of
[]
has sum 0, so it is a valid subset. Because of this, the function should return 1.S != 0: As the only subset of
[]
has sum 0, there is not a valid subset. Because of this, the function should return 0.Then, let's analyze the recursive case (i.e.:
v[i:]
is not empty). There are two choices: include the numberv[i]
in the current subset, or not include it. If we includev[i]
, then we are looking subsets that have sumS - v[i]
, otherwise, we are still looking for subsets with sumS
. The functionf
might be implemented in the following way:By checking
f(v, 0, S) > 0
, you can know if there is a solution to your problem. However, this code is too slow, each recursive call spawns two new calls, which leads to an O(2^n) algorithm. Now, we can apply memoization to make it run in time O(n*S), which is faster ifS
is not too big:Now, it is possible to code a function
g
that returns one subset that sumsS
. To do this, it is enough to add elements only if there is at least one solution including them:Disclaimer: This solution says there are two subsets of [10, 10] that sums 10. This is because it assumes that the first ten is different to the second ten. The algorithm can be fixed to assume that both tens are equal (and thus answer one), but that is a bit more complicated.
So, the logic is to reverse sort the numbers,and suppose the list of numbers is l and sum to be formed is s.
then, we go through this loop and a number is selected from l in order and let say it is i . there are 2 possible cases either i is the part of sum or not. So, we assume that i is part of solution and then the problem reduces to l being
l[l.index(i+1):]
and s being s-i so, if our function is a(l,s) then we calla(l[l.index(i+1):] ,s-i)
. and if i is not a part of s then we have to form s froml[l.index(i+1):]
list. So it is similar in both the cases , only change is if i is part of s, then s=s-i and otherwise s=s only.now to reduce the problem such that in case numbers in l are greater than s we remove them to reduce the complexity until l is empty and in that case the numbers which are selected are not a part of our solution and we return false.
and in case l has only 1 element left then either it can be part of s then we return true or it is not then we return false and loop will go through other number.
note in the loop if have used b..but b is our list only.and i have rounded wherever it is possible, so that we should not get wrong answer due to floating point calculations in python.
this solution works fast.more fast than one explained above. However this works for positive numbers only. However also it works good if there is a solution only otherwise it takes to much time to get out of loops.
an example run is like this lets say
just to give a comparison which i ran on my computer which is not so good. using
and
s=2000
my loop ran 1018 times and 31 ms.
and previous code loop ran 3415587 times and took somewhere near 16 seconds.
however in case a solution does not exist my code ran more than few minutes so i stopped it and previous code ran near around 17 ms only and previous code works with negative numbers also.
so i thing some improvements can be done.
This python code do what you asked, it will print the unique pair of numbers whose sum is equal to the target variable.
I have found an answer which has run-time complexity O(n) and space complexity about O(2n), where n is the length of the list.
The answer satisfies the following constraints:
List can contain duplicates, e.g. [1,1,1,2,3] and you want to find pairs sum to 2
List can contain both positive and negative integers
The code is as below, and followed by the explanation:
Iterate through through the dict, this time is to find how many pairs do we have. We need to consider 3 conditions:
3.1 The key is just half of the sum and this key occurs more than once in the list, e.g. list is [1,1,1], sum is 2. We treat this special condition as what the code does.
3.2 The key is just half of the sum and this key occurs only once in the list, we skip this condition.
3.3 For other cases that key is not half of the sum, just multiply the its value with another key's value where these two keys sum to the given value. E.g. If sum is 6, we multiply temp[1] and temp[5], temp[2] and temp[4], etc... (I didn't list cases where numbers are negative, but idea is the same.)
The most complex step is step 3, which involves searching the dictionary, but as searching the dictionary is usually fast, nearly constant complexity. (Although worst case is O(n), but should not happen for integer keys.) Thus, with assuming the searching is constant complexity, the total complexity is O(n) as we only iterate the list many times separately.
Advice for a better solution is welcomed :)