The powerset of {1, 2, 3}
is:
{{}, {2}, {3}, {2, 3}, {1, 2}, {1, 3}, {1, 2, 3}, {1}}
Let\'s say I have a Set
in Java:<
public class PowerSet {
public static List> powerset(int[] a) {
LinkedList> sets = new LinkedList>();
int n = a.length;
for (int i = 0; i < 1 << n; i++) {
HashSet set = new HashSet();
for (int j = 0; j < n; j++) {
if ((1 << j & i) > 0)
set.add(a[j]);
}
sets.add(set);
}
return sets;
}
public static void main(String[] args) {
List> sets = PowerSet.powerset(new int[]{ 1, 2, 3 });
for (HashSet set : sets) {
for (int i : set)
System.out.print(i);
System.out.println();
}
}
}