Click here to Skip to main content
13,249,813 members (70,433 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 16 Nov 2009

A C# Combinations Iterator

, 16 Nov 2009
Rate this:
Please Sign up or sign in to vote.
An iterator over all combinations of (m) elements from a sequence of (n) elements


In my previous article, 'A C# Permutation Iterator', I presented a compact way to permutate over a sequence. In this article, I will expand my iterator to include combinations.

Using the Code

This iterator works recursively. The recursion terminal condition is when we elect to choose 0 elements, in which case the sequence is returned as is. In the recursive case, it is a bit more complex than the permutation iterator. The algorithm works by holding the first element, and making a recursive call on the rest of the sequence. This is done iteratively (n) times where (n) is the length of the sequence. However, each progressive iteration calls the recursion with an ever decreasing sequence length. As an example, here is a depiction of the process when choosing 2 elements from a sequence of 5 (the result of each iteration is the first 2 elements):

Sequence yieldedCommentsResult (the first 2 elements)
12345The initial sequence and first result12
13452Inner rotation (starting at the 2nd element)13
14523Inner rotation14
15234Inner rotation15
23451Outer rotation23
24531Inner rotation - Note that the last element is now excluded from the rotation24
25341Inner rotation25
34512Outer rotation34
35412Inner rotation - Note how the last two elements are now excluded from the rotation35
45123Outer rotation45

And here is the code:

private static void RotateLeft<T>(IList<T> sequence, int start, int count)
    T tmp = sequence[start];
    sequence.Insert(start + count - 1, tmp);

public static IEnumerable<IList<T>> Combinations<T>(
    IList<T> sequence, 
    int start, 
    int count, 
    int choose)
    if (choose == 0) yield return sequence;
        for (int i = 0; i < count; i++)
            foreach (var perm in Combinations(
                                        start + 1,
                                        count - 1 - i,
                                        choose - 1))
                yield return perm;
            RotateLeft(sequence, start, count);

Note that each iteration returns a full length sequence, however the result is actually contained in the first (m) elements, where (m) is the number of elements we elected to choose from the sequence.

The initial call to the Combinations function should pass 0 as the start parameter and the sequence length as the count parameter. To make it easier, we can overload the call thus:

public static IEnumerable<IList<T>> Combinations<T>(
    IList<T> sequence, 
    int choose)
    return Combinations(sequence, 0, sequence.Count, choose);

Following is an example of iterating over combinations of 3 characters over the string "abcdef". Note how I am using the Take extension method to grab only the result part from the iteration result.

private static void CombinationsOverString(string s, int count)
    foreach (var comb in Iterator.Combinations(s.ToCharArray().ToList(), count))
        string r = new string(comb.Take(count).ToArray());
        Console.Write("{0,-8}", r);


abc     abd     abe     abf     acd     ace     acf     ade     adf     aef
bcd     bce     bcf     bde     bdf     bef     cde     cdf     cef     def

And here is an example of permutations over these combinations using the permutation iterator from my previous article.

private static void CombinationsPermutations(string s, int count)
    foreach (var combo in Iterator.Combinations(
        foreach (var permu in Iterator.Permutations(combo, count))
            string r = new string(permu.Take(count).ToArray());
            Console.Write("{0,-8}", r);


abc     bac     cab     acb     bca     cba     abd     bad     dab     adb
bda     dba     abe     bae     eab     aeb     bea     eba     abf     baf
fab     afb     bfa     fba     acd     cad     dac     adc     cda     dca
ace     cae     eac     aec     cea     eca     acf     caf     fac     afc
cfa     fca     ade     dae     ead     aed     dea     eda     adf     daf
fad     afd     dfa     fda     aef     eaf     fae     afe     efa     fea
bcd     cbd     dbc     bdc     cdb     dcb     bce     cbe     ebc     bec
ceb     ecb     bcf     cbf     fbc     bfc     cfb     fcb     bde     dbe
ebd     bed     deb     edb     bdf     dbf     fbd     bfd     dfb     fdb
bef     ebf     fbe     bfe     efb     feb     cde     dce     ecd     ced
dec     edc     cdf     dcf     fcd     cfd     dfc     fdc     cef     ecf
fce     cfe     efc     fec     def     edf     fde     dfe     efd     fed

Points of Interest

There are many implementations of combination and permutation iterators to be found on the internet, however I particularly like this one due to the fact that it is light on memory, and doesn't create any unnecessary objects. The corollary of this, is that the sequence is modified while the iterator is running; if this is not desired, the iteration should be run on a copy of the original sequence.


  • 16-Nov-2009 - Initial posting


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Aviad P.
Software Developer (Senior)
Israel Israel
Software developer since 1984, currently specializing in C# and .NET.

You may also be interested in...


Comments and Discussions

GeneralI like this Pin
NightJammer16-Nov-09 22:56
memberNightJammer16-Nov-09 22:56 
GeneralRecursion Pin
PIEBALDconsult16-Nov-09 5:56
memberPIEBALDconsult16-Nov-09 5:56 
GeneralRe: Recursion Pin
Aviad P.16-Nov-09 7:42
memberAviad P.16-Nov-09 7:42 
GeneralRe: Recursion Pin
PIEBALDconsult16-Nov-09 9:22
memberPIEBALDconsult16-Nov-09 9:22 
GeneralRe: Recursion Pin
Aviad P.16-Nov-09 10:17
memberAviad P.16-Nov-09 10:17 
GeneralRe: Recursion [modified] Pin
mb1816-Nov-09 11:31
membermb1816-Nov-09 11:31 
GeneralRe: Recursion Pin
Aviad P.17-Nov-09 5:09
memberAviad P.17-Nov-09 5:09 
GeneralRe: Recursion Pin
PIEBALDconsult16-Nov-09 15:38
memberPIEBALDconsult16-Nov-09 15:38 
GeneralRe: Recursion Pin
Lee N Middleton6-Dec-10 7:12
memberLee N Middleton6-Dec-10 7:12 
GeneralRe: Recursion Pin
PIEBALDconsult6-Dec-10 15:08
mvpPIEBALDconsult6-Dec-10 15:08 
Are you asking for one? I'm pretty sure I have one. I know I did combinations and permutations at least once in the last year.
GeneralRe: Recursion Pin
Lee N Middleton7-Dec-10 6:02
memberLee N Middleton7-Dec-10 6:02 
GeneralRe: Recursion Pin
PIEBALDconsult7-Dec-10 19:13
mvpPIEBALDconsult7-Dec-10 19:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 16 Nov 2009
Article Copyright 2009 by Aviad P.
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid