private static void CompareChooseKfromN(int chooseK, int fromN, CombinatoricMode mode) { var tst1 = Combinatorics.ChooseKfromN(chooseK, fromN, mode).ToArray(); var tst2 = Combinatorics.ChooseKfromN(chooseK, fromN, mode); // enter alternative-call here Compare(tst1, tst2); }
private void ApplyNumeric(int chooseK, int fromN, CombinatoricMode mode) { var sb = new StringBuilder(); var counter = 0; foreach (var result in Combinatorics.ChooseKfromN(chooseK, fromN, mode)) { sb.Append(string.Concat(result)).Append(" "); counter += 1; } txtOutput.Text = string.Concat(Preambel(chooseK, fromN, mode), counter, " results\n\n", sb.ToString()); }
private void Apply2Words(int chooseK, int fromN, CombinatoricMode mode) { //demo, how to transform the int-results to any other DataType: Just take them as Indicees of an Source-Element-Array (of any type). var sb = new StringBuilder(); var words = _rgxWords.Split(txtInput.Text.Trim()); if (words.Length != fromN) { MessageBox.Show("number of words must equal the fromN-Parameter"); return; } var counter = 0; foreach (var result in Combinatorics.ChooseKfromN(chooseK, fromN, mode)) { sb.AppendLine(string.Join(" ", result.Select(i => words[i]).ToArray())); counter += 1; } txtOutput.Text = string.Concat(Preambel(chooseK, fromN, mode), counter, " results\n\n", sb.ToString()); }
public static IEnumerable<int[]> ChooseKfromN(int chooseK, int fromN, CombinatoricMode mode) { var ubound = chooseK - 1; var nMax = fromN - 1; int iPivot = 0; switch (mode) { case CombinatoricMode.Combination_NoRepetition: // keeps the resultSet asc-ordered var resultSet = Enumerable.Range(0, chooseK).ToArray(); // init resultSet with ascending sequence do { for (var n = resultSet[ubound]; n <= nMax; n++) { // 1) output-loop: enumerate last digit and yield results resultSet[ubound] = n; yield return resultSet.ToArray(); } // 2) find incrementable pivot-element: must have a gap to its successor for (iPivot = ubound; iPivot-- > 0; ) if (resultSet[iPivot] < resultSet[iPivot + 1] - 1) break; if (iPivot < 0) yield break; // 4) // 3) "regeneration" var diff = resultSet[iPivot] + 1 - iPivot; // difference between pivotValue and its index // fill resultSet after pivot with ascending sequence keeps it ordered without Repetitions for (var i = iPivot; i <= ubound; i++) resultSet[i] = i + diff; } while (true); case CombinatoricMode.Combination_WithRepetitions: // keeps the resultSet asc-ordered resultSet = Enumerable.Repeat(0, chooseK).ToArray(); // init resultSet with 0 do { for (var n = resultSet[ubound]; n <= nMax; n++) { // 1) output-loop: enumerate last digit and yield results resultSet[ubound] = n; yield return resultSet.ToArray(); } // find incrementable position for (iPivot = ubound; iPivot-- > 0; ) if (resultSet[iPivot] < resultSet[iPivot + 1]) break; if (iPivot < 0) yield break; var pivotValue = resultSet[iPivot] + 1; // fill resultSet after pivot with same pivotValue keeps it ordered with Repetitions for (var i = iPivot; i <= ubound; i++) resultSet[i] = pivotValue; } while (true); case CombinatoricMode.Variation_WithRepetitions: resultSet = Enumerable.Repeat(0, chooseK).ToArray(); do { for (var n = resultSet[ubound]; n <= nMax; n++) { // 1) output-loop: enumerate last digit and yield results resultSet[ubound] = n; yield return resultSet.ToArray(); } for (iPivot = chooseK; iPivot-- > 0; ) { // find incrementable position, set others to 0 if (resultSet[iPivot] < nMax) break; else resultSet[iPivot] = 0; } if (iPivot < 0) yield break; resultSet[iPivot] += 1; } while (true); case CombinatoricMode.Variation_NoRepetition: // this is different, and results are not in lexicographic order // to each Combination_NoRepetition-result the Permutation-algo is applied // (Variation_WithRepetitions could be implemented analogously, but its actual Algo is faster, and ordered) foreach (var result in ChooseKfromN(chooseK, fromN, CombinatoricMode.Combination_NoRepetition)) { foreach (var perm in Permutation(result)) yield return perm; } break; } }
private string Preambel(int chooseK, int fromN, CombinatoricMode mode) { var sElementSet = string.Concat(Enumerable.Range(0, fromN)); return(string.Format("CombinatoricMode.{0}\nchoose {1} from {2} {{{3}}}\n", mode, chooseK, fromN, sElementSet)); }