I found this java code snippet to slice the array into all it's sub sets. But I'm not able to follow how the recursive calls work and how the subsets are formed. An explanation for the set {1,2,3} would be really helpful. Thanks in advance!
public static Set<Set<Integer>> powerSet(Set<Integer> originalSet) {
Set<Set<Integer>> sets = new HashSet<Set<Integer>>(); //All the subsets will be stored in sets.
if (originalSet.isEmpty()) {
sets.add(new HashSet<Integer>());
return sets;
}
List<Integer> list = new ArrayList<Integer>(originalSet);
int head = list.get(0);
Set<Integer> rest = new HashSet<Integer>(list.subList(1, list.size()));
for (Set<Integer> set : powerSet(rest)) {
Set<Integer> newSet = new HashSet<Integer>();
newSet.add(head);
newSet.addAll(set);
sets.add(newSet);
sets.add(set);
}
return sets;
}
sets itself is a set and it contains {{1},{1,2},{1,3},{2,3},{2},{3},{1,2,3},{}} after the code finishes executing.