Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over all subsets of a given size

I know that iterating over all subsets of a set of size n is a performance nightmare and will take O(2^n) time.

How about iterating over all subsets of size k (for (0 <= k <= n))? Is that a performance nightmare? I know there are (n, k) = n! / k! (n - k)! possibilities. I know that if k is very close to 0 or very close to n, this is a small number.

What is the worst case performance in terms of n and k? Is there a simpler way of saying it other than just O(n! / k! (n - k)!)? Is this asymptotically smaller than O(n!) or the same?

like image 502
Paul Reiners Avatar asked Apr 10 '13 17:04

Paul Reiners


1 Answers

You want Gosper's hack:

int c = (1<<k)-1;
while (c < (1<<n)) {
  dostuff(c);
  int a = c&-c, b = c+a;
  c = (c^b)/4/a|b;
}

Explanation:

Finding the next number with as many bits set basically reduces to the case of numbers with exactly one "block of ones" --- numbers having a bunch of zeroes, then a bunch of ones, then a bunch of zeroes again in their binary expansions.

The way to deal with such a "block of ones" number is to move the highest bit left by one and slam all the others as low as possible. Gosper's hack works by finding the lowest set bit (a), finding the "high part" comprising the bits we don't touch and the "carry bit" (b), then producing a block of ones of the appropriate size that begins at the least-significant bit.

like image 110
tmyklebu Avatar answered Sep 20 '22 22:09

tmyklebu