Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complexity when generating all combinations

Tags:

Interview questions where I start with "this might be solved by generating all possible combinations for the array elements" are usually meant to let me find something better.

Anyway I would like to add "I would definitely prefer another solution since this is O(X)".. the question is: what is the O(X) complexity of generating all combinations for a given set?

I know that there are n! / (n-k)!k! combinations (binomial coefficients), but how to get the big-O notation from that?

like image 699
Albert Avatar asked Jun 29 '15 16:06

Albert


People also ask

What is the time complexity of combinations?

The time complexity is equal to the number of combinations there are. In this case it is n choose k . – Nikos M.

What is the time complexity of combination sum?

The time complexity is O(k * 2^N) , where k is the average length of each possible solution. Copying such a possible solution list takes O(k) time. Solution Space: Since each element is used only once (use it or not), intuitively we would say the size of the solution space is at most 2^N .

What is the time complexity of finding the combinations nCr If the input array is of size n?

The time complexity analysis for Combination If an array of size n is given, then it will take O(n2) time to perform the task.


2 Answers

First, there is nothing wrong with using O(n! / (n-k)!k!) - or any other function f(n) as O(f(n)), but I believe you are looking for a simpler solution that still holds the same set.

If you are willing to look at the size of the subset k as constant, then for k <= n - k:

n! / ((n - k)! k!) = ((n - k + 1) (n - k + 2) (n - k + 3) ... n ) / k!  

But the above is actually (n ^ k + O(n ^ (k - 1))) / k!, which is in O(n ^ k)

Similarly, if n - k < k, you get O(n ^ (n - k))

Which gives us O(n ^ min{k, n - k})

like image 90
amit Avatar answered Oct 14 '22 04:10

amit


I know this is an old question, but it comes up as a top hit on google, and IMHO has an incorrectly marked accepted answer.

C(n,k) = n Choose k = n! / ( (n-k)! * k!)

The above function represents the number of sets of k-element that can be made from a set of n-element. Purely from a logical reasoning perspective, C(n, k) has to be smaller than

∑ C(n,k) ∀ k ∊ (1..n).

as this expression represents the power-set. In English, the above expression represents: add C(n,k) for all k from 1 to n. We know this to have 2 ^ n elements.

So, C(n, k) has an upper bound of 2 ^ n which is definitely smaller than n ^ k for any n, k > 3, and k < n.

So to answer your question C(n, k) has an upper bound of 2 ^ n for sure, but don't know if there is a tighter upper bound that describes it better.

like image 39
Vikhram Avatar answered Oct 14 '22 03:10

Vikhram