Power Set Power set P(S) of a set S is the set of all subsets of S. For example S = {a, b, c} then P(s) = {{}, {a}, {b}, {c}, {a,b}, {a, c}, {b, c}, {a, b, c}}.
If S has n elements in it then P(s) will have 2^n elements
Algorithm:
Input: Set[], set_size
1. Get the size of power set
powet_set_size = pow(2, set_size)
2 Loop for counter from 0 to pow_set_size
(a) Loop for i = 0 to set_size
(i) If ith bit in counter is set
Print ith element from set for this subset
(b) Print separator for subsets i.e., newlineExample:
Set = [a,b,c]
power_set_size = pow(2, 3) = 8
Run for binary counter = 000 to 111
Value of Counter Subset
000 -> Empty set
001 -> a
010 -> b
011 -> ab
100 -> c
101 -> ac
110 -> bc
111 -> abcMethod 1:
C++
// C++ Program of above approach#include <iostream>#include <math.h>using namespace std;class gfg{ public:void printPowerSet(char *set, int set_size){ /*set_size of power set of a set with set_size n is (2**n -1)*/ unsigned int pow_set_size = pow(2, set_size); int counter, j; /*Run from counter 000..0 to 111..1*/ for(counter = 0; counter < pow_set_size; counter++) { for(j = 0; j < set_size; j++) { /* Check if jth bit in the counter is set If set then print jth element from set */ if(counter & (1 << j)) cout << set[j]; } cout << endl; }}};/*Driver code*/int main(){ gfg g; char set[] = {'a','b','c'}; g.printPowerSet(set, 3); return 0;}// This code is contributed by SoM15242 |
C
#include <stdio.h>#include <math.h>void printPowerSet(char *set, int set_size){ /*set_size of power set of a set with set_size n is (2**n -1)*/ unsigned int pow_set_size = pow(2, set_size); int counter, j; /*Run from counter 000..0 to 111..1*/ for(counter = 0; counter < pow_set_size; counter++) { for(j = 0; j < set_size; j++) { /* Check if jth bit in the counter is set If set then print jth element from set */ if(counter & (1<<j)) printf("%c", set[j]); } printf("\n"); }}/*Driver program to test printPowerSet*/int main(){ char set[] = {'a','b','c'}; printPowerSet(set, 3); return 0;} |
Java
// Java program for power setimport java .io.*;public class GFG { static void printPowerSet(char []set, int set_size) { /*set_size of power set of a set with set_size n is (2**n -1)*/ long pow_set_size = (long)Math.pow(2, set_size); int counter, j; /*Run from counter 000..0 to 111..1*/ for(counter = 0; counter < pow_set_size; counter++) { for(j = 0; j < set_size; j++) { /* Check if jth bit in the counter is set If set then print jth element from set */ if((counter & (1 << j)) > 0) System.out.print(set[j]); } System.out.println(); } } // Driver program to test printPowerSet public static void main (String[] args) { char []set = {'a', 'b', 'c'}; printPowerSet(set, 3); }}// This code is contributed by anuj_67. |
Python3
# python3 program for power setimport math;def printPowerSet(set,set_size): # set_size of power set of a set # with set_size n is (2**n -1) pow_set_size = (int) (math.pow(2, set_size)); counter = 0; j = 0; # Run from counter 000..0 to 111..1 for counter in range(0, pow_set_size): for j in range(0, set_size): # Check if jth bit in the # counter is set If set then # print jth element from set if((counter & (1 << j)) > 0): print(set[j], end = ""); print("");# Driver program to test printPowerSetset = ['a', 'b', 'c'];printPowerSet(set, 3);# This code is contributed by mits. |
C#
// C# program for power setusing System;class GFG { static void printPowerSet(char []set, int set_size) { /*set_size of power set of a set with set_size n is (2**n -1)*/ uint pow_set_size = (uint)Math.Pow(2, set_size); int counter, j; /*Run from counter 000..0 to 111..1*/ for(counter = 0; counter < pow_set_size; counter++) { for(j = 0; j < set_size; j++) { /* Check if jth bit in the counter is set If set then print jth element from set */ if((counter & (1 << j)) > 0) Console.Write(set[j]); } Console.WriteLine(); } } // Driver program to test printPowerSet public static void Main () { char []set = {'a', 'b', 'c'}; printPowerSet(set, 3); }}// This code is contributed by anuj_67. |
PHP
<?php// PHP program for power setfunction printPowerSet($set, $set_size){ // set_size of power set of // a set with set_size // n is (2**n -1) $pow_set_size = pow(2, $set_size); $counter; $j; // Run from counter 000..0 to // 111..1 for($counter = 0; $counter < $pow_set_size; $counter++) { for($j = 0; $j < $set_size; $j++) { /* Check if jth bit in the counter is set If set then print jth element from set */ if($counter & (1 << $j)) echo $set[$j]; } echo "\n"; }} // Driver Code $set = array('a','b','c'); printPowerSet($set, 3);// This code is contributed by Vishal Tripathi?> |
Javascript
<script>// javascript program for power setpublic function printPowerSet(set, set_size) { /* * set_size of power set of a set with set_size n is (2**n -1) */ var pow_set_size = parseInt(Math.pow(2, set_size)); var counter, j; /* * Run from counter 000..0 to 111..1 */ for (counter = 0; counter < pow_set_size; counter++) { for (j = 0; j < set_size; j++) { /* * Check if jth bit in the counter is set If set then prvar jth element from set */ if ((counter & (1 << j)) > 0) document.write(set[j]); } document.write("<br/>"); } } // Driver program to test printPowerSet let set = [ 'a', 'b', 'c' ]; printPowerSet(set, 3);// This code is contributed by shikhasingrajput</script> |
a b ab c ac bc abc
Time Complexity: O(n2^n)
Method 2:
This method is specific to the python programming language. We can iterate a loop over 0 to the length of the set to obtain and generate all possible combinations of that string with the iterable length. The program below will give the implementation of the above idea.
Python3
#Python program to find powersetfrom itertools import combinationsdef print_powerset(string): for i in range(0,len(string)+1): for element in combinations(string,i): print(''.join(element))string=['a','b','c']print_powerset(string) |
a b c ab ac bc abc
Recursive program to generate power set
Refer Power Set in Java for implementation in Java and more methods to print power set.
References:
http://en.wikipedia.org/wiki/Power_set
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



