mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
Adding a simple cascading solution to generate a Power Set (#975)
* Add a simple cascading version of generating a PowerSet. * Update README. * Update README. * Update README.
This commit is contained in:
parent
a123b9017d
commit
65e4a7c8b3
@ -99,7 +99,7 @@ a set of rules that precisely define a sequence of operations.
|
|||||||
* **Sets**
|
* **Sets**
|
||||||
* `B` [Cartesian Product](src/algorithms/sets/cartesian-product) - product of multiple sets
|
* `B` [Cartesian Product](src/algorithms/sets/cartesian-product) - product of multiple sets
|
||||||
* `B` [Fisher–Yates Shuffle](src/algorithms/sets/fisher-yates) - random permutation of a finite sequence
|
* `B` [Fisher–Yates Shuffle](src/algorithms/sets/fisher-yates) - random permutation of a finite sequence
|
||||||
* `A` [Power Set](src/algorithms/sets/power-set) - all subsets of a set (bitwise and backtracking solutions)
|
* `A` [Power Set](src/algorithms/sets/power-set) - all subsets of a set (bitwise, backtracking, and cascading solutions)
|
||||||
* `A` [Permutations](src/algorithms/sets/permutations) (with and without repetitions)
|
* `A` [Permutations](src/algorithms/sets/permutations) (with and without repetitions)
|
||||||
* `A` [Combinations](src/algorithms/sets/combinations) (with and without repetitions)
|
* `A` [Combinations](src/algorithms/sets/combinations) (with and without repetitions)
|
||||||
* `A` [Longest Common Subsequence](src/algorithms/sets/longest-common-subsequence) (LCS)
|
* `A` [Longest Common Subsequence](src/algorithms/sets/longest-common-subsequence) (LCS)
|
||||||
|
@ -68,6 +68,44 @@ element.
|
|||||||
|
|
||||||
> See [btPowerSet.js](./btPowerSet.js) file for backtracking solution.
|
> See [btPowerSet.js](./btPowerSet.js) file for backtracking solution.
|
||||||
|
|
||||||
|
### Cascading Solution
|
||||||
|
|
||||||
|
This is, arguably, the simplest solution to generate a Power Set.
|
||||||
|
|
||||||
|
We start with an empty set:
|
||||||
|
|
||||||
|
```text
|
||||||
|
powerSets = [[]]
|
||||||
|
```
|
||||||
|
|
||||||
|
Now, let's say:
|
||||||
|
|
||||||
|
```text
|
||||||
|
originalSet = [1, 2, 3]
|
||||||
|
```
|
||||||
|
|
||||||
|
Let's add the 1st element from the originalSet to all existing sets:
|
||||||
|
|
||||||
|
```text
|
||||||
|
[[]] ← 1 = [[], [1]]
|
||||||
|
```
|
||||||
|
|
||||||
|
Adding the 2nd element to all existing sets:
|
||||||
|
|
||||||
|
```text
|
||||||
|
[[], [1]] ← 2 = [[], [1], [2], [1, 2]]
|
||||||
|
```
|
||||||
|
|
||||||
|
Adding the 3nd element to all existing sets:
|
||||||
|
|
||||||
|
```
|
||||||
|
[[], [1], [2], [1, 2]] ← 3 = [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
> See [caPowerSet.js](./caPowerSet.js) file for cascading solution.
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
* [Wikipedia](https://en.wikipedia.org/wiki/Power_set)
|
* [Wikipedia](https://en.wikipedia.org/wiki/Power_set)
|
||||||
|
28
src/algorithms/sets/power-set/__test__/caPowerSet.test.js
Normal file
28
src/algorithms/sets/power-set/__test__/caPowerSet.test.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import caPowerSet from '../caPowerSet';
|
||||||
|
|
||||||
|
describe('caPowerSet', () => {
|
||||||
|
it('should calculate power set of given set using cascading approach', () => {
|
||||||
|
expect(caPowerSet([1])).toEqual([
|
||||||
|
[],
|
||||||
|
[1],
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(caPowerSet([1, 2])).toEqual([
|
||||||
|
[],
|
||||||
|
[1],
|
||||||
|
[2],
|
||||||
|
[1, 2],
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(caPowerSet([1, 2, 3])).toEqual([
|
||||||
|
[],
|
||||||
|
[1],
|
||||||
|
[2],
|
||||||
|
[1, 2],
|
||||||
|
[3],
|
||||||
|
[1, 3],
|
||||||
|
[2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
37
src/algorithms/sets/power-set/caPowerSet.js
Normal file
37
src/algorithms/sets/power-set/caPowerSet.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Find power-set of a set using CASCADING approach.
|
||||||
|
*
|
||||||
|
* @param {*[]} originalSet
|
||||||
|
* @return {*[][]}
|
||||||
|
*/
|
||||||
|
export default function caPowerSet(originalSet) {
|
||||||
|
// Let's start with an empty set.
|
||||||
|
const sets = [[]];
|
||||||
|
|
||||||
|
/*
|
||||||
|
Now, let's say:
|
||||||
|
originalSet = [1, 2, 3].
|
||||||
|
|
||||||
|
Let's add the first element from the originalSet to all existing sets:
|
||||||
|
[[]] ← 1 = [[], [1]]
|
||||||
|
|
||||||
|
Adding the 2nd element to all existing sets:
|
||||||
|
[[], [1]] ← 2 = [[], [1], [2], [1, 2]]
|
||||||
|
|
||||||
|
Adding the 3nd element to all existing sets:
|
||||||
|
[[], [1], [2], [1, 2]] ← 3 = [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
|
||||||
|
|
||||||
|
And so on for the rest of the elements from originalSet.
|
||||||
|
On every iteration the number of sets is doubled, so we'll get 2^n sets.
|
||||||
|
*/
|
||||||
|
for (let numIdx = 0; numIdx < originalSet.length; numIdx += 1) {
|
||||||
|
const existingSetsNum = sets.length;
|
||||||
|
|
||||||
|
for (let setIdx = 0; setIdx < existingSetsNum; setIdx += 1) {
|
||||||
|
const set = [...sets[setIdx], originalSet[numIdx]];
|
||||||
|
sets.push(set);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sets;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user