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

Finding all subsets of a list in python

I need to create a function that will be passed a list and will create a new list with all the subsets of the the original list. I have to write it all from scratch, so no importing from itertools. For Example: the list given be like [1,2,3] and I…
JustBeginning
  • 33
  • 1
  • 4
0
votes
2 answers

How can I limit my powerset function?

I found a powerset function from a StackOverflow Post: public static T[][] FastPowerSet(T[] seq) { var powerSet = new T[1 << seq.Length][]; powerSet[0] = new T[0]; // starting only with empty set for (int i = 0; i…
Seth Kitchen
  • 1,526
  • 19
  • 53
0
votes
1 answer

Python : Finding power set without using any inbuilt function like itertools etc

I am trying to find the power set using bit manipulation.I can generate all sets but they are not indexable. I could not save it as a list of list. I tried to find the solution over the internet but could not get the relevant information. Here is…
Khurshid
  • 458
  • 1
  • 5
  • 20
0
votes
3 answers

AWK power set implementation

This page provides a power set implementation in shell, and here is my take on it: pa() { if [ "$#" = 0 ] then echo else ( shift pa "$@" ) | while read qu do printf '%s %s\n%s\n' "$1" "$qu" "$qu" done fi } pa x y z I…
Zombo
  • 1
  • 62
  • 391
  • 407
0
votes
0 answers

Is my Powerset function Running out of Memory?

I am using a powerset to find all of the possible sums of a set of BigIntegers (reference System.Numerics). First a helper class to sum the power set. public static class Summer { public static T Sum(this IEnumerable values, IAdder
Seth Kitchen
  • 1,526
  • 19
  • 53
0
votes
1 answer

What is the logic behind this program for generating the power set of a list of items

I'm reading this book on decision trees and in one section the author gave us an example of code for generating a power set. The explanation is god-awful and while i understand the syntax and meanings of all the operations. I'm not getting the…
0
votes
1 answer

Best way to get a power set of an array?

What is the best way to get the power set of an array? For example if I have an array: int[] A = {1, 2} And to get the following output int[] P = {{}, {1}, {2}, {1, 2}}
Michael
  • 39
  • 1
  • 5
0
votes
2 answers

How do I make a powerset without using map or lambda in Haskell?

I'm trying to make a powerset in Haskell (I'm very new to it), and I can't figure out exactly what I need to do in order to make one without map. WITH map and lambda, I found this solution: powerset :: Set a -> Set (Set a) powerset [] =…
TurboCrackers
  • 43
  • 1
  • 10
0
votes
1 answer

Python Power set , can't figure out my error

My code will crash and run forever: def subsets(nums): """ :type nums: List[int] :rtype: List[List[int]] """ results = [[]] for num in nums: for result in results: results.extend([result + [num]]) …
XueYu
  • 2,355
  • 29
  • 33
0
votes
2 answers

Algorithm for Power Set / Sub-Set combinations required, but only for a fixed number of elements

It is an extension of this problem - Super set in a list: Getting all possible combinations from a list of numbers This helps, but returns all sub-sets: http://rosettacode.org/wiki/Power_set#C.23 But what I need is to retrieve only the sub-sets that…
SomeOne
  • 5
  • 2
0
votes
0 answers

How do I make a Python 3.x Powerset Generator have conditions before generating sets?

I'm a beginning programer looking to make a powerset for a list of numbers of varying size (generally at least 150+). The code I have works for smaller lists of numbers but not when I substitute the [4,5,6] for my larger list. def powerset(s): x…
Lara PA
  • 1
  • 3
0
votes
2 answers

Alternative to mapcar lisp

I need to write a recursive function for powerset, but I can't use mapcar, loop. This is my code so far: (defun parts (L) (cond ((null L)'(nil)) (T (let ((PXs (parts (cdr L)))) (append PXs (add(car L) PXs)) ) )…
Ivan
  • 69
  • 6
0
votes
1 answer

Converting an array containing the power set of a positive integer d to an array with standard elementary vectors in d-dimensional space

Example of what I am trying to do: convert the power set of d = 2 using range(2): [(), (0,), (1,), (0, 1)] or using range(1, 3, 1): [(), (1,), (2,), (1, 2)] into the following array of arrays: [[0, 0], [1, 0], [0, 1], [1, 1]] More generally, I…
Mee Seong Im
  • 131
  • 1
  • 8
0
votes
3 answers

Calculate Adjacent Powerset of an array

Internet is full of powerset algorithms. But in my case, I need only powerset of adjacent parts. Eg. From: [1, 2, 3], I'd need to get: adjacentPowerset([1, 2, 3]) = [[1, 2, 3], [1, 2], [2, 3], [1], [2], [3]]; // Without [1, 3] I'm struggling to…
Augustin Riedinger
  • 20,909
  • 29
  • 133
  • 206
0
votes
1 answer

Generating powesets of a vector

I am currently trying to write a function which will allow me to create the powerset of a vector. I currently have a vector of objects and an empty vector of vectors. I looked around online, and found a recursive algorithm, but was unable to get it…
VivaLaPanda
  • 809
  • 7
  • 24