简体   繁体   中英

Implementation of decrease key function for a min heap priority queue

I'm trying to run Dijkstra's algorithm and I need to implement a decreaseKey function. I'm having trouble doing it. I read one solution here but it uses a lot of memory by storing a hash map in the heap. Is there a way to implement decreaseKey without a hash map and still maintain O(log n) time?

So far my decreaseKey function takes two parameters: vertex and newDistance. When I call decreaseKey(vertex * v, int newDistance) , the algorithm has to find the index where vertex 'v' is stored and then change its distance. I can't figure out how to 'find' the vertex to get its index and keep it in O(log n) time.

You don't need a hash map to store the information; you only need a mapping from vertex to priority-queue position. If your vertices are identified by consecutive integers, then the mapping consists of an array of |V| integers (where |V| is the number of vertices).

While that is not a trivial amount of space, it is only one word per graph vertex, which is considerably less than the space occupied by edge lists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM