Answer the question
In order to leave comments, you need to log in
How to traverse all subsets in a set?
Hello,
There are a variety of facilities. For example: {A,B,C}. It is necessary to find all subsets of the set {A,B,C} through cycles. What is the best way to do this? The complexity of the algorithm should be O(2^n). Zh
Preferably an example in java, but it is possible in any other language.
Thank you.
Answer the question
In order to leave comments, you need to log in
In fact, an n -element set 2 has n subsets, so n 2 for n > 2 is unrealistic to obtain.
One of the enumeration algorithms:
1. We start the second array (Boolean), the same size as the original one, initialize it with zeros.
2. We derive a subset of those elements of the original array, which correspond to units in the Boolean.
3. Treating the boolean as a representation of a number in the binary system, add 1 to it.
4. If there is at least one 1 in the boolean, go to step 2.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question