In this tutorial, we’ll learn how to Print all possible combinations of r elements in a given array of size n in C# Console Application.
Permutations and combinations are part of a branch of mathematics called combinatorics, which involves studying finite, discrete structures. Permutations are specific selections of elements within a set where the order in which the elements are arranged is important, while combinations involve the selection of elements without regard for order.
Permutations
The calculator provided computes one of the most typical concepts of permutations where arrangements of a fixed number of elements r, are taken from a given set n. Essentially this can be referred to as r-permutations of n or partial permutations, denoted as nPr, nPr, P(n,r), or P(n,r)among others.
Example:
Combinations
Combinations are related to permutations in that they are essentially permutations where all the redundancies are removed (as will be described below), since order in a combination is not important. Combinations, like permutations, are denoted in various ways including nCr, nCr, C(n,r), or C(n,r)
Example:
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp10 { class Program { private static bool NextCombination(IList<int> num, int n, int k) { bool finished; var changed = finished = false; if (k <= 0) return false; for (var i = k - 1; !finished && !changed; i--) { if (num[i] < n - 1 - (k - 1) + i) { num[i]++; if (i < k - 1) for (var j = i + 1; j < k; j++) num[j] = num[j - 1] + 1; changed = true; } finished = i == 0; } return changed; } static IEnumerable<IEnumerable<T>> GetCombinations<T>(IEnumerable<T> list, int length) where T : IComparable { if (length == 1) return list.Select(t => new T[] { t }); return GetCombinations(list, length - 1) .SelectMany(t => list.Where(o => o.CompareTo(t.Last()) > 0), (t1, t2) => t1.Concat(new T[] { t2 })); } static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length) { if (length == 1) return list.Select(t => new T[] { t }); return GetPermutations(list, length - 1) .SelectMany(t => list.Where(o => !t.Contains(o)), (t1, t2) => t1.Concat(new T[] { t2 })); } private static void Main() { int countP = 0, countC = 0; const int k = 2; var n = new[] { "1", "2","3"}; Console.Write("n: "); foreach (var item in n) { Console.Write("{0} ", item); } Console.WriteLine(); Console.WriteLine("k: {0}", k); Console.WriteLine(); Console.WriteLine("==============================="); Console.WriteLine("Permutations"); Console.WriteLine("==============================="); foreach (IEnumerable<string> i in GetPermutations(n, k)) { Console.WriteLine(string.Join(" ", i)); countC++; } Console.WriteLine("Count : "+countC); Console.WriteLine(); Console.WriteLine("==============================="); Console.WriteLine("Combinations"); Console.WriteLine("==============================="); foreach (IEnumerable<string> i in GetCombinations(n, k)) { Console.WriteLine(string.Join(" ", i)); countP++; } Console.WriteLine("Count : " + countP); Console.ReadKey(); } } } |
Output:
.