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.
204 questions
-2
votes
2 answers

Specialized Powerset Requirement

Let's say I have compiled five regular expression patterns and then created five Boolean variables: a = re.search(first, mystr) b = re.search(second, mystr) c = re.search(third, mystr) d = re.search(fourth, mystr) e = re.search(fifth, mystr) I…
Mark Ginsburg
  • 1,589
  • 2
  • 14
  • 23
-2
votes
1 answer

powerset(all combinations) of a resultset in T-SQL

I need a t-sql code to get powerset of a resultset. example input : ColumnName 1 2 3 Example Output(one columns as nvarchar) : 1 2 3 1,2 1,3 2,3 1,2,3 Output set may contain duplicate values such as (1,3 vs 3,1).
Jean
  • 494
  • 4
  • 19
-2
votes
1 answer

Power set of string array(as input) in C#

I want to create a power set function which accepts string[] as input and gives output as shown in the bottom under EDIT1. I came across this private static List PowerSet(string[] input) { int n = input.Length; // Power set contains…
Artiga
  • 695
  • 1
  • 14
  • 36
-3
votes
1 answer

Combinations of Words...Power Set?

I'm looking to crack a password I forgot. I know all the words possibly used in the password however some may be used or not. Example: HarryMetSally could be the password but I might use a wordlist of "Harry" "Billy" "Sally" and "Met" to combine to…
tekisfun
  • 5
  • 1
-4
votes
1 answer

Turning a recursive function with for loop, to pure recursive

I have made this function to print powerset within a given array, I Inherently built the function with for loop as a base, and then convert it to pure recursive, but i seem to hit a roadbloack. here's the function: void powerSet(int* arr, int* p,…
-4
votes
2 answers

Implementation of Permutation, Combinations and PowerSet in C++

I am looking for the implementation of Permutation, Combination and PowerSet using C+++
user3762106
  • 4,429
  • 1
  • 10
  • 7
-5
votes
2 answers

How to find all combination of an array in javascript

I'm using node.js currently, and i would like to find / try all the unique possibilities resulting from the combinations of data stored in an array. Example : // source array var array = [0,1,2]; // what i need…
NoxWorld
  • 13
  • 6
-5
votes
1 answer

Algorithm to make all possible subsets of n items

I have n items and I need an algorithm to make all possible subsets of these. What I really need is an algorithm that will make all possible partitions of these n items into two disjoint sets. But I thought all possible subsets would be a good…
Summer
  • 1
  • 1
-9
votes
2 answers

Given a positive integer N as input, how do I find the product of the numbers in their subsets?

If N=405, how do I generate all of it's subsets and then find the product of each subset's numbers? Like in this case, the set would be {0,4,5,40,05,45,405} and their products would result in a set {0,4,5,0,0,20,0}? EDIT: It's a multi-set of…
Aulene De
  • 21
  • 5
1 2 3
13
14