0

What is a good implementation of Power Set algorithm?

Recently I needed this algorithm for building a solver for my puzzle game. Generally, the solver should try strategies (sets of turns, the possible turns Power Set) and find the strategy that forms a solution.

I found out, that the naive implementation shown at Wikipedia page, as well as one from js-combinatorics library does not provide the stable order of items in generated subsets.

Also, the naive approach that utilise the bijection of the set to the natural numbers set and followed binary representation, is bounded by the size of the source set.

This limitation naturally occurs from the fact that internally the mentioned library uses 32-bit integer value for generating subsets.

3
  • "...lack the ordering of generated subsets" - this doesn't make sense. The power set is a set, so it does not have an ordering. If you need to generate them in a particular order, then describe that order in the question. Commented Mar 1, 2020 at 17:27
  • @kaya3 I completely agree with you, the power set is a set and the order doesn't matter, because sets don't have any order. But for some use cases, it would be good to have at least natural order of output from Power Set algorithm, as shown on Wikipedia page. Commented Mar 2, 2020 at 22:34
  • OK - well, I think you should still describe the required order in the question, since e.g. lexicographic order is also "natural" in some sense. It would also make your question easier to find by people who have the same problem. Commented Mar 2, 2020 at 23:05

2 Answers 2

1

Use the implementation fromt the itertools recipes:

from itertools import chain, combinations
def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
print(*powerset([1,2,3])) 

Output:

() (1,) (2,) (3,) (1, 2) (1, 3) (2, 3) (1, 2, 3)

It produces tuples - but you can convert them as you like. It looks also much shorter then your solution...

Sign up to request clarification or add additional context in comments.

3 Comments

It is a really great pick and great answer! Do you know alternative in JS?
@Vladimir Sorry, JS is not really my forte.
0

Probably, the Stackoverflow is not the best option for sharing Gist snippets, yet related questions present and I decided to share my snippet here, with a good belief that it may be useful for people who are looking for Power Set algorithm implementation and for Stackoverflow community itself.

https://gist.github.com/vladignatyev/e76b5fd1c3cdfff7034ce17506fae36e

My implementation may be difficult to understand. Please share with me your questions, improvements and suggestions related to this piece of Open source software freely!

Usage:
    >>> ps = power_set([1,2,3])
    >>> for ss in ps:
            print(ss)
Output:
    [], 
    [1], 
    [2], 
    [3], 
    [1, 2], 
    [1, 3], 
    [2, 3], 
    [1, 2, 3]

For your information, I ported my code to pure Swift 5, no dependencies required. Check out --> https://gist.github.com/vladignatyev/7e9399930cb614d6251a4f82b8e75ff1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.