Ejemplo n.º 1
0
        /// <summary>
        /// Underlying implementation for Subsets() overload.
        /// </summary>
        /// <typeparam name="T">The type of the elements in the sequence</typeparam>
        /// <param name="sequence">Sequence for which to produce subsets</param>
        /// <returns>Sequence of lists representing all subsets of a sequence</returns>

        private static IEnumerable <IList <T> > SubsetsImpl <T>(IEnumerable <T> sequence)
        {
            var sequenceAsList = sequence.ToList();
            var sequenceLength = sequenceAsList.Count;

            // the first subset is the empty set
            yield return(new List <T>());

            // all other subsets are computed using the subset generator
            // this check also resolves the case of permuting empty sets
            if (sequenceLength > 0)
            {
                for (var i = 1; i < sequenceLength; i++)
                {
                    // each intermediate subset is a lexographically ordered K-subset
                    var subsetGenerator = new SubsetGenerator <T>(sequenceAsList, i);
                    foreach (var subset in subsetGenerator)
                    {
                        yield return(subset);
                    }
                }

                yield return(sequenceAsList); // the last subet is the original set itself
            }
        }
Ejemplo n.º 2
0
        private static void Main()
        {
            foreach (List <int> ints in SubsetGenerator.BacktrackingGenerator(new List <int> {
                1, 2, 3, 4
            }, 0))
            {
                foreach (int i in ints)
                {
                    Console.Write(i + " ");
                }

                Console.WriteLine();
            }

            foreach (IEnumerable <int> enumerable in SubsetGenerator.LexicalGenerator(new List <int> {
                4, 3, 2, 1
            }))
            {
                foreach (int i in enumerable)
                {
                    Console.Write(i + " ");
                }

                Console.WriteLine();
            }

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


            foreach (int[] ints in KSubsetGenerator.BacktrackingGenerator(5, 4))
            {
                foreach (int i in ints)
                {
                    Console.Write(i + " ");
                }

                Console.WriteLine();
            }

            KSubsetGenerator.LexicalGenerator(5, 4);

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

            new PermutationGenerator().BacktrackingGeneration(new[] { 1, 2, 3, 4 }.ToArray(), 0, 3);

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

            //foreach (int[] subset in new PermutationGenerator().LexicalGeneration(new[] { 1, 2, 3, 4 }))
            //{
            //    foreach (int element in subset)
            //    {
            //        Console.Write(element + " ");
            //    }
            //    Console.WriteLine();
            //}

            var arr = new[] { 1, 2, 3, 4 };

            foreach (int i in arr)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine();

            while (!PermutationGenerator.NextPermutation(arr))
            {
                foreach (int i in arr)
                {
                    Console.Write(i + " ");
                }

                Console.WriteLine();
            }
        }
Ejemplo n.º 3
0
        /**
         *      solution:
         *      solution: True
         *      solution: True,True
         *      solution: True,True,True<== IsASolution
         *      { c a t}
         *      solution: True,True,False<== IsASolution
         *      { c a}
         *      solution: True,False
         *      solution: True,False,True<== IsASolution
         *      { c t}
         *      solution: True,False,False<== IsASolution
         *      { c}
         *      solution: False
         *      solution: False,True
         *      solution: False,True,True<== IsASolution
         *      { a t}
         *      solution: False,True,False<== IsASolution
         *      { a}
         *      solution: False,False
         *      solution: False,False,True<== IsASolution
         *      { t}
         *      solution: False,False,False<== IsASolution
         *      {}		 */
        public static void RunTests()
        {
            SubsetGenerator subsetGenerator = new SubsetGenerator("cat");

            subsetGenerator.GenerateSubsets();
        }