Math.NET Documentation - Free & Open Mathematics for .NET

Math.NET
RSS

Navigation




Quick Search
»
Advanced Search »

Math.NET Project


Wiki

New Admin Files Login Profile

PoweredBy
Combinatorics is all about sets and how you can select and order some of their elements. Math.NET Iridium can help you computing such combinatoric problems, whether it is about counting the number of possibilities or to actually generate some of these possibilities randomly.

This page is a Draft, its content is not complete and might contain errors.

Permutations - How to order all distinguishable elements

  • Situation: You have a set of N elements which are all distinguishable
  • Problem: There are various ways how you can order the elements into a list. In how many ways can you order them? How to generate such orders randomly?

Counting Permutations

There are two ways to order two elements A,B: AB and BA.
There are six ways to order three elements A,B,C: ABC,ACB,BAC,BCA,CAB,CBA.
But how many ways are there to order N elements? Iridium provides the following method to compute it:

int numberOfElements = 100;
double res = Combinatorics.Permutations(numberOfElements);
 --> res = 9.3326e+157

Note that these numbers get huge very fast. The number of permutations of 100 elements as computed above already has 157 digits!

Generating Random Permutations

To generate a random permutation of N Elements simply call RandomPermutation. It returns an array of the N element indices (0 to N-1) in a random order:

int numberOfElements = 10;
int[] res = Combinatorics.RandomPermutation(numberOfElements);
 --> res for example 6 8 3 1 2 5 9 7 0 4

If you already have a list of elements you would like to generate a random permutation of (that is, to shuffle the elements), there are two simpler methods that work directly on such arbitrary lists (one of them inplace, hence changing the input list directly, and one of them outplace, hence generating a new list without changing the input list):

string[] input = new string[] {"Apple", "Orange", "Banana"};
string[] output = new string[input.Length];
Combinatorics.RandomShuffle(input, output);
 --> output for example Orange Apple Banana

string[] input = new string[] {"Apple", "Orange", "Banana"};
Combinatorics.RandomShuffle(input);
 --> input for example Banana Orange Apple

Combinations -

General Principles

Large Numbers -> Ln ....

Math.NET, a mathematical opensource .Net project by Christoph Rüegg and contributors.