【发布时间】:2012-08-03 19:47:44
【问题描述】:
过去一周我一直在研究 dijkstra 的算法,我在 java 中有正确的运行代码。它使用数组来计算标准 findMin 函数,该函数为您提供距离最小的顶点。显然它是 O(n),现在我希望使用 Priority Queue(Min Heaps) 来实现它
我的思考过程是什么:
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;
}
}
}
但是如果堆中存在一个特定的顶点,那么它的距离可能会根据找到的 Min 节点的距离进行更新。
现在我的问题是如何在 O(log n ) 时间内更新堆中的特定元素。
我们不能在 O(1) 时间内找到那个元素,对吧?
在像我这样的幼稚实现中,它将是 O(n),
那么任何人都可以建议可以做些什么来处理这个瓶颈吗?我们如何在 O(log n) 时间内更新堆中的特定顶点? (类似地,我们如何在 O(1) 时间内找到特定元素)
【问题讨论】:
标签: algorithm data-structures priority-queue dijkstra