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
1 answer

subsets [that are of length l] of list in S size

hi i already looked throw the forum but didn't found solution to my problem. the problem is : how can i find all the possible subsets [that are of length l] of list in S size . and return it in a list.
guffi8
  • 3
  • 1
0
votes
1 answer

Find the powerest of a set recursively

I am trying to find the powerset of a set recursively and then print the results which I am very stuck on any help would be greatly appreciated. public static ArrayList getpowerset(int a[],int n, ArrayList power){ if(n<0) { …
user3008724
  • 31
  • 3
  • 7
0
votes
1 answer

Make recursive methods from an regular method

I have code below that does exactly what I want the program to do. The only problem is I don't even know where to get started to make the methods recursive. I understand using recursion for factorials and other problems but this one is over my head…
user2053184
0
votes
3 answers

Java: Generate a Powerset

This could be language agnostic/helpful answers could just be in pseudo-code. I have a program that I would like to test under a range of inputs. This program takes a set of files, one of which is designated as the root. I want to run the program…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
0
votes
1 answer

Finding a powerset from a linked list

I am looking for some help with my Java programming assignment. Using a linked list, I need to print out its power set. For example, the set {1,2,3} should print out {{},{1}{1,2}{1,3}{1,2,3}{2}{2,3}{3}}. The number of elements in the power set is…
Jeff
  • 65
  • 1
  • 7
0
votes
1 answer

How to make subset or powerSet quickly and efficiently in java from a list?

I'm going through a problem of making subsets or powersets from an arrayList with may be 70 or 80 or more String elements and then further process those subsets(or powersets). For example I have some arrayLists which have non duplicate strings as…
divine
  • 33
  • 1
  • 2
  • 8
0
votes
3 answers

Get all 1-k tuples in a n-tuple

With n=5 and k=3 the following loop will do it List l=new ArrayList(); l.add("A");l.add("B");l.add("C");l.add("D");l.add("E"); int broadcastSize = (int) Math.pow(2, l.size()); for (int i = 1; i < broadcastSize; i++) { …
user1125394
-1
votes
2 answers

Obtaining power set in mathematical order

The powerset of {1, 2, 3} is: {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}} I have a String array in java, String elements={"apple","mango","banana"}; String set[]=elements.split("[ ,]+"); How do I print the power set of…
-1
votes
1 answer

Why doesn't my code work for outputting of all subsets of a set work?

I'm trying to write code to output all subsets of a set (represented by a list of integers). Why doesn't the following code work? public static List> powerSet(List l){ List> result = new…
medihde
  • 21
  • 7
-1
votes
1 answer

Java - Iteratively generate powerset in specific order

I need to iteratively generate the powerset of a large set, in a specific order. With iteratively I mean that with each call to getNext() (or similar) I get the next element of the powerset in the specific order. Precalculating and storing the…
OppfinnarJocke
  • 1,038
  • 2
  • 9
  • 21
-1
votes
2 answers

Using Java Streams to operate on an integer power set

I am generating a power set (Set>) from an original set (Set). i.e. {1, 2, 3} -> { {}, {1}, {2}, {3}, {1,2}, {2,3}, {1,3}, {1,2,3} } Then I am using an isClique(Set) method that returns a boolean if the given set is a…
-1
votes
2 answers

2 power of 77 java Powerset

I have 77 elements n1,n2,n3... etc I need to calculate superset. I used the binary mask algorithme but the number is to big to fit in int. Here is the code: private static Vector powerset(String[] set) { //create the empty power set …
sdx11
  • 181
  • 1
  • 1
  • 9
-1
votes
3 answers

Power set of large set

I have to calculate power set of set which may have more elements upto 10^5. I tried an algo and the code below but it failed (I think cause large value of pow(2, size)). void printPowerSet(int *set, int set_size) { unsigned int pow_set_size =…
DKP
  • 11
  • 2
-1
votes
2 answers

Combinations of X elements into 1, 2, 3, 4, ... X sub-sub-arrays

I have an array that looks like this: [0, 1, 2, 3, 4, 5, ...] I need a function that will give me an array like this: [ [[0, 1, 2, 3, 4, 5]], [[0, 1, 2, 3, 4], [ 5 ]], [[0, 1, 2, 3], [ 4, 5 ]], [[0, 1, 2], [ 3, 4, 5 ]], ... [[0, 1, 2], [ 3, 4…
Horia
  • 15
  • 5
-1
votes
1 answer

Finding the powerset of a String

I'm trying to write a java method for finding the subset of a string but I keep getting a runtime error I have been unable to debug. Here is the code: public static List subset(String m, List list){ if (m.length() <= 1){ …
1 2 3
14
15