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

Generate all "unique" subsets of a set (not a powerset)

Let's say we have a Set S which contains a few subsets: - [a,b,c] - [a,b] - [c] - [d,e,f] - [d,f] - [e] Let's also say that S contains six unique elements: a, b, c, d, e and f. How can we find all possible subsets of S that contain each of the…
skanatek
  • 5,133
  • 3
  • 47
  • 75
7
votes
3 answers

Generate a powerset of a set without keeping a stack in Erlang or Ruby

I would like to generate a powerset of a rather big set (about 30-50 elements) and I know that it takes 2^n to store the powerset. Is it possible to generate one subset at a time? I.e. generate a powerset of a set with iterations, saving each…
skanatek
  • 5,133
  • 3
  • 47
  • 75
7
votes
6 answers

next_permutation for combinations or subsets in powerset

Is there some equivalent library or function that will give me the next combination of a set of values like next_permutation in does for me?
bobber205
  • 12,948
  • 27
  • 74
  • 100
7
votes
4 answers

Time complexity of this code to list all subsets of a set?

I have found numerous solutions across the web having O(2^n) complexity. Can someone help me figure out the time complexity of the code given below. Also it involves lot of bit manipulation and I am really weak in that area so I didn't quite get the…
Abhiroop Sarkar
  • 2,251
  • 1
  • 26
  • 45
7
votes
5 answers

How to do a powerset in DrRacket?

I'm using the beginning language with list abbreviations for DrRacket and want to make a powerset recursively but cannot figure out how to do it. I currently have this much (define (powerset aL) (cond [(empty? aL) (list)] any help would be…
user3109171
  • 79
  • 1
  • 1
  • 2
6
votes
1 answer

Prove that the powerset of a finite set is finite using Coq

While trying to prove some things, I encountered an innocent looking claim that I failed to prove in Coq. The claim is that for a given Finite Ensemble, the powerset is also finite. The statement is given in the Coq code below. I looked through the…
Herman Bergwerf
  • 143
  • 1
  • 5
6
votes
1 answer

Analysis of different Sets and optimizations. Best approach?

For the last few days, I've tried to accomplish the following task regarding the analysis of a Set of Objects, and the solutions I've come up with rely heavily on Memory (obtaining OutOfMemory exceptions in some cases) or take an incredible long…
RainierMallol
  • 806
  • 1
  • 8
  • 24
6
votes
3 answers

Python: powerset of a given set with generators

I am trying to build a list of subsets of a given set in Python with generators. Say I have set([1, 2, 3]) as input, I should have [set([1, 2, 3]), set([2, 3]), set([1, 3]), set([3]), set([1, 2]), set([2]), set([1]), set([])] as output. How can…
linkyndy
  • 17,038
  • 20
  • 114
  • 194
6
votes
2 answers

Efficient arrangement algorithm in java

I'm trying to write a method that will compute all permutations of a power set where order matters. I believe these are called "arrangements." What I mean by this is: {a} -> {{a}, {}} {a,b} -> {{a,b}, {b,a}, {a}, {b}, {}} {a,b,c} -> {{a,b,c},…
rhombidodecahedron
  • 7,693
  • 11
  • 58
  • 91
5
votes
3 answers

What is the running time of this powerset algorithm

I have an algorithm to compute the powerset of a set using all of the bits between 0 and 2^n: public static void findPowerSetsBitwise(Set set, Set> results){ T[] arr = (T[]) set.toArray(); int length = arr.length; …
Aly
  • 15,865
  • 47
  • 119
  • 191
5
votes
1 answer

Cracking the Coding Interview: Why does the recursive subset algorithm increase the index rather than decreasing it?

In chapter 8 of Cracking the Coding Interview, 6th Edition, there's a question for finding all the subsets, and this is the given solution: Arraylist> getSubsets(Arraylist set, int index) { …
F. Guo
  • 53
  • 3
5
votes
1 answer

F# Powerset Function

The function below returns the powerset of a set (list). let rec powerset = function | [] -> [[]] | x::xs -> List.collect (fun sub -> [sub; x::sub]) (powerset xs) I don't understand why exactly it works. I understand recursion. I also…
5
votes
3 answers

Powerset of a set with list comprehension in Haskell

I am a complete beginner in Haskell and I have 11 exercises for homework, 10 of which I have already solved. I have found several solutions to get the powerset of a set, but none of them includes list comprehension. I know I should not ask for a…
user3059248
  • 429
  • 2
  • 6
  • 14
5
votes
1 answer

Powerset Without Duplicates

I need to create a powerset function in haskell which takes a set and outputs the power set without duplicate entries, regardless of what is put in the input list. For example: [1,1] should return [[],[1]] powerset [] = [[]] powerset (x:xs)…
lepdeffard
  • 91
  • 6
5
votes
2 answers

Create cartesian product (powerset?) of JavaScript objects by looping through unknown number of arrays

I'm a beginner, so please pardon my ignorance if this is trivial. I have a javascript object of unknown length, and each property's value is an array (also of unknown length to me). For example: var obj = {"varA":[1,2,3], …
1
2
3
14 15