Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Update a key in Priority Queue in O(log n ) time in dijkstra's algorithm?

I have been working on dijkstra's algorithm for the past one week one I have the correct running code for it in java. It is using array for the computation of standard findMin function which gives you the vertex with smallest distance.Obviously it is O(n) and Now I am looking to implement it using Priority Queue(Min Heaps)

What My thought process is:

while (there are unseen Vertex)
{

    vertex= get TheVertex WithSmallest Distance Yet;//(In can be done in O(log n) using heap)

  for this vertex {

    find all of the adjacent edges and traverse them.

    for a particular vertex which is not there in heap yet{

        Simply add it in the queue;
    }
  }
}

But if a particular vertex exists in the heap then its distance might be updated taking into account the distance of the found Min node.

Now My question is How will be update a particular element in The Heap in O(log n )time.

We cant find that element in O(1) time right?

in my naive implementation like mine it would be O(n),

So can any one suggest what can be done to handle this bottleneck? How can we update a particular vertex in the Heap in O(log n) time? (similarly how can we find a particular element in O(1) time )

like image 974
Dude Avatar asked Aug 03 '12 19:08

Dude


People also ask

How do I change priority queue values?

Update priority and value To update priority in Priority Queue, get the index of the element that you want to update the priority of and assign a new key to the element. After updating the priority of an element, we need to heapify the heap to maintain the heap data structure.

What is the time complexity of Dijkstra algorithm using priority queue?

Time Complexity of Dijkstra's Algorithm is O ( V 2 ) but with min-priority queue it drops down to O ( V + E l o g V ) .

How do I use priority queue in Dijkstra?

1) Initialize distances of all vertices as infinite. 2) Create an empty priority_queue pq. Every item of pq is a pair (weight, vertex). Weight (or distance) is used as first item of pair as first item is by default used to compare two pairs 3) Insert source vertex into pq and make its distance as 0.

What does the running time of Dijkstra's algorithm depend on?

The running time of Dijkstra's algorithm depends on how these operations are implemented. We can use an unsorted array for the min-priority queue. Each insert and decreaseKey operation takes Θ(1) time. Each extractMin operation takes time O(q), where q is the number of vertices in the min-priority queue at the time.


1 Answers

I'm aware of two basic approaches for this situation:

  1. Whenever you're visiting the neighbors of a vertex, insert them in the heap no matter if they are in the heap or not. Then, when you get the vertex with smallest distance from the heap, check if it has already been removed from the heap before. If it has, then remove this one too and continue. Otherwise, mark it as removed and visit all the neighbors.

  2. Keep an explicit pointer to where in the heap each element is. Then you can perform the operation known as "decrease-key" on the element that you've already located.

like image 168
Ivan Vergiliev Avatar answered Sep 30 '22 11:09

Ivan Vergiliev