//TODO: quick sort and take the kth element
        public int FindKthLargest_HeapImplementation(int[] nums, int k)
        {
            //max-heapify and extract the first k numbers
            MaxHeap <int> maxHeap = new MaxHeap <int>(nums.ToList());

            for (int i = 0; i < k - 1; i++)
            {
                maxHeap.ExtractMax();
            }

            return(maxHeap.ExtractMax());
        }
Пример #2
0
        public static T[] Sort(T[] input)
        {
            var heap = new MaxHeap <T>(input.Length, input);

            for (int i = input.Length - 1; i >= 1; i--)
            {
                var tmp = heap.array[0];
                heap.array[0] = heap.array[i];
                heap.heapSize--;
                heap.Heapify(0);
            }
            return(heap.array);
        }
Пример #3
0
        // usage
        public static int minSum(List <int> num, int k)
        {
            MaxHeap heap = new MaxHeap();

            // fill the heap.
            heap.Heapify(num, num.Count);
            HeapNode data;

            for (int i = 0; i < k; i++)
            {
                data = heap.GetMax();
                int newValue = (int)Math.Ceiling(num[data.Index] / 2.0);
                num[data.Index] = newValue;
                heap.ChangeMax(newValue);
            }

            return(num.Sum());
        }
Пример #4
0
        public static int[] HeapSort(int[] inputArray)
        {
            var tempArray = new int[inputArray.Length];

            tempArray = inputArray.Select(item => item).ToArray();

            var heap = new MaxHeap <int>();

            foreach (var item in tempArray)
            {
                heap.Add(item);
            }

            for (var i = tempArray.Length - 1; i >= 0; i--)
            {
                tempArray[i] = heap.RemoveMaxAndReturnItsValue();
            }

            return(tempArray);
        }
Пример #5
0
        private static void MaxHeapTest()
        {
            var intHeap = new MaxHeap <int>();

            intHeap.Add(24);
            intHeap.Add(37);
            intHeap.Add(17);
            intHeap.Add(28);
            intHeap.Add(31);
            intHeap.Add(29);
            intHeap.Add(15);
            intHeap.Add(12);
            intHeap.Add(20);
            intHeap.Add(40);

            WriteHeapToConsole(intHeap);

            while (intHeap.Count > 0)
            {
                intHeap.RemoveMaxAndReturnItsValue();
                WriteHeapToConsole(intHeap);
            }
        }
        public int MaximumBWUsingHeap(Graph graph, int source, int destination)
        {
            elapsed = 0;
            var watch = Stopwatch.StartNew();

            Dad       = new int[graph.NumberOfVertices];
            Bandwidth = new int[graph.NumberOfVertices];
            Status    = new int[graph.NumberOfVertices];
            int     vertices = graph.NumberOfVertices;
            MaxHeap maxHeap  = new MaxHeap(graph.NumberOfVertices);

            try
            {
                for (int i = 0; i < vertices; i++)
                {
                    Status[i] = (int)Enum.VertexStatus.UNSEEN;
                }

                Status[source] = (int)Enum.VertexStatus.INTREE;
                // Console.Write("Path : " +source + " ");
                Dad[source]       = 0;
                Bandwidth[source] = 0;

                List <Edge> adjacentEdges = graph.Adjacent[source];
                foreach (Edge edge in adjacentEdges)
                {
                    int v = edge.GetAdjacentVertex(source);
                    Status[v]    = (int)Enum.VertexStatus.FRINGE;
                    Dad[v]       = source;
                    Bandwidth[v] = edge.Weight;
                    maxHeap.Insert(v, Bandwidth[v]);
                }

                int count = 0;

                while (!maxHeap.IsEmpty()) // while there are fringes
                {
                    count++;
                    int maximumIndex = -1;
                    maximumIndex         = maxHeap.ExtractMaximum();
                    Status[maximumIndex] = (int)Enum.VertexStatus.INTREE;

                    // Console.Write(maximumIndex + " ");


                    List <Edge> verticesTowardsMax = graph.Adjacent[maximumIndex];
                    foreach (Edge edge in verticesTowardsMax)
                    {
                        int v = edge.GetAdjacentVertex(maximumIndex);
                        if (Status[v] == (int)Enum.VertexStatus.UNSEEN)
                        {
                            Dad[v]       = maximumIndex;
                            Status[v]    = (int)Enum.VertexStatus.FRINGE;
                            Bandwidth[v] = Math.Min(Bandwidth[maximumIndex], edge.Weight);
                            maxHeap.Insert(v, Bandwidth[v]);
                        }
                        else if (Status[v] == (int)Enum.VertexStatus.FRINGE && Bandwidth[v] < Math.Min(Bandwidth[maximumIndex], edge.Weight))
                        {
                            Dad[v]       = maximumIndex;
                            Bandwidth[v] = Math.Min(Bandwidth[maximumIndex], edge.Weight);
                            maxHeap.Modify(v, Bandwidth[v]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }

            watch.Stop();
            elapsed = watch.Elapsed.TotalSeconds;
            Console.WriteLine("Dijkstra : Time to calculate maximum bandwidth using Heap = " + elapsed);
            return(Bandwidth[destination]);
        }