Gapotchenko.FX.Math.Combinatorics 2022.2.7

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
.NET 5.0 .NET Core 2.0 .NET Standard 2.0 .NET Framework 4.6
dotnet add package Gapotchenko.FX.Math.Combinatorics --version 2022.2.7
NuGet\Install-Package Gapotchenko.FX.Math.Combinatorics -Version 2022.2.7
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Gapotchenko.FX.Math.Combinatorics" Version="2022.2.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gapotchenko.FX.Math.Combinatorics --version 2022.2.7
#r "nuget: Gapotchenko.FX.Math.Combinatorics, 2022.2.7"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Gapotchenko.FX.Math.Combinatorics as a Cake Addin
#addin nuget:?package=Gapotchenko.FX.Math.Combinatorics&version=2022.2.7

// Install Gapotchenko.FX.Math.Combinatorics as a Cake Tool
#tool nuget:?package=Gapotchenko.FX.Math.Combinatorics&version=2022.2.7

Overview

The module provides the math operations for combinatorics.

Permutations

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;

var seq = new[] { "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

Unique Permutations

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:

var seq = new[] { 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):

var seq = new[] { 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 will definitely 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 top-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 preferred: Permutations.Of(seq.Distinct()) or Permutations.Of(seq).Distinct(), the algorithm complexity stays at bay thanks to the built-in optimizer that chooses the best execution plan for a query automatically.

Permutations in LINQ

Permutations.Of(...) is an explicit form of producing the permutations, but the LINQ shorthand Permute() is also available as a part of the fluent API:

using Gapotchenko.FX.Math.Combinatorics;

foreach (var i in new[] { "A", "B", "C" }.Permute())
    // ...

Cartesian Product

Another widespread combinatorial primitive is a Cartesian product:

using Gapotchenko.FX.Math.Combinatorics;

var seq1 = new[] { "1", "2" };
var seq2 = new[] { "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

Cartesian Product in LINQ

CartesianProduct.Of(...) is an explicit form of producing the Cartesian product, but the LINQ shorthand CrossJoin() is also available as a part of the fluent API:

using Gapotchenko.FX.Math.Combinatorics;

foreach (var i in new[] { "1", "2" }.CrossJoin(new[] { "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.

Commonly Used Types

  • Gapotchenko.FX.Math.Combinatorics.Permutations
  • Gapotchenko.FX.Math.Combinatorics.CartesianProduct

Other Modules

Let's continue with a look at some other modules provided by Gapotchenko.FX:

Or look at the full list of modules.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net46 net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2022.2.7 263 5/1/2022
2022.2.5 221 5/1/2022
2022.1.4 232 4/6/2022
2021.2.21 251 1/21/2022
2021.2.20 253 1/17/2022
2021.1.5 298 7/6/2021
2020.2.2-beta 270 11/21/2020