Given a set {a, b, c} how do you print out all elements of the power set
If a set has N items the power set has 2 to the power N items in it
That includes the null set
Solution
int powerSet;
powerSet=pow(2,N);
for(i = 0; i<(powerSet);i++)
{
for(j=0;j<N;j++)
{
if((i & (1<<j)))
{
printf("%c ",y[j]);
}
}
printf("\n");
}
printf("\n");
Explanation
The outer loop executes 2^N times. Each item it prints one element in the power set S.
The inner loop uses the condition i & (1<<j) to determine whether to print individual items for the element of the power set
No comments:
Post a Comment