Trying to calculate all the subsets (power set) of the 9-letter string \'ABCDEFGHI\'.
Using standard recursive methods, my machine hits out of memory (1GB) error bef
I would use divide and conquer for this:
Set powerSet(Set set) {
return merge(powerSet(Set leftHalf), powerSet(Set rightHalf));
}
merge(Set leftHalf, Set rightHalf) {
return union(leftHalf, rightHalf, allPairwiseCombinations(leftHalf, rightHalf));
}
That way, you immediately see that the number of solutions is 2^|originalSet| - that's why it is called the "power set". In your case, this would be 2^9, so there should not be an out of memory error on a 1GB machine. I guess you have some error in your algorithm.