This is based off other question I had on space complexity. Permutation Space Complexity
This is a solution to my problem of enumerating(naming) all sets. (Tested it, it works)
public static void subsets(Set<Integer> s) {
Queue<Integer> copyToProtectData = new LinkedList<Integer>();
for(int member: s) {
copyToProtectData.add(member);
}
generateSubsets(copyToProtectData, new HashSet<Integer>());
}
private static void generateSubsets(Queue<Integer> s,
Set<Integer> hashSet) {
if(s.isEmpty()) {
System.out.println(hashSet);
} else {
int member = s.remove();
Set<Integer> copy = new HashSet<Integer>();
for(int i:hashSet) {
copy.add(i);
}
hashSet.add(member);
Queue<Integer> queueCopy = new LinkedList<Integer>();
for(int i:s){
queueCopy.add(i);
}
generateSubsets(s, hashSet);
generateSubsets(queueCopy, copy);
}
}
I know that the time complexity of my algorithm of is O(2n) because a solution in discrete math is that a set n has 2n subsets. Is this an acceptable way of evaluating the time complexity of this algorithm(couldn't find a recurrence relation to do this)?
Moving on though, I am still having difficulties of evaluating the space complexity. I am trying to apply what I learned from my last question. In my last question which was on permutations of a String, @ajb said that because of the fact that I store a local string that grows by one on each recursive call, my space complexity is actually O(n2).
I am trying to apply that same here. Lets say my test set is {1,2,3}. To generate the subset {1,2,3}, from my algorithm, when {1,2,3} is finally printed, these "subsets" also exist in memory, - {1}, {}, {1,2},{1],{1,2,3}, {1,2}, meaning its not just a subset that has one less element like in the permutations problem. I also made copies of the leftovers at each round so that one operation in one side won't affect the copy on the other side. This is why I wasn't sure if @ajb's strategy would work here. Would the runtime still be O(n2) or would it be something greater?