public void Test_Add_MinIndex()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(10);

            for (int i = 1; i < 11; i++)
            {
                pq.Add(i, i);
            }

            Assert.Equal(1, pq.MinIndex());
        }
        public void Test_ItemAt()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(10);

            for (int i = 1; i < 11; i++)
            {
                pq.Add(i, i);
            }

            Assert.Equal(2, pq.ItemAt(2));
        }
        public void Test_Contains()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(5);

            for (int i = 1; i < 6; i++)
            {
                pq.Add(i, i);
            }

            Assert.True(pq.Contains(4));
        }
        public void Test_DecreaseItem()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(5);

            for (int i = 1; i < 6; i++)
            {
                pq.Add(i, i);
            }

            pq.DecreaseItem(1, -1);

            Assert.Equal(-1, pq.ItemAt(1));
        }
        public void Test_Delete()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(5);

            for (int i = 1; i < 6; i++)
            {
                pq.Add(i, i);
            }

            pq.Delete(4);

            Assert.False(pq.Contains(4));
        }
        public void Test_AddDeleteMin_Order()
        {
            IndexedMinPQ <Int32> pq = new IndexedMinPQ <Int32>(10);

            for (int i = 1; i < 11; i++)
            {
                pq.Add(i, i);
            }

            for (int i = 1; i < 11; i++)
            {
                Assert.Equal(i, pq.DeleteMin());
            }
        }
Esempio n. 7
0
        //VlogV + ElogV
        public Dictionary <int, int> GetMinPath()
        {
            _pQ.Add(0, _source);
            _currMinDistances.Add(_source, 0);

            while (_pQ.CanExtractMin())
            {
                var min = _pQ.ExtractMin();//Executed V times, logV complexity
                _processed.Add(min);

                foreach (var nbor in _adj[min])//Executed E times in all
                {
                    if (_processed.Contains(nbor.Key))
                    {
                        continue;
                    }
                    var newDist = _currMinDistances[min] + nbor.Value;
                    if (!_currMinDistances.ContainsKey(nbor.Key))
                    {
                        _pQ.Add(newDist, nbor.Key);//log V complexity
                        _parents.Add(nbor.Key, min);
                        _currMinDistances.Add(nbor.Key, newDist);
                    }
                    else
                    {
                        var currMinDist = _currMinDistances[nbor.Key];
                        if (newDist < currMinDist)
                        {
                            _pQ.DecreaseKey(newDist, nbor.Key);//log V complexity
                            _parents[nbor.Key]          = min;
                            _currMinDistances[nbor.Key] = newDist;
                        }
                    }
                }
            }
            return(_parents);
        }