mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-05 00:23:59 +08:00
Restructure folders.
This commit is contained in:
55
src/algorithms/sets/combinations/README.md
Normal file
55
src/algorithms/sets/combinations/README.md
Normal file
@ -0,0 +1,55 @@
|
||||
# Combinations
|
||||
|
||||
When the order doesn't matter, it is a **Combination**.
|
||||
|
||||
When the order **does** matter it is a **Permutation**.
|
||||
|
||||
**"My fruit salad is a combination of apples, grapes and bananas"**
|
||||
We don't care what order the fruits are in, they could also be
|
||||
"bananas, grapes and apples" or "grapes, apples and bananas",
|
||||
its the same fruit salad.
|
||||
|
||||
## Combinations without repetitions
|
||||
|
||||
This is how lotteries work. The numbers are drawn one at a
|
||||
time, and if we have the lucky numbers (no matter what order)
|
||||
we win!
|
||||
|
||||
No Repetition: such as lottery numbers `(2,14,15,27,30,33)`
|
||||
|
||||
**Number of combinations**
|
||||
|
||||

|
||||
|
||||
where `n` is the number of things to choose from, and we choose `r` of them,
|
||||
no repetition, order doesn't matter.
|
||||
|
||||
It is often called "n choose r" (such as "16 choose 3"). And is also known as the Binomial Coefficient.
|
||||
|
||||
## Combinations with repetitions
|
||||
|
||||
Repetition is Allowed: such as coins in your pocket `(5,5,5,10,10)`
|
||||
|
||||
Or let us say there are five flavours of icecream:
|
||||
`banana`, `chocolate`, `lemon`, `strawberry` and `vanilla`.
|
||||
|
||||
We can have three scoops. How many variations will there be?
|
||||
|
||||
Let's use letters for the flavours: `{b, c, l, s, v}`.
|
||||
Example selections include:
|
||||
|
||||
- `{c, c, c}` (3 scoops of chocolate)
|
||||
- `{b, l, v}` (one each of banana, lemon and vanilla)
|
||||
- `{b, v, v}` (one of banana, two of vanilla)
|
||||
|
||||
**Number of combinations**
|
||||
|
||||

|
||||
|
||||
Where `n` is the number of things to choose from, and we
|
||||
choose `r` of them. Repetition allowed,
|
||||
order doesn't matter.
|
||||
|
||||
## References
|
||||
|
||||
[Math Is Fun](https://www.mathsisfun.com/combinatorics/combinations-permutations.html)
|
@ -0,0 +1,59 @@
|
||||
import combineWithRepetitions from '../combineWithRepetitions';
|
||||
import factorial from '../../../math/factorial/factorial';
|
||||
|
||||
describe('combineWithRepetitions', () => {
|
||||
it('should combine string with repetitions', () => {
|
||||
expect(combineWithRepetitions(['A'], 1)).toEqual([
|
||||
['A'],
|
||||
]);
|
||||
|
||||
expect(combineWithRepetitions(['A', 'B'], 1)).toEqual([
|
||||
['A'],
|
||||
['B'],
|
||||
]);
|
||||
|
||||
expect(combineWithRepetitions(['A', 'B'], 2)).toEqual([
|
||||
['A', 'A'],
|
||||
['A', 'B'],
|
||||
['B', 'B'],
|
||||
]);
|
||||
|
||||
expect(combineWithRepetitions(['A', 'B'], 3)).toEqual([
|
||||
['A', 'A', 'A'],
|
||||
['A', 'A', 'B'],
|
||||
['A', 'B', 'B'],
|
||||
['B', 'B', 'B'],
|
||||
]);
|
||||
|
||||
expect(combineWithRepetitions(['A', 'B', 'C'], 2)).toEqual([
|
||||
['A', 'A'],
|
||||
['A', 'B'],
|
||||
['A', 'C'],
|
||||
['B', 'B'],
|
||||
['B', 'C'],
|
||||
['C', 'C'],
|
||||
]);
|
||||
|
||||
expect(combineWithRepetitions(['A', 'B', 'C'], 3)).toEqual([
|
||||
['A', 'A', 'A'],
|
||||
['A', 'A', 'B'],
|
||||
['A', 'A', 'C'],
|
||||
['A', 'B', 'B'],
|
||||
['A', 'B', 'C'],
|
||||
['A', 'C', 'C'],
|
||||
['B', 'B', 'B'],
|
||||
['B', 'B', 'C'],
|
||||
['B', 'C', 'C'],
|
||||
['C', 'C', 'C'],
|
||||
]);
|
||||
|
||||
const combinationOptions = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
|
||||
const combinationSlotsNumber = 4;
|
||||
const combinations = combineWithRepetitions(combinationOptions, combinationSlotsNumber);
|
||||
const n = combinationOptions.length;
|
||||
const r = combinationSlotsNumber;
|
||||
const expectedNumberOfCombinations = factorial((r + n) - 1) / (factorial(r) * factorial(n - 1));
|
||||
|
||||
expect(combinations.length).toBe(expectedNumberOfCombinations);
|
||||
});
|
||||
});
|
@ -0,0 +1,60 @@
|
||||
import combineWithoutRepetitions from '../combineWithoutRepetitions';
|
||||
import factorial from '../../../math/factorial/factorial';
|
||||
|
||||
describe('combineWithoutRepetitions', () => {
|
||||
it('should combine string without repetitions', () => {
|
||||
expect(combineWithoutRepetitions(['A', 'B'], 3)).toEqual([]);
|
||||
|
||||
expect(combineWithoutRepetitions(['A', 'B'], 1)).toEqual([
|
||||
['A'],
|
||||
['B'],
|
||||
]);
|
||||
|
||||
expect(combineWithoutRepetitions(['A'], 1)).toEqual([
|
||||
['A'],
|
||||
]);
|
||||
|
||||
expect(combineWithoutRepetitions(['A', 'B'], 2)).toEqual([
|
||||
['A', 'B'],
|
||||
]);
|
||||
|
||||
expect(combineWithoutRepetitions(['A', 'B', 'C'], 2)).toEqual([
|
||||
['A', 'B'],
|
||||
['A', 'C'],
|
||||
['B', 'C'],
|
||||
]);
|
||||
|
||||
expect(combineWithoutRepetitions(['A', 'B', 'C'], 3)).toEqual([
|
||||
['A', 'B', 'C'],
|
||||
]);
|
||||
|
||||
expect(combineWithoutRepetitions(['A', 'B', 'C', 'D'], 3)).toEqual([
|
||||
['A', 'B', 'C'],
|
||||
['A', 'B', 'D'],
|
||||
['A', 'C', 'D'],
|
||||
['B', 'C', 'D'],
|
||||
]);
|
||||
|
||||
expect(combineWithoutRepetitions(['A', 'B', 'C', 'D', 'E'], 3)).toEqual([
|
||||
['A', 'B', 'C'],
|
||||
['A', 'B', 'D'],
|
||||
['A', 'B', 'E'],
|
||||
['A', 'C', 'D'],
|
||||
['A', 'C', 'E'],
|
||||
['A', 'D', 'E'],
|
||||
['B', 'C', 'D'],
|
||||
['B', 'C', 'E'],
|
||||
['B', 'D', 'E'],
|
||||
['C', 'D', 'E'],
|
||||
]);
|
||||
|
||||
const combinationOptions = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
|
||||
const combinationSlotsNumber = 4;
|
||||
const combinations = combineWithoutRepetitions(combinationOptions, combinationSlotsNumber);
|
||||
const n = combinationOptions.length;
|
||||
const r = combinationSlotsNumber;
|
||||
const expectedNumberOfCombinations = factorial(n) / (factorial(r) * factorial(n - r));
|
||||
|
||||
expect(combinations.length).toBe(expectedNumberOfCombinations);
|
||||
});
|
||||
});
|
38
src/algorithms/sets/combinations/combineWithRepetitions.js
Normal file
38
src/algorithms/sets/combinations/combineWithRepetitions.js
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @param {*[]} combinationOptions
|
||||
* @param {number} combinationLength
|
||||
* @return {*[]}
|
||||
*/
|
||||
|
||||
export default function combineWithRepetitions(combinationOptions, combinationLength) {
|
||||
// If combination length equal to 0 then return empty combination.
|
||||
if (combinationLength === 0) {
|
||||
return [[]];
|
||||
}
|
||||
|
||||
// If combination options are empty then return "no-combinations" array.
|
||||
if (combinationOptions.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Init combinations array.
|
||||
const combos = [];
|
||||
|
||||
// Find all shorter combinations and attach head to each of those.
|
||||
const headCombo = [combinationOptions[0]];
|
||||
const shorterCombos = combineWithRepetitions(combinationOptions, combinationLength - 1);
|
||||
|
||||
for (let combinationIndex = 0; combinationIndex < shorterCombos.length; combinationIndex += 1) {
|
||||
const combo = headCombo.concat(shorterCombos[combinationIndex]);
|
||||
combos.push(combo);
|
||||
}
|
||||
|
||||
// Let's shift head to the right and calculate all the rest combinations.
|
||||
const combinationsWithoutHead = combineWithRepetitions(
|
||||
combinationOptions.slice(1),
|
||||
combinationLength,
|
||||
);
|
||||
|
||||
// Join all combinations and return them.
|
||||
return combos.concat(combinationsWithoutHead);
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
@see: https://stackoverflow.com/a/127898/7794070
|
||||
|
||||
Lets say your array of letters looks like this: "ABCDEFGH".
|
||||
You have three indices (i, j, k) indicating which letters you
|
||||
are going to use for the current word, You start with:
|
||||
|
||||
A B C D E F G H
|
||||
^ ^ ^
|
||||
i j k
|
||||
|
||||
First you vary k, so the next step looks like that:
|
||||
|
||||
A B C D E F G H
|
||||
^ ^ ^
|
||||
i j k
|
||||
|
||||
If you reached the end you go on and vary j and then k again.
|
||||
|
||||
A B C D E F G H
|
||||
^ ^ ^
|
||||
i j k
|
||||
|
||||
A B C D E F G H
|
||||
^ ^ ^
|
||||
i j k
|
||||
|
||||
Once you j reached G you start also to vary i.
|
||||
|
||||
A B C D E F G H
|
||||
^ ^ ^
|
||||
i j k
|
||||
|
||||
A B C D E F G H
|
||||
^ ^ ^
|
||||
i j k
|
||||
...
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {*[]} combinationOptions
|
||||
* @param {number} combinationLength
|
||||
* @return {*[]}
|
||||
*/
|
||||
export default function combineWithoutRepetitions(combinationOptions, combinationLength) {
|
||||
// If combination length is just 1 then return combinationOptions.
|
||||
if (combinationLength === 1) {
|
||||
return combinationOptions.map(option => [option]);
|
||||
}
|
||||
|
||||
// Init combinations array.
|
||||
const combinations = [];
|
||||
|
||||
for (let i = 0; i <= (combinationOptions.length - combinationLength); i += 1) {
|
||||
const smallerCombinations = combineWithoutRepetitions(
|
||||
combinationOptions.slice(i + 1),
|
||||
combinationLength - 1,
|
||||
);
|
||||
|
||||
for (let j = 0; j < smallerCombinations.length; j += 1) {
|
||||
const combination = [combinationOptions[i]].concat(smallerCombinations[j]);
|
||||
combinations.push(combination);
|
||||
}
|
||||
}
|
||||
|
||||
// Return all calculated combinations.
|
||||
return combinations;
|
||||
}
|
Reference in New Issue
Block a user