Tag Archives: combinatorics

Enumerate pairs of objects in arrays or IEnumerables in C#

Sometimes it’s useful to enumerate pairs of objects in an array or enumerable. For example, if you have an array holding volleyball teams, the pairs would list each of the possible matches you could make. If the teams are A, … Continue reading

Posted in arrays, combinatorics, extension methods | Tagged , , , , , , , , , , , , , | Leave a comment

Select N random items from an array in C#

This example builds a generic extension method that lets you easily select N random objects from an array. For example, it lets you select 5 items from an array holding 10 names. Picking one random item from an array is … Continue reading

Posted in algorithms, arrays, mathematics, variables | Tagged , , , , , , , , , , , , , | 1 Comment

Send secret Santa emails in C#

This example shows how you can automatically send secret Santa emails for a party. The example Pick a secret Santa assignment in C# shows how you can quickly make secret Santa assignments. The post Calculate the number of secret Santa … Continue reading

Posted in algorithms, combinatorics, mathematics | Tagged , , , , , , , , , , , , , | Leave a comment

Calculate the number of secret Santa permutations in C#

The example Pick a secret Santa assignment in C# picks a valid permutation of the people so no one is mapped to himself or herself. Such a permutation is called a derangement. See that example for details. The number of … Continue reading

Posted in algorithms, combinatorics, mathematics | Tagged , , , , , , , , , , , , | 4 Comments

Pick a secret Santa assignment in C#

In a secret Santa party, you put everyone’s names in a hat and people draw from the hat. Each person becomes the secret Santa for the person he draws and gets that person a present for the party. To keep … Continue reading

Posted in algorithms, combinatorics, mathematics | Tagged , , , , , , , , , , , | 2 Comments

Calculate the factorial function and use Debug.Assert in C#

The factorial of a number N is written N! and has the value 1 * 2 * 3 * … * N. By definition, 0! = 1. The value N! gives the number of permutations of a set of N … Continue reading

Posted in algorithms, combinatorics, debugging, mathematics | Tagged , , , , , , , , , , , | Leave a comment

Generate all selections of K items from a set of N items in C#

This example is somewhat similar to Generate all of the permutations of a set of objects in C#. The basic idea is to use a recursive method to assign the next item to the combination. The first call to the … Continue reading

Posted in algorithms, combinatorics, mathematics | Tagged , , , , , , , , , , , , , , , | 3 Comments

Calculate the binomial coefficient “N choose K” efficiently in C#

The binomial coefficient, written and pronounced “n choose k,” is the number of ways you can pick k items from a set of n items. For example, suppose you have a deck of 5 cards (n = 5) and you … Continue reading

Posted in algorithms, mathematics | Tagged , , , , , , , , , , , , , | 9 Comments

Generate all of the permutations of a set of objects in C#

The basic idea is to use a recursive method to assign the next item in a permutation. The first call to the method assigns the permutation’s first item, the next call assigns the second item, and so forth. To assign … Continue reading

Posted in algorithms, combinatorics, mathematics, recursion | Tagged , , , , , , , , , , | 10 Comments