Example #1
0
        public void BMinHeap_Test()
        {
            var rnd     = new Random();
            var initial = Enumerable.Range(0, 51).OrderBy(x => rnd.Next()).ToList();

            //insert test
            var tree = new BMinHeap <int>(initial);

            for (int i = 51; i <= 99; i++)
            {
                tree.Insert(i);
            }

            for (int i = 0; i <= 99; i++)
            {
                var min = tree.ExtractMin();
                Assert.AreEqual(min, i);
            }


            var testSeries = Enumerable.Range(1, 49).OrderBy(x => rnd.Next()).ToList();

            foreach (var item in testSeries)
            {
                tree.Insert(item);
            }

            for (int i = 1; i <= 49; i++)
            {
                var min = tree.ExtractMin();
                Assert.AreEqual(min, i);
            }
        }
        /// <summary>
        /// Returns a dictionary of chosen encoding bytes for each distinct T
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Dictionary <T, byte[]> Compress(T[] input)
        {
            var frequencies = computeFrequency(input);

            var minHeap = new BMinHeap <FrequencyWrap>();

            foreach (var frequency in frequencies)
            {
                minHeap.Insert(new FrequencyWrap(
                                   frequency.Key, frequency.Value));
            }

            while (minHeap.Count > 1)
            {
                var a = minHeap.ExtractMin();
                var b = minHeap.ExtractMin();

                var newNode = new FrequencyWrap(
                    default(T), a.Frequency + b.Frequency);

                newNode.Left  = a;
                newNode.Right = b;

                minHeap.Insert(newNode);
            }

            var root = minHeap.ExtractMin();

            var result = new Dictionary <T, byte[]>();

            DFS(root, new List <byte>(), result);

            return(result);
        }
Example #3
0
        /// <summary>
        /// Add a new item to stream
        /// </summary>
        /// <param name="newValue"></param>
        public void Add(int newValue)
        {
            var median = GetMedian();

            //our goal is to keep the balance factor atmost to be <=1
            if (leftHeap.Count == rightHeap.Count)
            {
                if (newValue < median)
                {
                    leftHeap.Insert(newValue);
                }
                else
                {
                    rightHeap.Insert(newValue);
                }

                return;
            }
            //left has more elements
            else if (leftHeap.Count > rightHeap.Count)
            {
                //we can't increase left count > 1
                //So borrow top to right first
                if (newValue < median)
                {
                    rightHeap.Insert(leftHeap.ExtractMax());
                    leftHeap.Insert(newValue);
                }
                else
                {
                    rightHeap.Insert(newValue);
                }

            }
            //right has more elements
            else if(rightHeap.Count > leftHeap.Count)
            {
                //we can't increase right count > 1
                //So borrow top to left first
                if (newValue > median)
                {
                    leftHeap.Insert(rightHeap.ExtractMin());
                    rightHeap.Insert(newValue);
                }
                else
                {
                    leftHeap.Insert(newValue);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Sort the given input
        /// where elements are misplaced almost by a distance k
        /// </summary>
        /// <param name="input"></param>
        /// <param name="k"></param>
        /// <returns></returns>
        public static int[] Sort(int[] input, int k)
        {
            if (k > input.Length)
            {
                throw new Exception("K cannot exceed array length.");
            }

            var firstKElements = new int[k];

            Array.Copy(input, firstKElements, k);

            var minHeap = new BMinHeap <int>(firstKElements);

            var result = new List <int>();

            for (int i = k; i < input.Length; i++)
            {
                result.Add(minHeap.ExtractMin());
                minHeap.Insert(input[i]);
            }

            while (minHeap.Count > 0)
            {
                result.Add(minHeap.ExtractMin());
            }

            return(result.ToArray());
        }
Example #5
0
        /// <summary>
        /// Do DFS to pick smallest weight neighbour edges
        /// of current spanning tree one by one
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="currentVertex"></param>
        /// <param name="spanTreeVertices"></param>
        /// <param name="spanTreeNeighbours"> Use Fibornacci Min Heap to pick smallest edge neighbour </param>
        /// <param name="spanTreeEdges">result MST edges</param>
        private void DFS(WeightedGraph <T, W> graph,
                         WeightedGraphVertex <T, W> currentVertex,
                         BMinHeap <MSTEdge <T, W> > spanTreeNeighbours,
                         HashSet <T> spanTreeVertices,
                         List <MSTEdge <T, W> > spanTreeEdges)
        {
            //add all edges to Fibornacci Heap
            //So that we can pick the min edge in next step
            foreach (var edge in currentVertex.Edges)
            {
                spanTreeNeighbours.Insert(new MSTEdge <T, W>(
                                              currentVertex.Value,
                                              edge.Key.Value, edge.Value));
            }

            //pick min edge
            var minNeighbourEdge = spanTreeNeighbours.ExtractMin();

            //skip edges already in MST
            while (spanTreeVertices.Contains(minNeighbourEdge.Source) &&
                   spanTreeVertices.Contains(minNeighbourEdge.Destination))
            {
                minNeighbourEdge = spanTreeNeighbours.ExtractMin();

                //if no more neighbours to explore
                //time to end exploring
                if (spanTreeNeighbours.Count == 0)
                {
                    return;
                }
            }

            //keep track of visited vertices
            //do not duplicate vertex
            if (!spanTreeVertices.Contains(minNeighbourEdge.Source))
            {
                spanTreeVertices.Add(minNeighbourEdge.Source);
            }

            //Destination vertex will never be a duplicate
            //since this is an unexplored Vertex
            spanTreeVertices.Add(minNeighbourEdge.Destination);

            //add edge to result
            spanTreeEdges.Add(minNeighbourEdge);

            //now explore the destination vertex
            DFS(graph, graph.Vertices[minNeighbourEdge.Destination],
                spanTreeNeighbours, spanTreeVertices, spanTreeEdges);
        }
        private void enqueueIntersectionEvent(Event currentEvent, Point intersection)
        {
            if (intersection == null)
            {
                return;
            }

            var intersectionEvent = new Event(intersection, pointComparer, EventType.Intersection, null, this);

            if (intersectionEvent.X > SweepLine.Left.X ||
                (intersectionEvent.X == SweepLine.Left.X &&
                 intersectionEvent.Y > currentEvent.Y))
            {
                if (!eventQueueLookUp.Contains(intersectionEvent))
                {
                    eventQueue.Insert(intersectionEvent);
                    eventQueueLookUp.Add(intersectionEvent);
                }
            }
        }
Example #7
0
        //O(nlog(n))
        public static T[] Sort(T[] array)
        {
            //heapify
            var heap = new BMinHeap <T>();

            foreach (var item in array)
            {
                heap.Insert(item);
            }

            //now extract min until empty and return them as sorted array
            var sortedArray = new T[array.Length];
            var j           = 0;

            while (heap.Count > 0)
            {
                sortedArray[j] = heap.ExtractMin();
                j++;
            }

            return(sortedArray);
        }
Example #8
0
        //O(nlog(n))
        public static T[] Sort(T[] array)
        {
            //heapify
            var heap = new BMinHeap <T>();

            for (int i = 0; i < array.Length; i++)
            {
                heap.Insert(array[i]);
            }

            //now extract min until empty and return them as sorted array
            var sortedArray = new T[array.Length];
            int j           = 0;

            while (heap.Count > 0)
            {
                sortedArray[j] = heap.ExtractMin();
                j++;
            }

            return(sortedArray);
        }
Example #9
0
 public void Enqueue(T queueItem)
 {
     minHeap.Insert(queueItem);
 }