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}
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: