幂集集合S的幂集P(S)是S的所有子集的集合。例如S = {a,b,c},则P(s)= {{},{a},{b}, {c},{a,b},{a,c},{b,c},{a,b,c}}。
如果S中包含n个元素,则P(s)将具有2 ^ n个元素
算法:
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., newline
例子:
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 -> abc
方法1:
C++
// C++ Program of above approach
#include
#include
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
#include
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<
Java
// Java program for power set
import 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 set
import 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 printPowerSet
set = ['a', 'b', 'c'];
printPowerSet(set, 3);
# This code is contributed by mits.
C#
// C# program for power set
using 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
Javascript
Python3
#Python program to find powerset
from itertools import combinations
def 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
ab
c
ac
bc
abc
时间复杂度: O(n2 ^ n)
方法2:
此方法特定于Python编程语言。我们可以将循环遍历0到集合的长度,以获得并生成具有可迭代长度的该字符串的所有可能组合。下面的程序将给出上述想法的实现。
Python3
#Python program to find powerset
from itertools import combinations
def 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
递归程序以生成功率集
请参阅发电机组在Java中对Java和更多的方法实现打印发电机组。
参考:
http://en.wikipedia.org/wiki/Power_set
如果您喜欢GeeksforGeeks并希望做出贡献,则也可以使用contribution.geeksforgeeks.org撰写文章,或将您的文章邮寄到contribution@geeksforgeeks.org。请参阅GeeksforGeeks主页上显示的文章并为其他Geeks提供帮助。如果发现任何错误,或者要分享有关上述主题的更多信息,请发表评论。