Ejemplo n.º 1
0
        public void Should_Insert_MaxHeap()
        {
            //arrange
            var result = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            //act
            result.Insert(100);
            result.Insert(19);
            result.Insert(36);
            result.Insert(17);
            result.Insert(3);
            result.Insert(25);
            result.Insert(1);
            result.Insert(2);
            result.Insert(7);

            //assert
            result.Count.ShouldBeEquivalentTo(9);
            result.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);

            result.Root.Data.ShouldBeEquivalentTo(100);
            result.Root.Left.Data.ShouldBeEquivalentTo(19);
            result.Root.Right.Data.ShouldBeEquivalentTo(36);
            result.Root.Left.Left.Data.ShouldBeEquivalentTo(17);
            result.Root.Left.Right.Data.ShouldBeEquivalentTo(3);
            result.Root.Right.Left.Data.ShouldBeEquivalentTo(25);
            result.Root.Right.Right.Data.ShouldBeEquivalentTo(1);
            result.Root.Left.Left.Left.Data.ShouldBeEquivalentTo(2);
            result.Root.Left.Left.Right.Data.ShouldBeEquivalentTo(7);
        }
Ejemplo n.º 2
0
        public void Should_Check_ExtractTop_MaxHeap()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(100);
            heap.Insert(19);
            heap.Insert(36);
            heap.Insert(17);
            heap.Insert(3);
            heap.Insert(25);
            heap.Insert(1);
            heap.Insert(2);
            heap.Insert(7);

            //act
            var result = heap.ExtractTop();

            //assert
            result.ShouldBeEquivalentTo(100);
            heap.Count.ShouldBeEquivalentTo(8);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);

            heap.Root.Data.ShouldBeEquivalentTo(36);
            heap.Root.Left.Data.ShouldBeEquivalentTo(19);
            heap.Root.Right.Data.ShouldBeEquivalentTo(25);
            heap.Root.Left.Left.Data.ShouldBeEquivalentTo(17);
            heap.Root.Left.Right.Data.ShouldBeEquivalentTo(3);
            heap.Root.Right.Left.Data.ShouldBeEquivalentTo(7);
            heap.Root.Right.Right.Data.ShouldBeEquivalentTo(1);
            heap.Root.Left.Left.Left.Data.ShouldBeEquivalentTo(2);
            heap.Root.Left.Left.Right.ShouldBeEquivalentTo(null);
        }
Ejemplo n.º 3
0
        public void Should_Check_ExtractTop_MinHeap()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            heap.Insert(1);
            heap.Insert(50);
            heap.Insert(23);
            heap.Insert(88);
            heap.Insert(90);
            heap.Insert(32);
            heap.Insert(74);
            heap.Insert(96);

            //act
            var result = heap.ExtractTop();

            //assert
            result.ShouldBeEquivalentTo(1);
            heap.Count.ShouldBeEquivalentTo(7);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);

            heap.Root.Data.ShouldBeEquivalentTo(23);
            heap.Root.Left.Data.ShouldBeEquivalentTo(50);
            heap.Root.Right.Data.ShouldBeEquivalentTo(32);
            heap.Root.Left.Left.Data.ShouldBeEquivalentTo(88);
            heap.Root.Left.Right.Data.ShouldBeEquivalentTo(90);
            heap.Root.Right.Left.Data.ShouldBeEquivalentTo(96);
            heap.Root.Right.Right.Data.ShouldBeEquivalentTo(74);
        }
Ejemplo n.º 4
0
        public void Should_Insert_MinHeap()
        {
            //arrange
            var result = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            //act
            result.Insert(4);
            result.Insert(50);
            result.Insert(7);
            result.Insert(55);
            result.Insert(90);
            result.Insert(87);
            result.Insert(2);

            //assert
            result.Count.ShouldBeEquivalentTo(7);
            result.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);

            result.Root.Data.ShouldBeEquivalentTo(2);
            result.Root.Left.Data.ShouldBeEquivalentTo(50);
            result.Root.Right.Data.ShouldBeEquivalentTo(4);
            result.Root.Left.Left.Data.ShouldBeEquivalentTo(55);
            result.Root.Left.Right.Data.ShouldBeEquivalentTo(90);
            result.Root.Right.Left.Data.ShouldBeEquivalentTo(87);
            result.Root.Right.Right.Data.ShouldBeEquivalentTo(7);
        }
Ejemplo n.º 5
0
            private void Insert(int node)
            {
                var      box = _tree.GetAabb(node);
                Vector3D tmp;

                Vector3D.Clamp(ref _vec, ref box.Min, ref box.Max, out tmp);
                var dist = Vector3D.DistanceSquared(tmp, _vec);

                _tmp.Insert(node, dist);
            }
Ejemplo n.º 6
0
        private Dictionary <T, T?> ComputePaths(T source, MyGraphAdj <T> graph)
        {
            var vertexes = graph.GetAllVertexes().ToList();
            var edges    = graph.GetAllEdges().ToList();

            Dictionary <T, int> distances        = new Dictionary <T, int>();
            Dictionary <T, T?>  parent           = new Dictionary <T, T?>();
            Dictionary <T, int> heapMinDistances = new Dictionary <T, int>();

            var heap = new MyBinaryHeap <MyBinaryHeapKeyNode <T, int> >(MyBinaryHeapType.MinHeap);

            foreach (var vertex in vertexes)
            {
                heap.Insert(new MyBinaryHeapKeyNode <T, int>(vertex.Key, vertex.Key.CompareTo(source) == 0 ? 0 : Int32.MaxValue));
                heapMinDistances.Add(vertex.Key, Int32.MaxValue);
            }
            parent[source]    = null;
            distances[source] = 0;

            while (heap.Count > 0)
            {
                var current = heap.ExtractTop();
                var nodes   = edges.Where(x => x[0].Equals(
                                              vertexes.First(y => y.Key.CompareTo(current.Key) == 0).Value)).ToList();
                if (nodes.Count == 0)
                {
                    break;
                }
                var parentNode = vertexes.First(x => x.Key.CompareTo(current.Key) == 0);
                foreach (var node in nodes)
                {
                    var vertex = vertexes.First(x => x.Value == node[1]);
                    if (!heap.Contains(new MyBinaryHeapKeyNode <T, int>(vertex.Key, vertex.Value)))
                    {
                        continue;
                    }

                    var currentDistance = edges.First(x =>
                                                      x[0] == parentNode.Value && x[1] == vertex.Value)[2];
                    distances[vertex.Key] = distances[current.Key] + currentDistance;

                    if (heapMinDistances[vertex.Key] >= distances[vertex.Key])
                    {
                        parent[vertex.Key] = current.Key;

                        heap.Decrease(new MyBinaryHeapKeyNode <T, int>(vertex.Key, Int32.MaxValue),
                                      new MyBinaryHeapKeyNode <T, int>(vertex.Key, currentDistance));
                        heapMinDistances[vertex.Key] = currentDistance;
                    }
                }
            }

            return(parent);
        }
Ejemplo n.º 7
0
        public void Should_Decrease_Min_Heap()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            heap.Insert(6);
            heap.Insert(7);
            heap.Insert(12);
            heap.Insert(10);
            heap.Insert(15);
            heap.Insert(17);

            //act
            heap.Decrease(10, 2);

            //assert
            heap.Root.Data.ShouldBeEquivalentTo(2);
            heap.Root.Left.Data.ShouldBeEquivalentTo(6);
            heap.Root.Right.Data.ShouldBeEquivalentTo(12);
            heap.Root.Left.Left.Data.ShouldBeEquivalentTo(7);
            heap.Root.Left.Right.Data.ShouldBeEquivalentTo(15);
            heap.Root.Right.Left.Data.ShouldBeEquivalentTo(17);

            heap.Count.ShouldBeEquivalentTo(6);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);
        }
Ejemplo n.º 8
0
        public void Should_Check_ExtractTop_MinHeap_Multiple()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            heap.Insert(1);
            heap.Insert(3);
            heap.Insert(6);
            heap.Insert(5);
            heap.Insert(9);
            heap.Insert(8);

            //act

            //assert
            heap.ExtractTop().ShouldBeEquivalentTo(1);
            heap.ExtractTop().ShouldBeEquivalentTo(3);
            heap.ExtractTop().ShouldBeEquivalentTo(5);
            heap.ExtractTop().ShouldBeEquivalentTo(6);
            heap.ExtractTop().ShouldBeEquivalentTo(8);
            heap.ExtractTop().ShouldBeEquivalentTo(9);

            heap.Root.ShouldBeEquivalentTo(null);
            heap.Count.ShouldBeEquivalentTo(0);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);
        }
        public void InsertMyBinaryHeapTest()
        {
            //Assign
            var val = new List <int>()
            {
                9, 6, 5, 2, 3
            };
            var expected = new List <int>()
            {
                0, 1, 3, 2, 6, 9, 5
            };
            var myBinaryHeap = new MyBinaryHeap <int>();

            //Act
            myBinaryHeap.BuildHeapList(val);
            var result = myBinaryHeap.Insert(1);

            //Assert
            CollectionAssert.AreEqual(expected, result, "Result differs with what is expected");
        }
Ejemplo n.º 10
0
        public IEnumerable <T> HeapSort(IEnumerable <T> list)
        {
            if (list == null)
            {
                throw new ArgumentNullException();
            }
            var array = list.ToArray();

            var heap = new MyBinaryHeap <T>(MyBinaryHeapType.MinHeap);

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

            while (heap.Count > 0)
            {
                yield return(heap.ExtractTop());
            }
        }
Ejemplo n.º 11
0
        public void Should_Throw_Decrease_If_Lower_Value_MaxHeap()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            //act
            Action act = () => heap.Decrease(17, 11);

            //assert
            act.ShouldThrow <ArgumentException>();

            heap.Count.ShouldBeEquivalentTo(6);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }
Ejemplo n.º 12
0
        public void Should_Check_Contains_True()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            //act
            var result = heap.Contains(10);

            //assert
            result.ShouldBeEquivalentTo(true);

            heap.Count.ShouldBeEquivalentTo(6);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }
Ejemplo n.º 13
0
        public void Should_Throw_Decrease_If_Not_Contains()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            heap.ExtractTop();

            //act
            Action act = () => heap.Decrease(17, 11);

            //assert
            act.ShouldThrow <InvalidOperationException>();

            heap.Count.ShouldBeEquivalentTo(5);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }
Ejemplo n.º 14
0
        public void Should_Check_ExtractTop_Insert_MaxHeap_Multiple()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            heap.ExtractTop();
            heap.ExtractTop();
            heap.ExtractTop();

            //act
            heap.Insert(1);
            heap.Insert(2);
            heap.Insert(3);
            heap.Insert(11);
            heap.Insert(6);

            //assert
            heap.Root.Data.ShouldBeEquivalentTo(11);
            heap.Root.Left.Data.ShouldBeEquivalentTo(7);
            heap.Root.Right.Data.ShouldBeEquivalentTo(10);
            heap.Root.Left.Left.Data.ShouldBeEquivalentTo(6);
            heap.Root.Left.Right.Data.ShouldBeEquivalentTo(2);
            heap.Root.Right.Left.Data.ShouldBeEquivalentTo(3);
            heap.Root.Right.Right.Data.ShouldBeEquivalentTo(6);
            heap.Root.Left.Left.Left.Data.ShouldBeEquivalentTo(1);

            heap.Count.ShouldBeEquivalentTo(8);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }
Ejemplo n.º 15
0
        internal void EnqueueCorrectionExpiration(MyGridPhysics.PredictiveDestructionData data)
        {
            var time = Now + MyGridPhysics.PredictiveDestructionData.ExpirationTime;

            m_predictiveDestructionUpdate.Insert(data, time);
        }
Ejemplo n.º 16
0
        public MyPath <V> FindPath(V start, V end, Predicate <V> vertexTraversable = null, Predicate <IMyPathEdge <V> > edgeTraversable = null)
        {
            // CH: TODO: Make multiple private copies of this method and call the right one
            // according to what were the arguments to the public interface method

            CalculateNextTimestamp();

            MyPathfindingData startData = start.PathfindingData;

            Visit(startData);
            startData.Predecessor = null;
            startData.PathLength  = 0.0f;

            IMyPathVertex <V> retVal             = null;
            float             shortestPathLength = float.PositiveInfinity;

            m_openVertices.Insert(start.PathfindingData, start.EstimateDistanceTo(end));
            while (m_openVertices.Count > 0)
            {
                MyPathfindingData currentData = m_openVertices.RemoveMin();
                V     current           = currentData.Parent as V;
                float currentPathLength = currentData.PathLength;

                if (retVal != null && currentPathLength >= shortestPathLength)
                {
                    break;
                }

                for (int i = 0; i < current.GetNeighborCount(); ++i)
                {
                    /*IMyPathVertex<V> neighbor = current.GetNeighbor(i);
                     * if (neighbor == null) continue;*/

                    IMyPathEdge <V> edge = current.GetEdge(i);
                    if (edge == null || (edgeTraversable != null && !edgeTraversable(edge)))
                    {
                        continue;
                    }

                    V neighbor = edge.GetOtherVertex(current);
                    if (neighbor == null || (vertexTraversable != null && !vertexTraversable(neighbor)))
                    {
                        continue;
                    }

                    float             newPathLength = currentData.PathLength + edge.GetWeight();
                    MyPathfindingData neighborData  = neighbor.PathfindingData;

                    if (neighbor == end && newPathLength < shortestPathLength)
                    {
                        retVal             = neighbor;
                        shortestPathLength = newPathLength;
                    }

                    if (Visited(neighborData))
                    {
                        if (newPathLength < neighborData.PathLength)
                        {
                            neighborData.PathLength  = newPathLength;
                            neighborData.Predecessor = currentData;
                            m_openVertices.ModifyUp(neighborData, newPathLength + neighbor.EstimateDistanceTo(end));
                        }
                    }
                    else
                    {
                        Visit(neighborData);
                        neighborData.PathLength  = newPathLength;
                        neighborData.Predecessor = currentData;
                        m_openVertices.Insert(neighborData, newPathLength + neighbor.EstimateDistanceTo(end));
                    }
                }
            }

            m_openVertices.Clear();

            if (retVal == null)
            {
                return(null);
            }
            else
            {
                return(ReturnPath(retVal.PathfindingData, null, 0));
            }
        }