Ejemplo n.º 1
0
        /// <summary>
        /// Returns a dictionary of chosen encoding bytes for each distinct T.
        /// </summary>
        public Dictionary <T, byte[]> Compress(T[] input)
        {
            var frequencies = computeFrequency(input);

            var minHeap = new BHeap <FrequencyWrap>();

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

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

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

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

                minHeap.Insert(newNode);
            }

            var root = minHeap.Extract();

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

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

            return(result);
        }
Ejemplo n.º 2
0
        public void Max_BHeap_Test()
        {
            var rnd = new Random();

            var initial = new List <int>(Enumerable.Range(0, 51)
                                         .OrderBy(x => rnd.Next()));


            var maxHeap = new BHeap <int>(true, initial);

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

            for (int i = 0; i <= 99; i++)
            {
                var max = maxHeap.Extract();
                Assert.AreEqual(max, 99 - i);
            }

            //IEnumerable tests.
            Assert.AreEqual(maxHeap.Count, maxHeap.Count());

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

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

            for (int i = 1; i <= 49; i++)
            {
                var max = maxHeap.Extract();
                Assert.AreEqual(max, 49 - i + 1);
            }

            //IEnumerable tests.
            Assert.AreEqual(maxHeap.Count, maxHeap.Count());
        }
Ejemplo n.º 3
0
        public void Min_BHeap_Test()
        {
            var rnd     = new Random();
            var initial = Enumerable.Range(0, 51).OrderBy(x => rnd.Next()).ToList();

            var minHeap = new BHeap <int>(false, initial);

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

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

            //IEnumerable tests.
            Assert.AreEqual(minHeap.Count, minHeap.Count());

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

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

            for (int i = 1; i <= 49; i++)
            {
                var min = minHeap.Extract();
                Assert.AreEqual(min, i);
            }

            //IEnumerable tests.
            Assert.AreEqual(minHeap.Count, minHeap.Count());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Do DFS to pick smallest weight neighbour edges
        /// of current spanning tree one by one.
        /// </summary>
        /// <param name="spanTreeNeighbours"> Use Fibonacci 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,
                         BHeap <MSTEdge <T, W> > spanTreeNeighbours, HashSet <T> spanTreeVertices,
                         List <MSTEdge <T, W> > spanTreeEdges)
        {
            while (true)
            {
                //add all edges to Fibonacci 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.Extract();

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

                    //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
                var graph1 = graph;
                currentVertex = graph1.Vertices[minNeighbourEdge.Destination];
            }
        }
Ejemplo n.º 5
0
        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);
                }
            }
        }