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.
204 questions
124
votes
29 answers

How to get all subsets of a set? (powerset)

Given a set {0, 1, 2, 3} How can I produce the subsets: [set(), {0}, {1}, {2}, {3}, {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}, {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3}, {0, 1, 2, 3}]
patrick
  • 13,334
  • 23
  • 94
  • 151
87
votes
26 answers

Obtaining a powerset of a set in Java

The powerset of {1, 2, 3} is: {{}, {2}, {3}, {2, 3}, {1, 2}, {1, 3}, {1, 2, 3}, {1}} Let's say I have a Set in Java: Set mySet = new HashSet(); mySet.add(1); mySet.add(2); mySet.add(3); Set> powerSet =…
Manuel Araoz
  • 14,795
  • 21
  • 67
  • 91
44
votes
11 answers

How to find all subsets of a set in JavaScript?

I need to get all possible subsets of an array. Say I have this: [1, 2, 3] How do I get this? [], [1], [2], [1, 2], [2, 3], [1, 3], [1, 2, 3] I am interested in all subsets. For subsets of specific length, refer to the following…
le_m
  • 15,910
  • 7
  • 55
  • 65
35
votes
13 answers

Find all subsets of length k in an array

Given a set {1,2,3,4,5...n} of n elements, we need to find all subsets of length k . For example, if n = 4 and k = 2, the output would be {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}. I am not even able to figure out how to start. We don't have…
h4ck3d
  • 5,584
  • 15
  • 44
  • 73
25
votes
8 answers

What algorithm can calculate the power set of a given set?

I would like to efficiently generate a unique list of combinations of numbers based on a starting list of numbers. example start list = [1,2,3,4,5] but the algorithm should work for [1,2,3...n] result =…
ross
  • 263
  • 1
  • 3
  • 5
22
votes
8 answers

How to generate a power set of a given set?

I am studying for an interview and I stumbled upon this question online under the "Math" category. Generate power set of given set: int A[] = {1,2,3,4,5}; int N = 5; int Total = 1 << N; for ( int i = 0; i < Total; i++ ) { for ( int j = 0; j <…
Ralph
  • 2,719
  • 7
  • 21
  • 43
21
votes
3 answers

Unordered combinations of all lengths

I am looking a function that return me all the unordered combination of a vector. eg x <- c('red','blue','black') uncomb(x) [1]'red' [2]'blue' [3]'black' [4]'red','blue' [5]'blue','black' [6]'red','black' [7]'red','blue','black' I guess that there…
emanuele
  • 2,329
  • 6
  • 35
  • 55
21
votes
8 answers

How to generate the power set of a set in Scala

I have a Set of items of some type and want to generate its power set. I searched the web and couldn't find any Scala code that adresses this specific task. This is what I came up with. It allows you to restrict the cardinality of the sets produced…
Björn Jacobs
  • 3,730
  • 4
  • 25
  • 44
17
votes
5 answers

Is there a name for this set/array operation?

Given the input array [a,b,c,d,e] and a 'join' function (a,b) => (a+b) my code returns the following array of arrays, containing each possible variation obtained by applying the join function to various pairs of elements whilst maintaining the…
Dylan Beattie
  • 50,029
  • 31
  • 120
  • 189
15
votes
5 answers

Power set generated by bits

I have this code which generates power set of an array of size 4 (number is just example, less combinations to write...). #define ARRAY_SIZE 4 unsigned int i, j, bits, i_max = 1U << ARRAY_SIZE; int array[ARRAY_SIZE]; for (i = 0; i < i_max ; ++i)…
user1824918
9
votes
7 answers

Printing all possible subsets of a list

I have a List of elements (1, 2, 3), and I need to get the superset (powerset) of that list (without repeating elements). So basically I need to create a List of Lists that looks like: {1} {2} {3} {1, 2} {1, 3} {2, 3} {1, 2, 3} What is the best…
Steve
  • 3,819
  • 11
  • 41
  • 74
8
votes
5 answers

Memory efficient power set algorithm

Trying to calculate all the subsets (power set) of the 9-letter string 'ABCDEFGHI'. Using standard recursive methods, my machine hits out of memory (1GB) error before completing. I have no more physical memory. How can this be done better? Language…
zaf
  • 21,901
  • 11
  • 58
  • 95
8
votes
1 answer

why Data.Set has no powerset function?

I was looking at Data.Set, and I found out that it has no powerset function. Why? I can implement it like this: import Data.Set (Set, empty, fromList, toList, insert) powerset :: (Ord a) => Set a -> Set (Set a) powerset s = fromList $ map…
MarcoS
  • 12,788
  • 5
  • 36
  • 61
8
votes
3 answers

Fast powerset implementation with complement set

I would like to have a function powersetWithComplements :: [a] -> [([a], [a])] Such that for example: powersetWithComplements [1,2,3] = [([],[1,2,3]),([3],[1,2]),([2],[1,3]),([2,3],[1]),([1],[2,3]),([1,3],[2]),([1,2],[3]),([1,2,3],[])] It is easy…
user1747134
  • 1,982
  • 15
  • 19
8
votes
4 answers

Algorithm to calculate power set (all possible subsets) of a set in R

I couldn't find an answer to this anywhere, so here's my solution. The question is: how can you calculate a power set in R? It is possible to do this with the library "sets", with the command 2^as.set(c(1,2,3,4)), which yields the output {{}, {1},…
sssheridan
  • 610
  • 1
  • 6
  • 13
1
2 3
13 14