-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetMaker.java
38 lines (31 loc) · 961 Bytes
/
SetMaker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* Chris Fahlin
* Tcss 342
* Graphs
* SetMaker.java
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class SetMaker {
// http://stackoverflow.com/questions/1670862/obtaining-powerset-of-a-set-in-java
public static <T> Set<Set<T>> powerSet(Set<T> originalSet) {
Set<Set<T>> sets = new HashSet<Set<T>>();
if (originalSet.isEmpty()) { // an empty set has only one subset
sets.add(new HashSet<T>()); // and that's the empty set
return sets; // sets contains only the empty set
}
List<T> list = new ArrayList<T>(originalSet);
T head = list.get(0); // first element
Set<T> rest = new HashSet<T>(list.subList(1, list.size())); // all the rest
for (Set<T> set : powerSet(rest)) {
Set<T> newSet = new HashSet<T>();
newSet.add(head);
newSet.addAll(set);
sets.add(newSet);
sets.add(set);
}
return sets;
}
}