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.
The right structure would be a fibonacci heap, but there is none in the std library.
Does anyone have suggestions for a better, c++ implemented structure?
You can use std::set
and store a pair<distance, vertex> in it. 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. The closest unvisited vertex is always in the first element of the set(and can obtained using set.begin()
).
For most practical purposes, the std::priority_queue
based implementation is good enough for sparse graphs. Implementing Dijkstra this way has a runtime of 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. As the graph gets denser, the asymptotics of the Fib-heap version move closer to the vanilla implementation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.