Example #1
0
        public static SpanningTree Create(List<Vertex> vertices, List<DelaunayEdge> edges)
        {
            Dictionary<Vertex, int> vertexIndex = new Dictionary<Vertex, int>();

            int i = 0;
            foreach (var v in vertices)
            {
                vertexIndex[v] = i++;
            }

            List<DelaunayEdge> mstEdges = new List<DelaunayEdge>();
            var sortedEdges = edges.Quicksort();

            int[] parents = new int[vertices.Count];
            for (int j = 0; j < vertices.Count; j++)
            {
                parents[j] = -1;
            }

            foreach (var edge in sortedEdges)
            {
                var startRoot = GetRootVertexIndex(parents, vertexIndex[edge.Start]);
                var endRoot = GetRootVertexIndex(parents, vertexIndex[edge.End]);
                if (startRoot != endRoot)
                {
                    mstEdges.Add(edge);
                    parents[endRoot] = startRoot;
                }
                if (mstEdges.Count == vertices.Count - 1)
                {
                    break;
                }
            }

            return new SpanningTree()
            {
                Edges = mstEdges
            };
        }
        public static void Main()
        {
            Console.WriteLine("*LastDigitComparer + BubbleSort: ");
            Console.WriteLine();

            var array = new List<int>() { 213, 412, 141, 445, 550 };
            var sortedArray = array.BubbleSort(new LastDigitComparer());

            foreach (var item in sortedArray)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            Console.WriteLine("BubbleSort with Delegate - Predicate for Compare Integers : ");
            Console.WriteLine();

            var secondArrey = new List<int>() { 213, 412, 141, 445, 550 };
            PredicateDelegate<int> myDelegate = CompareIntegers;
            var sortedArreyPredicate = secondArrey.BubbleSort(myDelegate);
            foreach (var item in sortedArreyPredicate)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            Console.WriteLine("*ReverseComparer<T> + StringLengthComparer + SelectionSort: ");
            Console.WriteLine();

            var strings = new List<string>() { "sss", "cccc", null, "aaaaa", "bbbssss", "A" };
            var stringsArray = strings.SelectionSort(new ReverseComparer<string>(new StringLengthComparer()));
            strings.Add("o");
            foreach (var item in stringsArray)
            {
                if (item == null)
                {
                    Console.WriteLine("\"null\"");
                }
                else
                {
                    Console.WriteLine(item);
                }
            }
            Console.WriteLine();

            Console.WriteLine("*OddEvenComparer + Quicksort: ");
            Console.WriteLine();

            var oddOrEven = new List<int?>() { 5, null, 2, 7, 8, 12, 10, 9, 8 };
            var oddOrEvenSorted = oddOrEven.Quicksort(new OddEvenComparer());

            foreach (var item in oddOrEvenSorted)
            {
                if (item == null)
                {
                    Console.WriteLine("\"null\"");
                }
                else
                {
                    Console.WriteLine(item);
                }
            }
            Console.WriteLine();
        }