why powerset gives 2^N time complexity?

peter Source

The following is a recursive function for generating powerset

void powerset(int[] items, int s, Stack<Integer> res) {
     System.out.println(res);

     for(int i = s; i < items.length; i++) {
          res.push(items[i]);
          powerset(items, s+1, res);
          res.pop();
     }
}

I don't really understand why this would take O(2^N). Where's that 2 coming from ? Why T(N) = T(N-1) + T(N-2) + T(N-3) + .... + T(1) + T(0) solves to O(2^n). Can someone explains why ?

mathrecursiondiscrete-mathematicsrecurrence

Answers

answered 4 years ago Yongzhi #1

Something like this

T(1)=T(0);

T(2)=T(1)+T(0)=2T(0);

T(3)=T(2)+T(1)+T(0)=2T(2);

Thus we have

T(N)=2T(N-1)=4T(N-2)=... = 2^(N-1)T(1), which is O(2^N)

answered 4 years ago Lrrr #2

I could explain this in several mathematical ways to you the first one :

Consider one element like a each subset have 2 option about a either they have it or not so we must have $ 2^n $ subset and since you need to call function for create every subset you need to call this function $ 2^n $.

another solution:

This solution is with this recursion and it produce you equation , let me define T(0) = 2 for first a set with one element we have T(1) = 2, you just call the function and it ends here. now suppose that for every sets with k < n elements we have this formula

T(k) = T(k-1) + T(k-2) + ... + T(1) + T(0) (I name it * formula)

I want to prove that for k = n this equation is true.

consider every subsets that have the first element ( like what you do at began of your algorithm and you push first element) now we have n-1 elements so it take T(n-1) to find every subsets that have the first element. so far we have :

T(n) = T(n-1) + T(subsets that dont have the first element) (I name it ** formula)

at the end of your for loop you remove the first element now we have every subsets that dont have the first element like I said in (**) and again you have n-1 elements so we have :

T(subsets that dont have the first element) = T(n-1) (I name it * formula)

from formula (*) and (*) we have :

T(subsets that dont have the first element) = T(n-1) = T(n-2) + ... + T(1) + T(0) (I name it **** formula)

And now we have what you want from the first, from formula() and (**) we have :

T(n) = T(n-1) + T(n-2) + ... + T(1) + T(0)

And also we have T(n) = T(n-1) + T(n-1) = 2 * T(n-1) so T(n) = $2^n $

answered 3 years ago Aspen #3

We are doubling the number of operations we do every time we decide to add another element to the original array.

For example, let us say we only have the empty set {}. What happens to the power set if we want to add {a}? We would then have 2 sets: {}, {a}. What if we wanted to add {b}? We would then have 4 sets: {}, {a}, {b}, {ab}.

Notice 2^n also implies a doubling nature. 2^1 = 2, 2^2 = 4, 2^3 = 8, ...

answered 2 years ago Shriganesh Shintre #4

Below is more generic explanation. Note that generating power set is basically generating combinations. (nCr is number of combinations can be made by taking r items from total n items)

formula: nCr = n!/((n-r)! * r!)

Example:Power Set for {1,2,3} is {{}, {1}, {2}, {3}, {1,2}, {2,3}, {1,3} {1,2,3}} = 8 = 2^3

1) 3C0 = #combinations possible taking 0 items from 3 = 3! / ((3-0)! * 0!) = 1
2) 3C1 = #combinations possible taking 1 items from 3 = 3! / ((3-1)! * 1!) = 3
3) 3C2 = #combinations possible taking 2 items from 3 = 3! / ((3-2)! * 2!) = 3
4) 3C3 = #combinations possible taking 3 items from 3 = 3! / ((3-3)! * 3!) = 1

if you add above 4 it comes out 1 + 3 + 3 + 1 = 8 = 2^3. So basically it turns out to be 2^n possible sets in a power set of n items.

So in an algorithm if you are generating a power set with all these combinations, then its going to take time proportional to 2^n. And so the time complexity is 2^n.

answered 2 years ago Keyan P #5

Shriganesh Shintre's answer is pretty good, but you can simplify it even more:

Assume we have a set S:

{a1, a2, ..., aN}

Now we can write a subset s where each item in the set can have the value 1 (included) or 0 (excluded). Now we can see that the number of possible sets s is a product of:

2 * 2 * ... * 2 or 2^N

comments powered by Disqus