繁体   English   中英

在elogv时间内实现dijkstra算法

[英]implementing dijkstra algorithm in elogv time

1) Create a Min Heap of size V where V is the number of vertices in the given graph. 
   Every node of min heap contains vertex number and distance value of the vertex.
2) Initialize Min Heap with source vertex as root (the distance value assigned to source
   vertex is 0). The distance value assigned to all other vertices is INF (infinite).
3) While Min Heap is not empty, do following
    a) Extract the vertex with minimum distance value node from Min Heap. Let the 
    extracted vertex be u.
    b) For every adjacent vertex v of u, check if v is in Min Heap. If v is in Min Heap
       and distance value is more than weight of u-v plus distance value of u, then 
       update the distance value of v.

我当时正在考虑在C ++中为Dijkstra实现此伪代码。 这是为了快速实施。 我在这里有些困惑,即我们正在使用堆来跟踪未开发区域的相邻顶点的Dijkstra分数。 在3b中,我们需要检查u的每个相邻顶点v,检查v是否为minHeap,minHeap是否在恒定时间内支持这种操作,堆是用于搜索的最差的数据结构,应该花费线性时间来搜索是否存在是否在最小堆中,此外,我们还必须更新相邻的顶点,所以不仅我们应该知道它是否在minHeap中,而且还要知道它的位置以便我们可以更新它,我们希望所有这些都在logv时间发生,否则没有一点快速实施的意义。 应该使用什么数据结构代替Heap?

PS:我说的是图形作为邻接表实现的实现。

在最坏的情况下,Dijkstra的算法必须执行|E| DecreaseKey操作和|V| ExtractMin操作。

  1. 在二进制堆数据结构中,DecreaseKey和ExtractMin操作的成本均为O(log(n))。 因此,算法的总成本为O(|E|log(|V|) + |V|log(|V|))

  2. 如果我们使用斐波那契堆而不是简单的二进制堆,则可以将总成本降低为O(|E| + |V|log(|V|))因为现在DecreaseKey操作的摊销成本为O(1)

此外,对于图形中的每个顶点,还必须维护一个指向堆中相应元素的指针数组。 这样就可以在O(1)标识堆中的顶点

暂无
暂无

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

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