Provides math operations for combinatorics.
$ dotnet add package Gapotchenko.FX.Math.CombinatoricsThe module provides math operations for combinatorics.
Permutation is a widely used operation that returns all possible permutations of elements in a sequence.
Let's take a look at the sample code:
using Gapotchenko.FX.Math.Combinatorics;
string[] seq = ["A", "B", "C"];
foreach (var i in Permutations.Of(seq))
Console.WriteLine(string.Join(" ", i));
The code produces the following output:
A B C
A C B
B A C
B C A
C A B
C B A
By default, the generated permutations are positional, e.g. if the input sequence contains some duplicates, there will be duplicate permutations in the output.
Let's see an example of non-unique permutations:
int[] seq = [1, 2, 1];
foreach (var i in Permutations.Of(seq))
Console.WriteLine(string.Join(" ", i.Select(x => x.ToString())));
The output contains some duplicates, which is expected:
1 2 1
1 1 2
2 1 1
2 1 1
1 1 2
1 2 1
But if you want to get the unique permutations, there exists a straightforward way (note the Distinct operation):
int[] seq = [1, 2, 1];
foreach (var i in Permutations.Of(seq).Distinct())
Console.WriteLine(string.Join(" ", i.Select(x => x.ToString())));
which will produce the following output:
1 1 2
1 2 1
2 1 1
An experienced engineer may spot that while that approach produces the correct result, it is not the most efficient way of achieving it.
It all comes to the number of elements processed by the Distinct operation.
The number of resulting permutations is n! where n is the size of the input sequence.
So if we take care to perform Distinct on the input sequence instead, we are going to achieve considerable savings in the number of operations and amount of used memory.
Like so:
foreach (var i in Permutations.Of(seq.Distinct()))
Console.WriteLine(string.Join(" ", i.Select(x => x.ToString())));
(Note that Distinct operation is now applied to the input sequence, supposedly making the whole algorithm more efficient while producing the same results)
This whole way of thinking stands true but Gapotchenko.FX.Math.Combinatorics goes ahead of that and provides the out-of-the-box support for such natural idiosyncrasies.
Whatever syntax is used: Permutations.Of(seq.Distinct()) or Permutations.Of(seq).Distinct(),
the algorithm complexity stays at bay thanks to the built-in optimizer that automatically chooses the best execution plan for a query.
Permutations.Of(...) is an explicit form of producing the permutations,
but the IEnumerable<T>.Permute() extension method for LINQ is also available as part of Gapotchenko.FX.Math.Combinatorics fluent API:
using Gapotchenko.FX.Math.Combinatorics;
foreach (var i in new[] { "A", "B", "C" }.Permute())
// ...
Another widespread combinatorial primitive is a Cartesian product:
using Gapotchenko.FX.Math.Combinatorics;
string[] seq1 = ["1", "2"];
string[] seq2 = ["A", "B", "C"];
foreach (var i in CartesianProduct.Of(seq1, seq2))
Console.WriteLine(string.Join(" ", i));
The output:
1 A
2 A
1 B
2 B
1 C
2 C
CartesianProduct.Of(...) is an explicit form of producing the Cartesian product,
but the IEnumerable<T>.CrossJoin(...) extension method for LINQ is also available as a part of Gapotchenko.FX.Math.Combinatorics fluent API:
using Gapotchenko.FX.Math.Combinatorics;
foreach (var i in new[] { "1", "2" }.CrossJoin(["A", "B", "C"]))
// ...
Note the naming difference between CartesianProduct and CrossJoin.
LINQ is historically built around data access,
and data access, in turn, historically uses the term cross join for Cartesian product.
Gapotchenko.FX.Math.Combinatorics.PermutationsGapotchenko.FX.Math.Combinatorics.CartesianProductLet's continue with a look at some other modules provided by Gapotchenko.FX:
Or look at the full list of modules.