Re: Get sub sets from list
Search for COMBINATION'S. A simple loop can compute what you want.
Re: Get sub sets from list
This sounds like homework, so I'll try to only sketch the solution:
For a set of N elements there are 2^N subsets (or 2^N - 1, if you don't include the null set). An insight is that you can order them by assigning each subset a binary number. For, for the set {1, 2, 3, 4} you can denote the subset that includes the 2nd and 4th element as:
{ 2, 4 } = 0101
And in binary 0101 is 5, so this set corresponds to the number 5 and an element is included if there is a ONE at it's position in the binary representation of the number.
You can perform a loop to see if you should include an element like:
Code:
int subsetIDNumber = 0101;
for(int i = 0; i < 4; i++)
{
if( subsetIDNumber & 1 == 1 )
//Include the element
else
//Don't include the element
subsetIDNumber >>= 1; //Bitwise shift the subsetIDnumber right
}
Thus you can loop over them like...
Code:
int twoToTheNumElements = 16;
for(int i = 1; i <= twoToTheNumElements; i++)
{
//Construct the subset corresponding to the number i
}
Hope that helps. Good luck.