Пример #1
0
 private void Run(IGraph g, int s)
 {
     //set the weight to source vertex s as 0
     _distTo[s] = 0.0;
     //insert the vertex into the priority queue as (vertex number, weight)
     _pq.Insert(s, _distTo[s]);
     while (!_pq.IsEmpty())
     {
         //remove a vertex from the top of the queue
         var v = _pq.DeleteMin();
         Scan(g, v);
     }
 }
Пример #2
0
        public void Run(int vertex)
        {
            _distanceTo[vertex] = 0.0;
            _priorityQueue.Insert(vertex, _distanceTo[vertex]);

            while (!_priorityQueue.IsEmpty())
            {
                var v = _priorityQueue.DeleteMin();
                foreach (var neighbour in _graph.Neighbours(v))
                {
                    Relax(neighbour);
                }
            }
        }
Пример #3
0
        public void MinPriorityQueueTest()
        {
            var pq = new MinPriorityQueue <string>(9);

            var results = new List <string>();

            pq.Insert("P");
            pq.Insert("Q");
            pq.Insert("E");

            results.Add(pq.DeleteMin());

            pq.Insert("X");
            pq.Insert("A");
            pq.Insert("M");

            results.Add(pq.DeleteMin());

            pq.Insert("P");
            pq.Insert("L");
            pq.Insert("E");

            results.Add(pq.DeleteMin());
        }
        public void AddNum(int num)
        {
            if (maxHeap.Count == 0 || num < maxHeap.Max)
            {
                maxHeap.Add(num);
            }
            else
            {
                minHeap.Add(num);
            }

            if (maxHeap.Count < minHeap.Count)
            {
                maxHeap.Add(minHeap.DeleteMin());
            }
            else if (maxHeap.Count > minHeap.Count + 1)
            {
                minHeap.Add(maxHeap.DeleteMax());
            }
        }