snippetjavaMinor
Generate all elements of a power set
Viewed 0 times
elementsallpowergenerateset
Problem
```
/*
* Power set is just set of all subsets for given set.
* It includes all subsets (with empty set).
* It's well-known that there are 2N elements in this set, where N is count of elements in original set.
* To build power set, following thing can be used:
* Create a loop, which iterates all integers from 0 till 2^N-1
* Proceed to binary representation for each integer
* Each binary representation is a set of N bits (for lesser numbers, add leading zeros).
* Each bit corresponds, if the certain set member is included in current subset.
*/
import java.util.NoSuchElementException;
import java.util.BitSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.List;
import java.util.ArrayList;
public class PowerSet implements Iterator>, Iterable> {
private final E[] ary;
private final int subsets;
private int i;
public PowerSet(Set set) {
ary = (E[])set.toArray();
subsets = (int)Math.pow(2, ary.length) - 1;
}
public Iterator> iterator() {
return this;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Cannot remove()!");
}
@Override
public boolean hasNext() {
return i next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Set subset = new TreeSet();
BitSet bitSet = BitSet.valueOf(new long[] { i });
// get the index of truth values.
for (int e = bitSet.nextSetBit(0); e != -1; e = bitSet.nextSetBit(e + 1)) {
subset.add(ary[e]);
}
i++;
return subset;
}
// Unit Test
public static void main(String[] args) {
Set numbers = new TreeSet();
for (int i = 1; i pSet = new PowerSet(numbers);
for (Set subset : pSet) {
//System.out.println(subset);
count++;
}
assert count == (int)Math.pow(2, numbers.size()): "Total number of elements s
/*
* Power set is just set of all subsets for given set.
* It includes all subsets (with empty set).
* It's well-known that there are 2N elements in this set, where N is count of elements in original set.
* To build power set, following thing can be used:
* Create a loop, which iterates all integers from 0 till 2^N-1
* Proceed to binary representation for each integer
* Each binary representation is a set of N bits (for lesser numbers, add leading zeros).
* Each bit corresponds, if the certain set member is included in current subset.
*/
import java.util.NoSuchElementException;
import java.util.BitSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.List;
import java.util.ArrayList;
public class PowerSet implements Iterator>, Iterable> {
private final E[] ary;
private final int subsets;
private int i;
public PowerSet(Set set) {
ary = (E[])set.toArray();
subsets = (int)Math.pow(2, ary.length) - 1;
}
public Iterator> iterator() {
return this;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Cannot remove()!");
}
@Override
public boolean hasNext() {
return i next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Set subset = new TreeSet();
BitSet bitSet = BitSet.valueOf(new long[] { i });
// get the index of truth values.
for (int e = bitSet.nextSetBit(0); e != -1; e = bitSet.nextSetBit(e + 1)) {
subset.add(ary[e]);
}
i++;
return subset;
}
// Unit Test
public static void main(String[] args) {
Set numbers = new TreeSet();
for (int i = 1; i pSet = new PowerSet(numbers);
for (Set subset : pSet) {
//System.out.println(subset);
count++;
}
assert count == (int)Math.pow(2, numbers.size()): "Total number of elements s
Solution
Power of 2
Instead of using:
You can compute a power of two more efficiently like this:
You can use
Manual bit manipulation
Instead of using a
Instead of using:
(int)Math.pow(2, numbers.size())You can compute a power of two more efficiently like this:
1 << numbers.size()You can use
1L to compute it as a long if you think that the power may exceed 31.Manual bit manipulation
Instead of using a
BitSet to find the 1 bits in your int, you can use integer arithmetic to check the bits yourself:for (int bit = 0; bit < 32; bit++) {
if ((i & (1 << bit)) != 0) {
subset.add(ary[bit]);
}
}Code Snippets
(int)Math.pow(2, numbers.size())1 << numbers.size()for (int bit = 0; bit < 32; bit++) {
if ((i & (1 << bit)) != 0) {
subset.add(ary[bit]);
}
}Context
StackExchange Code Review Q#107507, answer score: 4
Revisions (0)
No revisions yet.