Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

permutation in c#

Tags:

c#

Is it possible to generate all permutations of a collection in c#?

char[] inputSet = { 'A','B','C' };
Permutations<char> permutations = new Permutations<char>(inputSet);
foreach (IList<char> p in permutations)
{
   Console.WriteLine(String.Format("{{{0} {1} {2}}}", p[0], p[1], p[2]));
}
like image 205
Ramya Avatar asked Jul 22 '10 09:07

Ramya


3 Answers

I've already faced the problem and I wrote these simple methods:

    public static IList<T[]> GeneratePermutations<T>(T[] objs, long? limit)
    {
        var result = new List<T[]>();
        long n = Factorial(objs.Length);
        n = (!limit.HasValue || limit.Value > n) ? n : (limit.Value);

        for (long k = 0; k < n; k++)
        {
            T[] kperm = GenerateKthPermutation<T>(k, objs);
            result.Add(kperm);
        }

        return result;
    }

    public static T[] GenerateKthPermutation<T>(long k, T[] objs)
    {
        T[] permutedObjs = new T[objs.Length];

        for (int i = 0; i < objs.Length; i++)
        {
            permutedObjs[i] = objs[i];
        }
        for (int j = 2; j < objs.Length + 1; j++)
        {
            k = k / (j - 1);                      // integer division cuts off the remainder
            long i1 = (k % j);
            long i2 = j - 1;
            if (i1 != i2)
            {
                T tmpObj1 = permutedObjs[i1];
                T tmpObj2 = permutedObjs[i2];
                permutedObjs[i1] = tmpObj2;
                permutedObjs[i2] = tmpObj1;
            }
        }
        return permutedObjs;
    }

    public static long Factorial(int n)
    {
        if (n < 0) { throw new Exception("Unaccepted input for factorial"); }    //error result - undefined
        if (n > 256) { throw new Exception("Input too big for factorial"); }  //error result - input is too big

        if (n == 0) { return 1; }

        // Calculate the factorial iteratively rather than recursively:

        long tempResult = 1;
        for (int i = 1; i <= n; i++)
        {
            tempResult *= i;
        }
        return tempResult;
    }

Usage:

var perms = Utilities.GeneratePermutations<char>(new char[]{'A','B','C'}, null);
like image 144
digEmAll Avatar answered Sep 22 '22 01:09

digEmAll


There's nothing built in.

I've found a a couple of Code Project articles here and here which might help you implement your own class.

like image 29
ChrisF Avatar answered Sep 20 '22 01:09

ChrisF


    public static void Recursion(char[] charList, int loBound, int upBound )
    {
            for (int i = loBound; i <= upBound; i++)
            {

                swap(ref charList[loBound], ref charList[i]);
                if (loBound == upBound)
                {
                    Console.Write(charList);
                    Console.WriteLine("");
                }

                Recursion(charList, loBound + 1, upBound);
                swap(ref charList[loBound], ref charList[i]);
            }

        }



    public static void swap(ref char a, ref char b)
    {
        if (a == b) return;
        a ^= b;
        b ^= a;
        a ^= b;
    }

    public static void Main(string[] args)
    {
        string c = "123";
        char[] c2 = c.ToCharArray();
        Recursion(c2, 0, c2.Length-1);
        Console.ReadKey();
    }
like image 25
ObjectMatrix Avatar answered Sep 19 '22 01:09

ObjectMatrix