CombinatoricsCombinationsT Method (T, Boolean) |
Namespace: Accord.Math
// Let's say we would like to compute all the // possible combinations of elements (1,2,3,4): // int[] elements = new[] { 1, 2, 3, 4 }; // The number of possible subsets might be too large // to fit on memory. For this reason, we can compute // values on-the-fly using foreach: foreach (int[] combination in Combinatorics.Combinations(elements)) { // ... } // Or we could try to compute them all and store in an array: int[][] combinations = Combinatorics.Combinations(elements).ToArray(); // In either case, the result will be: int[][] expected = { new [] { 1 }, new [] { 2 }, new [] { 3 }, new [] { 4 }, new [] { 1, 2 }, new [] { 1, 3 }, new [] { 2, 3 }, new [] { 1, 4 }, new [] { 2, 4 }, new [] { 3, 4 }, new [] { 1, 2, 3 }, new [] { 1, 2, 4 }, new [] { 1, 3, 4 }, new [] { 2, 3, 4 }, new [] { 1, 2, 3, 4 } }; // Note: although the empty set is technically a subset // of all sets, it won't be included in the enumeration