简体   繁体   English

最小堆优先级队列的减少密钥功能的实现

[英]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. 我正在尝试运行Dijkstra的算法,并且需要实现一个decreaseKey函数。 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? 是否有实施方式decreaseKey没有一个哈希表,并仍然保持O(log n)的时间?

So far my decreaseKey function takes two parameters: vertex and newDistance. 到目前为止,我decreaseKey函数有两个参数:顶点和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. 当我调用decreaseKey(vertex * v, int newDistance) ,算法必须找到存储顶点“ v”的索引,然后更改其距离。 I can't figure out how to 'find' the vertex to get its index and keep it in O(log n) time. 我无法弄清楚如何“找到”顶点以获取其索引并将其保持在O(log n)时间。

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| 如果您的顶点由连续的整数标识,则映射由| V |的数组组成 integers (where |V| is the number of vertices). 整数(其中| V |是顶点数)。

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. 虽然这不是一个小数目的空间,但每个图形顶点只有一个单词,大大少于边列表所占据的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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