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

How to modify this power set function to only include 'valid' subsets?

I am using a binomial tree as a list with 2^T - 1 'nodes' and want to create a set of subsets that work within some given criteria (outlined below) on the elements of the list. Right now, I use the following code to generate a tree def…
jodawg
  • 1
  • 1
0
votes
1 answer

How to use Iterator next() method to generate power set of LinkedSet

For my class I have to create an iterator that will generate a power set of a LinkedSet. Per my professor, I have to iterate through a bitString of the int current. Where current is the subset to which we are adding elements of the main set. I keep…
0
votes
2 answers

Convert itertools powerset into columnized numpy array

Given a tuple items, I get the powerset with itertools: items = tuple(('a', 'b', 'c')) combos = powerset(items) def powerset(iterable): "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" s = list(iterable) return…
Craig Nathan
  • 197
  • 10
0
votes
1 answer

How to find the power set of a given set without using left shift bit?

I'm trying to figure out how to implement an algorithm to find a power set given a set, but I'm having some trouble. The sets are actually vectors so for example I am given Set set1{ 'a','b','c' }; I would do PowerSet(set1); and I would get…
Jasmine
  • 49
  • 6
0
votes
3 answers

How to get powerset from a set with 3600 elements using as little memory as possible

I have been looking for a language and code to help me calculate all possible subsets of a set of 3600 elements. At first my search started with python, then I went through JavaScript and then came to Perl. I know using Perl to calculate all subsets…
7beggars_nnnnm
  • 697
  • 3
  • 12
0
votes
1 answer

Find all subsets upto length k while calculating power set of n?

Given a set {1, 2, 3, 4, 5 ... n} of n elements, we need to find all subsets of length up to k. For example, Input: n = 4 and k = 2 Output: {{1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}} private static final…
mux032
  • 65
  • 2
  • 8
0
votes
0 answers

Python powerset - memory error when unpacking the itertools.chain object into a list

Hi I am fairly new to python. I am trying to generate a powerset of all combinations for a list of integers, using the recommended code: def powerset(iterable): s = list(iterable) return chain.from_iterable(combinations(s, r) for r in…
TvelA
  • 1
0
votes
1 answer

Java 8 - Generate power set of list

I have written the following method to generate power set using Java 8 map function public static List> powerSet(List arr){ List> powerSet = new ArrayList<>(); powerSet.add(new ArrayList<>()); …
code-geek
  • 441
  • 1
  • 8
  • 22
0
votes
1 answer

how to find all the subsets with restrictions in python?

So i am doing a comb. auction algorithm and i like to give him N number of items for example (A,B,C) and i want the algorithm to give me back the following result (A,B,C) (ABC) (AB,C) (AC,B) (BC,A) any ideas ? i tried this one but its not enough k…
0
votes
2 answers

Power Sets too slow python

I am using the following function to find the subsets of a list L. However, when converting the output of the function powerset into a list it takes way too long. Any suggestion? For clarification, this powerset function does not output the empty…
gapansi
  • 105
  • 2
  • 10
0
votes
0 answers

Recursive backtracking to find all subsets

I have tried to solve a problem of generating all subsets of an array which contains no duplicates from leetcode : https://leetcode.com/problems/subsets/ . e.g if input is [1,2,3], output should be [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]. Since…
0
votes
1 answer

Find powersets for a given set

I'm trying to learn how to code and stumbled upon a problem generating a powerset of a source set in java. I try to write a method that returns a list of lists of doubles for a input of list of doubles. Sadly for a input of [1.0, 2.0, 3.0] it only…
0
votes
1 answer

Power set of an input set as custom collection

I have been reading the Effective Java book and I have stuck with this code I am unable to understand how this code is generating power set. Code: public class PowerSet { public static final Collection> of(Set s) { List
deadshot
  • 8,881
  • 4
  • 20
  • 39
0
votes
1 answer

How to save the contents of a powerSet into a 2d array in Java

I'm trying to save the contents of a PowerSet, obtained from a 1d Array into a 2d Array. I tried assigning the values in the array inside the "if" Statement but I'm getting the indices completely wrong int[] set = new int[]{2,4,5,8} int…
0
votes
1 answer

Generate all combinations of subsets which contain exactly the elements in the original set

Given the powerset of 1, 2, 3 I get 8 subsets: [], [1], [2], [2,1], [3], [3,1], [3,2], [3,2,1]. I would like to then generate the subsets of this new set which have exactly 1 of the original set. The number of subsets in the powerset of my powerset…
Toeler
  • 11
  • 2