forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a simple cascading solution to generate a Power Set (trekhleb#975
) * Add a simple cascading version of generating a PowerSet. * Update README. * Update README. * Update README.
- Loading branch information
Showing
4 changed files
with
115 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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], | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |