The power set of a given set \( \mathcal{S} \) is the set of all possible subsets of \( \mathcal{S} \), including: 1) the empty set \(\emptyset \), 2) each individual element of \( \mathcal{S} \) as a subset, 3) all possible combinations of elements in \( \mathcal{S} \), and 4) the set itself \( \mathcal{S} \).
The formula for the Number of Subsets
If a set contains n elements, then the total number of subsets in its power set is given by: \(2^n\)
Example:
If \( \mathcal{S} = \{a,b,c\} \), then the power set \( P(\mathcal{S}) \) is:
\[ P(\mathcal{S}) = \{\emptyset, \{a\}, \{b\}, \{c\}, \{a,b\}, \{a,c\}, \{b,c\}, \{a,b,c\}\} \]
Since \( \mathcal{S} \) has 3 elements, the number of subsets in its power set is: \(2^3\) = 8.
The only subset of an empty set \(\emptyset \) is itself, so its power set is: \(\{\emptyset\} \)
Power Set in Python
If you want to generate a power set programmatically, Python provides an easy way to do so using the itertools
module. Here is how you can generate the power set of a set in Python:
from itertools import combinations
import argparse
def power_set(s):
"""
Find the power set of a given set.
"""
power_set_result = []
for r in range(len(s) + 1):
power_set_result.extend(combinations(s, r))
return [set(subset) for subset in power_set_result]
def main():
"""
This program finds power set of a given set
"""
# provide the set as command-line argument
parser = argparse.ArgumentParser(description="Find power set")
parser.add_argument("vals", nargs="*", help="List of values to add to the set")
args = parser.parse_args()
# create list using command-line values
input_set = set(args.vals)
# find power set
ps = power_set(input_set)
print(f"Power set of {input_set}: {ps}")
print(f"Number of subsets in the power set: {len(ps)}")
if __name__ == "__main__":
main()
To run this code, save it as power_set.py
(or any preferred name you like) and execute it from the command line with space-separated values, as shown below:
python power_set.py 1 2 3 4 5
The code will take all command-line arguments and create a set from them. In this example, the set will be {1, 2, 3, 4, 5}.
Please let me know in the comments if you encounter any issues with the code.