The powerset of {1, 2, 3} is:
{{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}
I have a String array in java,
String elements={"apple","mango","banana"};
String set[]=elements.split("[ ,]+");
How do I print the power set of this array in the Mathematical order? (I have tried bit manipulation method, it does not gives the solution in that order! )
My bit manipulation method! Did not give the required result!
static void printPowerSet(String[] set) {
long pset = (long) Math.pow(2, set.length);
System.out.print("Power Set is \n{");
for (int i = 0; i < pset; i++) {
System.out.print("{");
for (int j = 0; j < set.length; j++) {
if ((i & (1 << j)) > 0){
System.out.print(set[j] + " ");
}
if (i == 0 && j==0 )
System.out.print(" ");
}
System.out.println("}");
}
System.out.println(" } \n");
}
This is a fun algorithm and one Knuth considers. It's actually far easier than it sounds.
I'm interpreting the order to be:
Output the subsets in order of cardinality (starting with the empty set then singletons then pairs and so on).
Within that order then further order them lexically with each other based on position of the included members ({1,2} is before {1,3} is before {2,3}).
The example below shows a set of 4 objects because 3 objects doesn't quite reveal what's going on...
It works on an array of
booleanflags to indicate membership. At each step look for afalseflag after at least onetrueflag. If found then set thatfalseflag totrueand set the number of true flags counted so far minus one back to the start.If not found because all flags are set, we've just output the full set. So finish. Otherwise it wasn't found because the set flags have migrated to the end, so start with the first subset with more members (the count+1 flags set).
You can comment back in the line
dump(flags,fruit);to see the fruit. But the flags output is the most illuminating.Expected Output:
Refer to: Art of Computer Programming, Volume 4A, The: Combinatorial Algorithms, Part 1.