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

Recursive function to calculate the powerset of a set

I have been asked to write a recursive function that returns the powerset (a list of all subsets) of a set in the following exact order: (powerset '(a b)) -> (() (b) (a) (a b)) (powerset '(a b c )) -> (() (c) (b) (b c) (a) (a c) (a b) (a b c)) We…
KOB
  • 4,084
  • 9
  • 44
  • 88
0
votes
0 answers

Powerset of a python list

I wrote this code to try to get the powerset of a python list, and return it as a list of lists, but for some reason it is not working, because it gives a 'NoneType' object has no attribute 'append'. I cannot figure out why though, can anyone…
Kamal
  • 560
  • 1
  • 5
  • 14
0
votes
1 answer

Java PowerSet -prints only one

Having a problem, only printing one instead of each powerset and I can't quite spot it. Trying to write a program that gives out a powerset of a given set. import java.util.HashSet; import java.util.Iterator; public class PowerSet { public…
Joe Valenti
  • 11
  • 1
  • 5
0
votes
1 answer

For a set with N members, what is the number of set partition in which each subset is either size of 1 or 2?

I have a set with N members {1,2,.. N}. I like to know number of set partition in which each subset is either size of 1 or 2, i.e., cardinality of each subset in the partition is maximum 2. For example with N=3.. we have the set partitions of…
Sabyasachi G
  • 113
  • 1
0
votes
2 answers

Python inverse power set generator

I am trying to get an inverse power set generator, a generator that returns power sets from largest to smallest. Here is a standard power set generator (see this question): from itertools import chain, combinations def powerset_generator(i): for…
byrass
  • 360
  • 2
  • 12
0
votes
1 answer

Scheme: power set recursion, printing in sorted order using R5RS

Scheme subset recursion problem For the power function: (define (p l) (define (n next) (if (null? next) '() (cons (car next) (cons (cons (car l) (car next)) (n (cdr next)))))) (if (null?…
Jieun Chon
  • 139
  • 1
  • 10
0
votes
1 answer

How can I add an object to each element of a list in LISP?

I'm trying to write a function that adds an element to each element of a given powerset. No matter what it always evaluates (null pset) as true. I can't understand why. Here's what I have so far: (defun addxtopowerset(x pset) (cond …
0
votes
2 answers

Why the output of java program while using ^ operator is this?

I have made a program in which i want to calculate the power set size.But i am unable to do it because the ouput of the statement 1 is 0?Why is this showing wrong? public class PowerSetDemo { public static void main(String s[]) { int…
0
votes
1 answer

vector size remaining static after pushback() calls for powerset function

I wrote the following function, as an implementation of this algorithm/approach, to generate the power-set (set of all subsets) of a given string: vector getAllSubsets(string a, vector allSubsets) { if(a.length() == 1) { …
0
votes
1 answer

Permutation algorithm

I have a Hashmap called H1. There are n Hashmap keys for H1. For H1 hashmap, the program will create all the permutations of the powerset {1,2,3,4,...n}. So in other words, if n = 5, any number from 1,2,3,..5555 is a valid list for H1. So if, …
user3256369
  • 135
  • 1
  • 9
0
votes
1 answer

Iteratively calculate the power set of a set or vector

While there are plenty of examples on how to generate the actual power set of a set, I can't find anything about iteratively (as in std::iterator) generating the power set. The reason why I would appreciate such an algorithm is the size of my base…
wondering
  • 363
  • 5
  • 14
0
votes
0 answers

Is there any utility in java to create power set from a set?

I have a set ={1,2,3,4}. I want to create power set of this set. One way to create the power set is to take every element as binary(0/1) and print all possible combination. I was thinking about some utility/library in java to do the same.
Mosbius8
  • 119
  • 6
0
votes
3 answers

Using binary counting to count all subsets of an array

So if I am given an array such as a = {1, 2, 3} We know that the given subarrays (non contiguous included) are (this represents the power set) {1} {2} {3} {1,2,3} {1,2} {1,3} {2,3} I also know that these subsets can be represented by counting in…
cotro34
  • 15
  • 4
0
votes
1 answer

How to write C++ version of Python powerset function

I want to write the maximal clique algorithm for use with an adjacency matrix. I'm following a video which explains how to code and implement the algorithm using Python. I'm currently trying to code the powerset function at 2 minutes into the…
0
votes
1 answer

Invalid cast exception error when implementing Power set function

I am trying to generate the powerset of a list of node elements in a graph. I have identified and adapted the following code from a previous post (Unique Combination of Set) Public Function PowerSet(ByVal s As List(Of Node)) As List(Of List(Of…
MLD
  • 25
  • 9