Let us have some sequence A, {-1, 3, 2, 5} for example. We can select all its non-empty subsequences using iterations (binary incrementing some iterator) 1 .. 2^|A|:
int i, A[] = {-1, 3, 2, 5};
for (i = 1; i < (1 << sizeof(A)); i++)
{
int t = i, p = 0;
while (t > 0)
{
if (t % 2 > 0)
printf("%d\t", a[p]);
t /= 2, p++;
}
printf("\n");
}
But what should we do if A contains, for example, 5000000 elements? E.g. how to handle 5-billion-bit number?
Well there is an inerestign set of algorithms that perform such tasks called “Enumeration algorithms”. They deal mainly which questions such as:
1. What is the ith non-empty subset of all subsets ?
2. Given a non empty subset, what is the next element ? or previous
3. Given a non empty subset, what is the rank of this subset ?
All these operations are performed with linear time. In case you want to obtain all subsets, then you will need to do backtracing.
An interesting book that deals with these issues is: Combinatorial Algorithms, Generation, Enumeration, Search By K. Rossen.
I have also a set of slides on this topic, I dont know how to attach them though. Check Lucia Moura website, course Combinatorial Algorithms.
(if you need details of any of these algorithms above, let me know please)