简体   繁体   English

c ++ std :: priority_queue是dijkstra队列的正确结构吗

[英]Is a c++ std::priority_queue the right structure for the dijkstra queue

I don't think a c++ priority queue is the right structure for the dijkstra queue, because it contains no functionality for an easy lookup or deletion of elements. 我认为c ++优先级队列不是dijkstra队列的正确结构,因为它不包含用于轻松查找或删除元素的功能。

The right structure would be a fibonacci heap, but there is none in the std library. 正确的结构将是斐波那契堆,但std库中没有。

Does anyone have suggestions for a better, c++ implemented structure? 有没有人提出更好的C ++实现结构的建议?

You can use std::set and store a pair<distance, vertex> in it. 您可以使用std::set并在其中存储pair <distance,vertex>。 To lookup and delete elements, you can keep distance to each vertex in an array or in std::vector to obtain a pair<distance, vertex> for a given vertex quickly. 要查找和删除元素,可以保持到数组或std::vector每个顶点的距离,以快速获取给定顶点的pair <distance,vertex>。 The closest unvisited vertex is always in the first element of the set(and can obtained using set.begin() ). 最接近的未访问顶点始终位于set的第一个元素中(可以使用set.begin()获得)。

For most practical purposes, the std::priority_queue based implementation is good enough for sparse graphs. 对于大多数实际目的,基于std::priority_queue的实现足以用于稀疏图。 Implementing Dijkstra this way has a runtime of O(E log V) . 以这种方式实现Dijkstra的运行时为O(E log V) If you have a dense enough graph, you could simply use the basic O(V*V) version of Dijkstra's algorithm. 如果您有足够密集的图,则可以简单地使用Dijkstra算法的基本O(V*V)版本。 As the graph gets denser, the asymptotics of the Fib-heap version move closer to the vanilla implementation. 随着图变得更密集,Fib-heap版本的渐近性逐渐接近于香草实现。

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

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