2

Normally, when dealing with combinations, the Big-O complexity seems to be O(n choose k). In this algorithm, I am generating all the combinations within the array that match the target sum:

def combos(candidates,start, target):
    if target == 0:
        return [[]]
    res = []
    for i in range(start,len(candidates)):
        for c in combos(candidates, i+1, target-candidates[i]):
            res.append([candidates[i]]+ c)
    return res


print combos([2,1,10,5,6,4], 10)
# [[1, 5, 4], [10], [6, 4]]

I am having a hard time determining Big-O here, is this a O(n choose t) algorithm? If not, what is it and why?

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
ApathyBear
  • 9,057
  • 14
  • 56
  • 90

2 Answers2

2

If the point is to give the worst-cast complexity in terms of the set size, n, then it is Θ(2n). Given any set, if the target sum is large enough, you'll end up enumerating all the possible subsets of the set. This is Θ(2n), as can be seen in two ways:

  • Each item can be chosen or not.

  • It is your Θ(n choose k), just summed up over all k.


A more refined bound would take into account both n and the target sum t. In this case, following the reasoning of the 2nd point above, then if all elements (and target sum) are positive integers, then the complexity will be the sum of Θ(n choose k) for k summing only up to t.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
1

Your algorithm is at least O(2^n) and I believe it is O(n * 2^n). Here is an explanation.

In your algorithm you have to generate all possible combinations of a set (except of an empty set). So this is:

enter image description here

O(2^n) at least. Now for every combinations you have to sum them up. Some sets are of length 1 on is of length n, but majority of them would be somewhere of length n/2. So I believe that your complexity is close to the O(n * 2^n).

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • I don't follow the last summation step above, so out of curiosity (since I probably just don't understand), what sum rule are you using here? (Something in the context of sets and permutations?). The closest I can think of is the finite series `sum_{i=0}^n 2^i = 2^{n+1}-1` or `sum_{i=0}^n a^i = (a^{n+1}-1)/(a-1)` (`a>1`), but none of those really apply to yield the result of your sum above. Thanks! – dfrib Jan 19 '16 at 13:08
  • @dfri are you speaking about `sum(C_n^i)`? If so than this is a sum of binomials. Check https://en.wikipedia.org/wiki/Binomial_coefficient right after `Series involving binomial coefficients`. – Salvador Dali Jan 19 '16 at 18:30