I need an algorithm to find all of the subsets of a set where the number of elements in a set is n
.
S={1,2,3,4...n}
Edit: I am having trouble understanding the answers provided so far. I would like to have step-by-step explanation of how the answers work to find the subsets.
For example,
S={1,2,3,4,5}
How do you know {1}
and {1,2}
are subsets?
Could someone help me with a simple function in c++ to find subsets of {1,2,3,4,5}
The number of subsets that can be created from the set {1, 2, 3} is 8.
Answer: The set {1, 2, 3, 4, 5} has 32 subsets and 31 proper subsets.
For each subset it can either contain or not contain an element. For each element, there are 2 possibilities. Multiplying these together we get 27 or 128 subsets.
Summary: The subsets that can be made from a set of six elements, including the null set and the set itself, are 64.
It's very simple to do this recursively. The basic idea is that for each element, the set of subsets can be divided equally into those that contain that element and those that don't, and those two sets are otherwise equal.
Edit To make it crystal clear:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With