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
0
votes
2 answers

Generating Power Set algorithm implementation

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…
Vladimir Ignatev
  • 2,054
  • 1
  • 20
  • 34
0
votes
1 answer

Power Set using Recursion

#include void powerSet(int* a, int index, int *curr, int N) { if (index == N) return; printf("("); for(int i = 0; i <= index; i++) printf("%d, ", curr[i]); printf(")\n"); // processing here. int…
Giridhar
  • 155
  • 1
  • 8
0
votes
2 answers

Time Complexity of Finding All Possible Sub Array of an Array

As per the implementation of finding all possible sub-array from a given array as follows: public class AllPossibleSubArray { public static void main(String[] args) { int[] arr = { 1, 2, 3 }; List> result = new…
0
votes
1 answer

Initialise Power set as anonymous interface

import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; public class PowerSet { public static final Collection> of(Set s) { List src = new…
Coolman
  • 166
  • 3
  • 11
0
votes
1 answer

Saving output from powerset

I recently started to learn something about the powerset-possibility with python. The code which i use looks as followed: def printPowerSet(set,set_size): # set_size of power set of a set # with set_size n is (2**n -1) pow_set_size…
menbar
  • 77
  • 1
  • 7
0
votes
1 answer

ArrayList value changed to last element when accessing outside the loop in which values are added

Actually I want to store all the distinct subset in the res ArrayList. I don't want to change the datatype of list, as it is required by some other method. The problem I get is the value of res is changed if accessing outside the loop in which it is…
0
votes
3 answers

is there a way to duplicate a row?

I want for something that works the opposite way that a COUNT, that's like a inverse GROUP BY (a SPLIT BY?) but that you can work with more freely. So far I've seen all "commands" are for restrictions or specification, meaning that you can't create…
iaguitos
  • 1
  • 1
0
votes
1 answer

How to print what is in a Set of Sets with an iterator?

I'm trying to print out the contents of my powerSet which is a set of a given set, however when I try to iterate through my powerSet, I get a C2679 Error of << binary "<<" with this following function. template void…
Amor Diaz
  • 41
  • 5
0
votes
2 answers

What's wrong with this combination-generating recursion?

I have the following code: const findMult_3 = (num) => { const powerset = (set) => { const combinations = [] const combine = (prefix, chars) => { for (let i = 0; i < chars.length; i++) { combinations.push(prefix +…
j_d
  • 2,818
  • 9
  • 50
  • 91
0
votes
1 answer

Expansion of list comprehension leading to infinite loop in listing power sets

I have been trying to solve the problem of listing power sets on Question 78 on Leetcode. I ran into a solution that uses list comprehensions and it works. I tried expanding it and following through with the Python documentation to make sure I get…
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33
0
votes
1 answer

python - custom power set function

I am trying to execute this code in python and it compiles without error. However, I do not see the variable z in the variable explorer. I am trying to create a function that gives all the subsets of an input set. import numpy as np import itertools…
0
votes
5 answers

Power set manipulation in Python

I am manipulating a set, so if you have a set(aka:list) of n distinctive elements, then you have 2^n subset. Here I show how: def powerset(s): x = len(s) masks = [1 << i for i in range(x)] for i in range(1 << x): yield [ss for…
user8650813
0
votes
1 answer

What itertools returns i.e. powerSet

import itertools def powerSet(items): ''' items : a list returns all possible combinations of the items ''' return itertools.chain.from_iterable(itertools.combinations(items,r) for r…
Dayson D
  • 321
  • 2
  • 7
0
votes
1 answer

SMLNJ powerset function

I am trying to print the size of a list created from below power set function fun add x ys = x :: ys; fun powerset ([]) = [[]] | powerset (x::xr) = powerset xr @ map (add x) (powerset xr) ; val it = [[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]]…
bapuy
  • 17
  • 5
0
votes
1 answer

Why does my powerset function run out of memory?

def powerset(x): total1 = [[]] total2 = [[]] for a in x: for b in total1: c = list(b + [x[a]]) total2.append(c) total1 = total2 # the total 1 and total 2 system should prevent it …
sumguy
  • 47
  • 7