Combinatorics

By at February 15, 2008 06:31
Filed Under:

Here's a basic class to provide enumerable permutations & combinations ...

public class Combinatorics
{
    public static IEnumerable<IEnumerable<T>> Permutations<T>(IList<T> items, int setSize)
    {
        if (setSize > 0)
            foreach (T item in items)
            {
                IList<T> remainingItems = items.Where(n => !item.Equals(n)).ToList();

                foreach (IEnumerable<T> remainingPermutations in Permutations(remainingItems, setSize - 1))
                    yield return Enumerable.Concat<T>(new T[] { item }, remainingPermutations);
            }
        else
            yield return new T[0];
    }


    public static IEnumerable<IEnumerable<T>> Combinations<T>(IList<T> items, int setSize)
    {
        int[] a = new int[setSize];
        List<T> returnList = new List<T>(setSize);

        for (int i = 0; i < a.Length; i++)
            a[i] = i;

        foreach (int item in a)
            returnList.Add(items[item]);

        yield return returnList.ToArray();

        long numLeft = GetTotalNumberOfCombinations(items.Count, setSize);

        while (numLeft > 1)
        {
            int i = setSize - 1;
            while (a[i] == items.Count - setSize + i)
                i--;

            a[i] = a[i] + 1;
            for (int j = i + 1; j < setSize; j++)
                a[j] = a[i] + j - i;

            numLeft--;

            returnList.Clear();

            foreach (int item in a)
                returnList.Add(items[item]);

            yield return returnList.ToArray();

        } 
    }

    private static long GetTotalNumberOfCombinations(long itemCount, long setSize)
    {
        //takes advantage of the fact that COMBIN(100,97) == COMBIN(100,3) 
        // where's BigInteger when you need it ? 
        
        if (itemCount < setSize) return 0;
        if (itemCount == setSize) return 1;

        long delta, iMax;

        delta = setSize < itemCount - setSize ? itemCount - setSize : setSize;
        iMax = setSize < itemCount - setSize ? setSize : itemCount - setSize;
        
        long returnValue = delta + 1;

        for (long i = 2; i <= iMax; ++i)
            checked { returnValue = (returnValue * (delta + i)) / i; }

        return returnValue;
    }
}

Comments

3/20/2010 11:40:36 PM #

cheap prom Dresses

nice  post!

cheap prom Dresses People's Republic of China | Reply

4/19/2010 2:41:41 AM #

Someone

I have read several of the articles on this website today, and I sincerely love your technique of writing. I bookmarked it and will be checking back soon.

Someone Taiwan | Reply

4/27/2010 1:45:47 PM #

Get ripped body

I was looking for this the other day. i dont usually post in blogbut i wanted to say thank you!

Get ripped body United States | Reply

6/2/2010 2:57:55 PM #

how cast

Your blog is fine.  I just want to comment on the design.  Its too loud.  Its doing way too much and it takes away from what youve got to say --which I think is really important.  I dont know if you didnt think that your words could hold everyones attention, but you were wrong.  

how cast United States | Reply

6/5/2010 4:42:43 AM #

How To Get Back An Ex

Fantastic blog!  I dont think Ive seen all the angles of this subject the way youve pointed them out.  Youre a true star, a rock star man.  Youve got so much to say and know so much about the subject that I think you should just teach a class about it...HaHa!

How To Get Back An Ex United States | Reply

7/7/2010 9:59:44 AM #

Larita Amoa

Perhaps you have considered adding more video clips to your weblog posts to keep the visitors more entertained? I mean I just read through the whole piece of yours and it had been quite enjoyable but since I'm mare like a visual learner,I found that to be further helpful well let me know how it turns out! I enjoy what you guys are always up too. Such intelligent work and reporting! Carry on the great works guys I've added you guys to my blogroll. This is a good article thanks for sharing this informative information.. I'll visit your blog regularly for some latest post.

Larita Amoa Rwanda | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading