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
5
votes
3 answers

Find n-th set of a powerset

I'm trying to find the n-th set in a powerset. By n-th I mean that the powerset is generated in the following order -- first by the size, and then, lexicographically --, and so, the indices of the sets in the powerset of [a, b, c] is: 0 - [] 1 -…
Rubens
  • 14,478
  • 11
  • 63
  • 92
4
votes
3 answers

Generate powerset lazily

I want to calculate powerset of a set. Because I don't need the whole powerset at a time, it's better to generate it lazily. For example: powerset (set ["a"; "b"; "c"]) = seq { set []; set ["a"]; set ["b"]; set ["c"]; set ["a"; "b"]; …
pad
  • 41,040
  • 7
  • 92
  • 166
4
votes
2 answers

Find powerset of all unique combinations of vector of strings

I am trying to find all of the unique groupings of a vector/list of items, length 39. Below is the code I have: x <- c("Dominion","progress","scarolina","tampa","tva","TminKTYS", "TmaxKTYS","TminKBNA","TmaxKBNA","TminKMEM","TmaxKMEM", …
HazelnutCoffee
  • 175
  • 1
  • 1
  • 9
4
votes
3 answers

Generating Power Set with Recursion and Yield Statement in Python

I am a bit new to Python and am working through programming exercises. I have written the following recursive method to generate the power set based on an input list in Python. It should return a generator that generates the power set of a given…
ben348943
  • 51
  • 4
4
votes
1 answer

how can you find the length of the longest subset (powerset) with sum equal to k with least time complexity?

Given an array of integers, I'm trying to find the longest subset (powerset) with sum equal to k using the lease possible time complexity. e.g. if inputArr= [1, 2, 8, 1, 1, 7] and k= 10, then the output should be 4 since the longest subset with sum…
Jim
  • 133
  • 1
  • 8
4
votes
1 answer

Algorithm to get every possible subset of a list, in order of their product, without building and sorting the entire list (i.e Generators)

Practically, I've got a set of objects with probabilities, and I want to look at each possible group of them, in order of how likely it is that they're all true assuming they're independent -- i.e. in descending order of the product of the elements…
Tim Perry
  • 11,766
  • 1
  • 57
  • 85
4
votes
2 answers

Is there a more efficient way to calculate the power set of an array?

This is my current implementation using bits: Function Array_PowerSet(Self) Array_PowerSet = Array() PowerSetUpperBound = -1 For Combination = 1 To 2 ^ (UBound(Self) - LBound(Self)) ' I don't want the null set Subset = Array() …
Hao Zhang
  • 211
  • 2
  • 7
4
votes
3 answers

Python Power Set of a List

I am trying to implement a function to generate the powerset of a list xs. The general idea is that we walk through the elements of xs and choose whether to include x or not. The problem I'm facing is that withX ends up being equal to [None] (a…
user2993016
  • 41
  • 1
  • 1
  • 2
4
votes
2 answers

Pandas create powerset and average data

For every date date I want to get the average of the Amount from every combination of Brand. For example, I have a dataframe: df1 = Company Brand Date Amount A 1 01/01/2015 3 A 1 01/02/2015 4 A 1 …
user3357979
  • 607
  • 1
  • 5
  • 12
4
votes
2 answers

selecting a random element of the power set

For a problem that I'm working on right now, I would like a reasonably uniform random choice from the powerset of a given set. Unfortunately this runs right into statistics which is something that I've not studied at all (something that I need to…
aaronasterling
  • 68,820
  • 20
  • 127
  • 125
4
votes
5 answers

Reduced permutations

Consider the following string abcd I can return 2 character permutations (cartesian product) like this $ echo {a,b,c,d}{a,b,c,d} aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd However I would like to remove redundant entries such as ba ca cb da…
Zombo
  • 1
  • 62
  • 391
  • 407
4
votes
3 answers

JAVA : generating power set with subset length between 3 and a max

Everything is in the title .. =) I want to create a powerset fastly, only with subset of length between 3 (minimum constant length) and a maximum. This maximum should be 5 most of the times but I want it variable (from 3 to 5,6). The original set…
Nikkolasg
  • 444
  • 4
  • 18
3
votes
1 answer

efficient powerset algorithm for subsets of minimal length

i am using the following C# function to get a powerset limited to subsets of a minimal length string[] PowerSet(int min_len, string set) { IEnumerable> seed = new List>() {…
o17t H1H' S'k
  • 2,541
  • 5
  • 31
  • 52
3
votes
3 answers

Generating the power set of a list

I have to write a brute-force implementation of the knapsack problem. Here's the pseudocode: computeMaxProfit(weight_capacity) max_profit = 0 S = {} // Each element of S is a weight-profit pair. while true if the sum of the…
nucleartide
  • 3,888
  • 4
  • 28
  • 29
3
votes
1 answer

Generate a powerset without a stack in Erlang

Note: This is a sequel to my previous question about powersets. I have got a nice Ruby solution to my previous question about generating a powerset of a set without a need to keep a stack: class Array def powerset return to_enum(:powerset)…
skanatek
  • 5,133
  • 3
  • 47
  • 75
1 2
3
14 15