|
1 | 1 | # Power Set |
2 | 2 |
|
3 | | -Power set of a set A is the set of all of the subsets of A. |
| 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 | 5 |
|
5 | | -Eg. for `{x, y, z}`, the subsets are : `{{}, {x}, {y}, {z}, {x, y}, {x, z}, {y, z}, {x, y, z}}` |
| 6 | +For example for `{x, y, z}`, the subsets |
| 7 | +are: |
| 8 | + |
| 9 | +```text |
| 10 | +{ |
| 11 | + {}, // (also denoted empty set ∅ or the null set) |
| 12 | + {x}, |
| 13 | + {y}, |
| 14 | + {z}, |
| 15 | + {x, y}, |
| 16 | + {x, z}, |
| 17 | + {y, z}, |
| 18 | + {x, y, z} |
| 19 | +} |
| 20 | +``` |
6 | 21 |
|
7 | 22 |  |
8 | 23 |
|
| 24 | +Here is how we may illustrate the elements of the power set of the set `{x, y, z}` ordered with respect to |
| 25 | +inclusion: |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +**Number of Subsets** |
| 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 |
| 33 | +notation `2^S`, may be demonstrated simply as follows: |
| 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 |
| 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 |
| 39 | +distinct subsets that can be constructed this way is `2^n` as `γi ∈ {0, 1}`. |
| 40 | + |
| 41 | +## Algorithms |
| 42 | + |
| 43 | +### Bitwise Solution |
| 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 |
| 48 | +number of `0b010` would mean that we need to include only `2` to the current set. |
| 49 | + |
| 50 | +> See [bwPowerSet.js](./bwPowerSet.js) file for bitwise solution. |
| 51 | +
|
| 52 | +### Backtracking Solution |
| 53 | + |
| 54 | +In backtracking approach we're constantly trying to add next element of the set |
| 55 | +to the subset, memorizing it and then removing it and try the same with the next |
| 56 | +element. |
| 57 | + |
| 58 | +> See [btPowerSet.js](./btPowerSet.js) file for backtracking solution. |
| 59 | +
|
9 | 60 | ## References |
10 | 61 |
|
11 | 62 | * [Wikipedia](https://en.wikipedia.org/wiki/Power_set) |
|
0 commit comments