Questions tagged [powerset]

A powerset is the set of all subsets for a given set.

For a given set S: the powerset P(S) the set of all subsets of that set S.

  • P(S) = {T: TS) where T is a set.

Given a set with three elements {1, 2, 3}, the powerset P({1, 2, 3}) would contain the eight subsets of the set, including itself and the empty set:

  • {∅, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}}

For a set S of finite size n, the size of the powerset P(S) is 2n. The cardinality of S is always strictly less than the cardinality of P(S): the set of natural numbers N is countably infinite, but the set of the powerset of N is uncountable.

Algorithm to create a Powerset

Python offers itertools which can be used to create a powerset of a given set or list. Here is the documentation: https://docs.python.org/3/library/itertools.html#recipes

def powerset(s: set):
    s = list(s)
    ps = chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
    return list(ps) # set(ps) also works

Here is a test run:

s = {1,2,3}
ps = powerset(s)
# ps = [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]

There exists an iterative method to create a powerset. The size of the powerset grows exponentially and reaches over two billion for sets of size larger than 32. For a set S of size n:

  • Create an initially empty list L which will eventually represent the powerset
  • For each element, give it an index, ranging from 0 to n-1.
    • Suppose we have {A, B, C}. Let the index of A be 0, B be 1, C be 2.
  • Loop through all the numbers from 0 to 2n-1 and express each in binary form.
    • The loop counter i goes from 0 (000 in binary) to 7 (111 in binary).
  • Create a subset Ti based on the binary number, which contains the elements such that the binary digit position of the elements' index for i is 1.
    • The ones digit is given position 0, the fours digit (third digit) is given position 2.
    • When we have 0, the subset T0 is the empty set.
    • When we have 5, the binary digits are 101, the digits with position 2 and 0 are ones, and so the subset T5 is {A, C}.
  • Add that subset to the original list L.

Read more

  • Wikipedia
  • , a mathematical object and a datastructure which represents a collection of unique objects.
222 questions
1
vote
2 answers

Getting a powerset recursively

I want to preface this by saying that this is for a school assignment, so while I want help, it would definitely be preferable to point me in the right direction rather than giving me code to use. So the assignment is to be able to print out the…
Caboose
  • 453
  • 5
  • 16
1
vote
1 answer

Optimal way to obtain get Powerset of a List (recursively)

To the get the power set of a list, I implemented the following: public static ArrayList> getAllSubsets(ArrayList set, int index, ArrayList subset){ ArrayList> subsets = new…
1
vote
6 answers

print powerset of a string

I'm trying to write python code to print the powerset of a string, but am running into some bugs. Here's what I've got: def getperm (string): perm = [] if len(string) == 0: perm.append("") return perm #if len(string) ==…
mythander889
  • 915
  • 5
  • 16
  • 23
1
vote
2 answers

how to make all possible power set(or subset) from arrayList objects?

Say I have the following class: class A { String name; Double value; } and a list of the above class objects which might have: [{f 2.1}, {c 1.1}, {a 0.3}... and so on] [{n 0.5}, {f 1.9}, {x 0.1}, {a 1.9}, {b 1.1}... and so on] ...…
flyleaf
  • 985
  • 4
  • 11
  • 27
1
vote
2 answers

Dynamic programming in power sets

Is it possible to use dynamic programming in the calculation of power set of a string (ie, all possible subsequences of that string) to reduce the number of computations significantly?
1
vote
1 answer

Prolog powerset predicate

I wish to define a predicate powerset(X, P) which is true when P is the powerset of X. Should work whether or not P is ground.
user1283759
  • 51
  • 1
  • 2
0
votes
1 answer

Generate a powerset with the help of a binary representation

I know that "a powerset is simply any number between 0 and 2^N-1 where N is number of set members and one in binary presentation denotes presence of corresponding member". (Hynek -Pichi- Vychodil) I would like to generate a powerset using this…
skanatek
  • 5,133
  • 3
  • 47
  • 75
0
votes
1 answer

Divide a set by other set in all possible variations

Imagine we have a set S = [a,b,c,d,e,f]. And we have a set N = [1,2,3]. How can we assign elements of S to elements of N in all possible combinations? The desired result will hold something like this: [1,[a]], [2,[b,c]], [3,[d,e,f]]. [1,[a]],…
skanatek
  • 5,133
  • 3
  • 47
  • 75
0
votes
0 answers

Is there an efficient/analytical solution to find probabilities of each case in a power set?

In my capacity as a design engineer, I needed to size equipment to feed compressed air to a set of dust collectors. These dust collectors require 10 minutes (approximately) of "pulsing" air every few hours to clean the filter medium. The pulses last…
0
votes
0 answers

Permutation a list and Powerset is a list of all subsets OOP python

As a beginner need help, how to implement logic to figure out this problems. I am trying to find Permutation is a list of all arrangements of elements and Powerset is a list of all subsets of elements including the empty set. from functools import…
WsTec
  • 1
  • 2
0
votes
1 answer

How to make a powerset in ascending order for the first item in a list?

I am trying to make a powerset of a list for the first item of that list in ascending order. However, I couldn't find on StackOverflow how to tackle this specific problem. When making a powerset of the following list: backlog = [1, 2, 3, 4, 5] with…
Mathijsvdk
  • 11
  • 3
0
votes
0 answers

How can I create a power set at the end of the program?

i need to create a power set of array lo[], array lo[] is {5, 6, 7}; it's for my laboratories, I don't know how to do this in general power set is 5, {5, 6} {5, 7} {5, 6, 7}, all combinations and another important thing, power of array. the number…
KonstantaV
  • 23
  • 4
0
votes
2 answers

Power set elements of a certain length

Given an array of elements in PHP, I wish to create a new two-dimensional array containing only those elements of the power set that are a specific length. As an example, for the following array: array(4) { 0 => 'A', 1 => 'B', 2 => 'C', …
stevendesu
  • 15,753
  • 22
  • 105
  • 182
0
votes
2 answers

generating powerset with backtracking where subsets with lower number of elements appear earlier and they are sorted

I was solving the classic powerset generation using backtracking. Now, let's say, I add another constraint, the output should appear in a specific order. For input = [1, 2, 3], the output should be [[], [1], [2], [3], [1,2], [1,3], [2,3],…
Zabir Al Nazi
  • 10,298
  • 4
  • 33
  • 60
0
votes
1 answer

How to generate powersets with a maximum size in C#?

I am trying to generate all powersets from a given list within a limit of given maximum size. I've found some great answers for how to generate powersets in general and admire the solution using bitmaps found here All Possible Combinations of a list…
Elphie
  • 3
  • 2