forked from williamfiset/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerSet.java
84 lines (68 loc) · 2.05 KB
/
PowerSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* This code snippet shows how to generate the powerset of a set which is the set of all subsets of
* a set. There are two common ways of doing this which are to use the binary representation of
* numbers on a computer or to do it recursively. Both methods are shown here, pick your flavor!
*
* <p>Time Complexity: O( 2^n )
*
* @author William Fiset, [email protected]
*/
package com.williamfiset.algorithms.other;
public class PowerSet {
// Use the fact that numbers represented in binary can be
// used to generate all the subsets of an array
static void powerSetUsingBinary(int[] set) {
final int N = set.length;
final int MAX_VAL = 1 << N;
for (int subset = 0; subset < MAX_VAL; subset++) {
System.out.print("{ ");
for (int i = 0; i < N; i++) {
int mask = 1 << i;
if ((subset & mask) == mask) System.out.print(set[i] + " ");
}
System.out.println("}");
}
}
// Recursively generate the powerset (set of all subsets) of an array by maintaining
// a boolean array used to indicate which element have been selected
static void powerSetRecursive(int at, int[] set, boolean[] used) {
if (at == set.length) {
// Print found subset!
System.out.print("{ ");
for (int i = 0; i < set.length; i++) if (used[i]) System.out.print(set[i] + " ");
System.out.println("}");
} else {
// Include this element
used[at] = true;
powerSetRecursive(at + 1, set, used);
// Backtrack and don't include this element
used[at] = false;
powerSetRecursive(at + 1, set, used);
}
}
public static void main(String[] args) {
// Example usage:
int[] set = {1, 2, 3};
powerSetUsingBinary(set);
// prints:
// { }
// { 1 }
// { 2 }
// { 1 2 }
// { 3 }
// { 1 3 }
// { 2 3 }
// { 1 2 3 }
System.out.println();
powerSetRecursive(0, set, new boolean[set.length]);
// prints:
// { 1 2 3 }
// { 1 2 }
// { 1 3 }
// { 1 }
// { 2 3 }
// { 2 }
// { 3 }
// { }
}
}