过去一周我一直在研究迪杰斯特拉算法,现在我已经用Java编写了正确运行的代码。它使用数组计算标准findMin函数,该函数会返回距离最近的顶点,显然这个方法是O(n)。现在我想使用优先队列(最小堆)来实现它。
我的思路是:
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;
}
}
}
如果一个特定的顶点存在于堆中,则可以考虑将其距离更新为找到的最小节点的距离。
现在我的问题是如何在O(log n)时间内更新堆中的特定元素。
我们不能在O(1)时间内找到该元素,对吧?
在我这样的天真实现中,它可能需要 O(n) 时间,
那么有人能建议如何处理这种瓶颈吗?如何在O(log n)时间内更新堆中的特定顶点?(类似地,如何在O(1)时间内找到特定元素)