简体   繁体   中英

Permutation Algorithm OutOfMemory Exception

Please help me to how to fix OutOfMemory exception using below code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Permutation
{
    class Program
    {
        static void Main(string[] args)
        {
            CreatePartsByFreezingEachElementOnce("abcedfghijk");

            PrintPossiblePermutations(false);
            Console.WriteLine("---------------");

            PrintPossiblePermutations(true);
            Console.ReadLine();
        }

        static void PrintPossiblePermutations(bool unique)
        {
            var allPermutations = new List<string>();
            foreach (var item in _listWithFreezedKey)

                if (item.Item2.Count() == 2)
                {
                    allPermutations.Add(Swap(String.Join(",", item.Item1), item.Item2[0], item.Item2[1]));
                    allPermutations.Add(Swap(String.Join(",", item.Item1), item.Item2[1], item.Item2[0]));
                }

            if (unique)
            {
                var uniuePermutations = allPermutations.Distinct();
                //   PrintPermutations(uniuePermutations.ToList());
                Console.WriteLine(uniuePermutations.Count());
            }
            else
            {
                //    PrintPermutations(allPermutations);
                Console.WriteLine(allPermutations.Count());
            }

        }

        static void PrintPermutations(List<string> permutations)
        {
            int i = 1;
            foreach (var item in permutations)
            {
                Console.WriteLine(string.Format("{0}    :{1}", i, item));
                i++;
            }
        }

        static List<Tuple<List<char>, List<char>>> _listWithFreezedKey = new List<Tuple<List<char>, List<char>>>();
      static void CreatePartsByFreezingEachElementOnce(string str, List<char> indexToFreeze = null)
                    {
                        List<Tuple<List<char>, List<char>>> _innerlistWithFreezedKey = new List<Tuple<List<char>, List<char>>>();


                        var arr = str.ToCharArray().ToList();
                        var copy = arr;
                        if (indexToFreeze == null)
                        {
                            indexToFreeze = new List<char>();
                        }
                        for (int i = 0; i < arr.Count(); i++)
                        {
                            copy = str.ToCharArray().ToList();
                            var nth = arr[i];
                            copy.RemoveAt(i);

                            indexToFreeze.Add(nth);

                            _listWithFreezedKey.Add(new Tuple<List<char>, List<char>>(indexToFreeze.ToList(), copy));
                            _innerlistWithFreezedKey.Add(new Tuple<List<char>, List<char>>(indexToFreeze.ToList(), copy));

                            indexToFreeze.RemoveAt(indexToFreeze.Count() - 1);
                        }

                        foreach (var item in _innerlistWithFreezedKey)
                        {                              
                                List<char> l = item.Item2;
                                CreatePartsByFreezingEachElementOnce(String.Join("", l), item.Item1);
                         }


                        }

        static string Swap(string frezedPart, char swapChar1, char swapChar2)
        {
            return frezedPart + "," + swapChar1 + "," + swapChar2;
        }
    }
}

If you run this code using 10 chrs, it throws out of memory exception. But for 9 chars , it returns result.

It was my interview question to write a code such that it should not go out of memory if big data passed.

Thanks,

Your problem is that you want to generate all permutations at once, instead of generating them one-by-one. You are looking for an algorithm which produces the next permutation.

See the first page of Knuth's book on generating permutations.

I would like to answer my question by myself because it may help someone as well.

I will use Iterator Design Pattern to remove unused elements from memory and will convert collection into sequence.

Thanks,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM