Skip to main content

Power Set

Definition​

The Power Set Algorithm is a method used to generate all possible subsets of a given set. It's commonly implemented using recursion and bitwise manipulation for efficiency

Practice​

powerSet(inputSet):
memo = {} # Initialize a memoization dictionary to store already computed subsets
result = [] # Initialize an empty list to store subsets

generateSubset(0, []) # Start the recursive function with index 0 and an empty subset
return result # Return the power set

# Recursive function to generate subsets
generateSubset(index, currentSubset):
if index == len(inputSet): # Base case: If index equals the length of input set
result.append(currentSubset) # Add currentSubset to result list
return
if (index, currentSubset) in memo: # Check if subset with current index and currentSubset is memoized
return
generateSubset(index + 1, currentSubset) # Skip current element
generateSubset(index + 1, currentSubset + [inputSet[index]]) # Include current element
memo[(index, currentSubset)] = True # Memoize the current subset