1
1
# Power Set
2
2
3
3
Power set of a set ` S ` is the set of all of the subsets of ` S ` , including the
4
- empty set and ` S ` itself. Power set of set ` S ` is denoted as ` P(S) ` .
4
+ empty set and ` S ` itself. Power set of set ` S ` is denoted as ` P(S) ` .
5
5
6
6
For example for ` {x, y, z} ` , the subsets
7
7
are:
21
21
22
22
![ Power Set] ( https://www.mathsisfun.com/sets/images/power-set.svg )
23
23
24
- Here is how we may illustrate the elements of the power set of the set ` {x, y, z} ` ordered with respect to
24
+ Here is how we may illustrate the elements of the power set of the set ` {x, y, z} ` ordered with respect to
25
25
inclusion:
26
26
27
27
![ ] ( https://upload.wikimedia.org/wikipedia/commons/e/ea/Hasse_diagram_of_powerset_of_3.svg )
28
28
29
29
** Number of Subsets**
30
30
31
- If ` S ` is a finite set with ` |S| = n ` elements, then the number of subsets
32
- of ` S ` is ` |P(S)| = 2^n ` . This fact, which is the motivation for the
31
+ If ` S ` is a finite set with ` |S| = n ` elements, then the number of subsets
32
+ of ` S ` is ` |P(S)| = 2^n ` . This fact, which is the motivation for the
33
33
notation ` 2^S ` , may be demonstrated simply as follows:
34
34
35
- > First, order the elements of ` S ` in any manner. We write any subset of ` S ` in
36
- the format ` {γ1, γ2, ..., γn} ` where ` γi , 1 ≤ i ≤ n ` , can take the value
35
+ > First, order the elements of ` S ` in any manner. We write any subset of ` S ` in
36
+ the format ` {γ1, γ2, ..., γn} ` where ` γi , 1 ≤ i ≤ n ` , can take the value
37
37
of ` 0 ` or ` 1 ` . If ` γi = 1 ` , the ` i ` -th element of ` S ` is in the subset;
38
- otherwise, the ` i ` -th element is not in the subset. Clearly the number of
38
+ otherwise, the ` i ` -th element is not in the subset. Clearly the number of
39
39
distinct subsets that can be constructed this way is ` 2^n ` as ` γi ∈ {0, 1} ` .
40
40
41
41
## Algorithms
42
42
43
43
### Bitwise Solution
44
44
45
- Each number in binary representation in a range from ` 0 ` to ` 2^n ` does exactly
46
- what we need: it shows by its bits (` 0 ` or ` 1 ` ) whether to include related
47
- element from the set or not. For example, for the set ` {1, 2, 3} ` the binary
45
+ Each number in binary representation in a range from ` 0 ` to ` 2^n ` does exactly
46
+ what we need: it shows by its bits (` 0 ` or ` 1 ` ) whether to include related
47
+ element from the set or not. For example, for the set ` {1, 2, 3} ` the binary
48
48
number of ` 0b010 ` would mean that we need to include only ` 2 ` to the current set.
49
49
50
50
| | ` abc ` | Subset |
51
51
| :---: | :---: | :-----------: |
52
52
| ` 0 ` | ` 000 ` | ` {} ` |
53
53
| ` 1 ` | ` 001 ` | ` {c} ` |
54
- | ` 2 ` | ` 010 ` | ` {b} ` |
54
+ | ` 2 ` | ` 010 ` | ` {b} ` |
55
55
| ` 3 ` | ` 011 ` | ` {c, b} ` |
56
56
| ` 4 ` | ` 100 ` | ` {a} ` |
57
57
| ` 5 ` | ` 101 ` | ` {a, c} ` |
@@ -68,6 +68,44 @@ element.
68
68
69
69
> See [ btPowerSet.js] ( ./btPowerSet.js ) file for backtracking solution.
70
70
71
+ ### Cascading Solution
72
+
73
+ This is, arguably, the simplest solution to generate a Power Set.
74
+
75
+ We start with an empty set:
76
+
77
+ ``` text
78
+ powerSets = [[]]
79
+ ```
80
+
81
+ Now, let's say:
82
+
83
+ ``` text
84
+ originalSet = [1, 2, 3]
85
+ ```
86
+
87
+ Let's add the 1st element from the originalSet to all existing sets:
88
+
89
+ ``` text
90
+ [[]] ← 1 = [[], [1]]
91
+ ```
92
+
93
+ Adding the 2nd element to all existing sets:
94
+
95
+ ``` text
96
+ [[], [1]] ← 2 = [[], [1], [2], [1, 2]]
97
+ ```
98
+
99
+ Adding the 3nd element to all existing sets:
100
+
101
+ ```
102
+ [[], [1], [2], [1, 2]] ← 3 = [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
103
+ ```
104
+
105
+ And so on, for the rest of the elements from the ` originalSet ` . On every iteration the number of sets is doubled, so we'll get ` 2^n ` sets.
106
+
107
+ > See [ caPowerSet.js] ( ./caPowerSet.js ) file for cascading solution.
108
+
71
109
## References
72
110
73
111
* [ Wikipedia] ( https://en.wikipedia.org/wiki/Power_set )
0 commit comments