Esempio n. 1
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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }