|
| 1 | +/** |
| 2 | + * Copyright 2012 Akseli Palén. |
| 3 | + * Created 2012-07-15. |
| 4 | + * Licensed under the MIT license. |
| 5 | + * |
| 6 | + * <license> |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 8 | + * a copy of this software and associated documentation files |
| 9 | + * (the "Software"), to deal in the Software without restriction, |
| 10 | + * including without limitation the rights to use, copy, modify, merge, |
| 11 | + * publish, distribute, sublicense, and/or sell copies of the Software, |
| 12 | + * and to permit persons to whom the Software is furnished to do so, |
| 13 | + * subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be |
| 16 | + * included in all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 22 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 23 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | + * SOFTWARE. |
| 26 | + * </lisence> |
| 27 | + * |
| 28 | + * Implements functions to calculate combinations of elements in JS Arrays. |
| 29 | + * |
| 30 | + * Functions: |
| 31 | + * k_combinations(set, k) -- Return all k-sized combinations in a set |
| 32 | + * combinations(set) -- Return all combinations of the set |
| 33 | + */ |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * K-combinations |
| 38 | + * |
| 39 | + * Get k-sized combinations of elements in a set. |
| 40 | + * |
| 41 | + * Usage: |
| 42 | + * k_combinations(set, k) |
| 43 | + * |
| 44 | + * Parameters: |
| 45 | + * set: Array of objects of any type. They are treated as unique. |
| 46 | + * k: size of combinations to search for. |
| 47 | + * |
| 48 | + * Return: |
| 49 | + * Array of found combinations, size of a combination is k. |
| 50 | + * |
| 51 | + * Examples: |
| 52 | + * |
| 53 | + * k_combinations([1, 2, 3], 1) |
| 54 | + * -> [[1], [2], [3]] |
| 55 | + * |
| 56 | + * k_combinations([1, 2, 3], 2) |
| 57 | + * -> [[1,2], [1,3], [2, 3] |
| 58 | + * |
| 59 | + * k_combinations([1, 2, 3], 3) |
| 60 | + * -> [[1, 2, 3]] |
| 61 | + * |
| 62 | + * k_combinations([1, 2, 3], 4) |
| 63 | + * -> [] |
| 64 | + * |
| 65 | + * k_combinations([1, 2, 3], 0) |
| 66 | + * -> [] |
| 67 | + * |
| 68 | + * k_combinations([1, 2, 3], -1) |
| 69 | + * -> [] |
| 70 | + * |
| 71 | + * k_combinations([], 0) |
| 72 | + * -> [] |
| 73 | + */ |
| 74 | +function k_combinations(set, k) { |
| 75 | + var i, j, combs, head, tailcombs; |
| 76 | + |
| 77 | + // There is no way to take e.g. sets of 5 elements from |
| 78 | + // a set of 4. |
| 79 | + if (k > set.length || k <= 0) { |
| 80 | + return []; |
| 81 | + } |
| 82 | + |
| 83 | + // K-sized set has only one K-sized subset. |
| 84 | + if (k == set.length) { |
| 85 | + return [set]; |
| 86 | + } |
| 87 | + |
| 88 | + // There is N 1-sized subsets in a N-sized set. |
| 89 | + if (k == 1) { |
| 90 | + combs = []; |
| 91 | + for (i = 0; i < set.length; i++) { |
| 92 | + combs.push([set[i]]); |
| 93 | + } |
| 94 | + return combs; |
| 95 | + } |
| 96 | + |
| 97 | + // Assert {1 < k < set.length} |
| 98 | + |
| 99 | + // Algorithm description: |
| 100 | + // To get k-combinations of a set, we want to join each element |
| 101 | + // with all (k-1)-combinations of the other elements. The set of |
| 102 | + // these k-sized sets would be the desired result. However, as we |
| 103 | + // represent sets with lists, we need to take duplicates into |
| 104 | + // account. To avoid producing duplicates and also unnecessary |
| 105 | + // computing, we use the following approach: each element i |
| 106 | + // divides the list into three: the preceding elements, the |
| 107 | + // current element i, and the subsequent elements. For the first |
| 108 | + // element, the list of preceding elements is empty. For element i, |
| 109 | + // we compute the (k-1)-computations of the subsequent elements, |
| 110 | + // join each with the element i, and store the joined to the set of |
| 111 | + // computed k-combinations. We do not need to take the preceding |
| 112 | + // elements into account, because they have already been the i:th |
| 113 | + // element so they are already computed and stored. When the length |
| 114 | + // of the subsequent list drops below (k-1), we cannot find any |
| 115 | + // (k-1)-combs, hence the upper limit for the iteration: |
| 116 | + combs = []; |
| 117 | + for (i = 0; i < set.length - k + 1; i++) { |
| 118 | + // head is a list that includes only our current element. |
| 119 | + head = set.slice(i, i + 1); |
| 120 | + // We take smaller combinations from the subsequent elements |
| 121 | + tailcombs = k_combinations(set.slice(i + 1), k - 1); |
| 122 | + // For each (k-1)-combination we join it with the current |
| 123 | + // and store it to the set of k-combinations. |
| 124 | + for (j = 0; j < tailcombs.length; j++) { |
| 125 | + combs.push(head.concat(tailcombs[j])); |
| 126 | + } |
| 127 | + } |
| 128 | + return combs; |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | +/** |
| 133 | + * Combinations |
| 134 | + * |
| 135 | + * Get all possible combinations of elements in a set. |
| 136 | + * |
| 137 | + * Usage: |
| 138 | + * combinations(set) |
| 139 | + * |
| 140 | + * Examples: |
| 141 | + * |
| 142 | + * combinations([1, 2, 3]) |
| 143 | + * -> [[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]] |
| 144 | + * |
| 145 | + * combinations([1]) |
| 146 | + * -> [[1]] |
| 147 | + */ |
| 148 | +function combinations(set) { |
| 149 | + var k, i, combs, k_combs; |
| 150 | + combs = []; |
| 151 | + |
| 152 | + // Calculate all non-empty k-combinations |
| 153 | + for (k = 1; k <= set.length; k++) { |
| 154 | + k_combs = k_combinations(set, k); |
| 155 | + for (i = 0; i < k_combs.length; i++) { |
| 156 | + combs.push(k_combs[i]); |
| 157 | + } |
| 158 | + } |
| 159 | + return combs; |
| 160 | +} |
| 161 | + |
| 162 | +module.exports = combinations |
0 commit comments