5

I have an algorithm to compute the powerset of a set using all of the bits between 0 and 2^n:

public static <T> void findPowerSetsBitwise(Set<T> set, Set<Set<T>> results){
        T[] arr = (T[]) set.toArray();
        int length = arr.length;

        for(int i = 0; i < 1<<length; i++){
            int k = i;
            Set<T> newSubset = new HashSet<T>();
            int index = arr.length - 1;
            while(k > 0){
                if((k & 1) == 1){
                    newSubset.add(arr[index]);
                }
                k>>=1;
                index --;
            }
            results.add(newSubset);
        }

    }

My question is: What is the running time of this algorithm. The loop is running 2^n times and in each iteration the while loop runs lg(i) times. So I think the running time is

T(n) = the sum from i=0 to i=2^n of lg(i)

But I don't know how to simplify this further, I know this can be solved in O(2^n) time (not space) recursively, so I'm wondering if the method above is better or worse than this, timewise as it's better in space.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Aly
  • 15,865
  • 47
  • 119
  • 191

3 Answers3

6
sigma(lg(i)) where i in (1,2^n) 
= lg(1) + lg(2) + ... + lg(2^n)     
= lg(1*2*...*2^n) 
= lg((2^n)!) 
> lg(2^2^n) 
  = 2^n

thus, the suggested solution is worth in terms of time complexity then the recursive O(2^n) one.


EDIT:
To be exact, we know that for each k - log(k!) is in Theta(klogk), thus for k=2^n we get that lg((2^n)!) is in Theta(2^nlog(2^n) = Theta(n*2^n)

amit
  • 175,853
  • 27
  • 231
  • 333
  • why is x! > 2^x, still up voting but would be very grateful if you could explain that :) – Aly May 21 '11 at 21:46
  • 2
    @Aly: because x! = 1*2*3...*x (x times, where all but first and 2nd element > 2) where 2^x=2*2*...*2 (x times, where all is 2...) so for large x, x!>2^x – amit May 21 '11 at 21:48
2

Without attempting to solve or simulate, it is easy to see that this is worse than O(2^n) because this is 2^n * $value where $value is greater than one (for all i > 2) and increases as n increases, so it is obviously not a constant.

Seth Robertson
  • 30,608
  • 7
  • 64
  • 57
2

Applying Sterling's formula, it is actually O(n*2^n).

Philip Sheard
  • 5,789
  • 5
  • 27
  • 42