# Can I run Dijkstra's algorithm using priority queue?

I think I can run Dijkstra's algorithm using any data structure. I do not see any implementation details of Dijkstra's algorithm.

Is a priority queue a possible data structure? Will running Dijkstra's algorithm using a priority queue reduce or increase the complexity? Will I overshoot the problem?

• What would the priority queue keep track of? What are its contents? Sep 24 '16 at 11:02
• Any data structure? What happens if you use a stack or a queue?
– Raphael
Sep 24 '16 at 13:30
• "I do not see any implementation details of dijkstra's algorithm" -- check other sources, then.
– Raphael
Sep 24 '16 at 13:30
• I have found this document very useful for working with the A* algorithm (which is very closely related to Dijkstra's algorithm). It contains a very good discussion on the tradeoffs involved in using different data structures to represent sets, and AFAICS a large proportion of its discussion should apply to Dijkstra's algorithm as much as it does to A*. Sep 24 '16 at 15:23

Yes, you can use priority queues to improve the complexity of the algorithm from $O(V^2)$ to $O(|E| + |V| \log|V|)$ where $E$ is the number of edges and $V$ is the number of nodes.

You should consider carefully the number of nodes in your graph and the desired run time before adding complexity to your implementation.

See here for a brief explanation of performance vs difficulty trade offs.

To summarize the blog, using a regular queue can still speed up Dijkstra's algorithm by up to a factor of 4 in most cases, with rarely occurring graphs running in $O(V^3)$. The link says "never" occurring, but that would depend on the actual problem you are solving.

See this for the original research on using min-priority queues to speed up Dijkstra's algorithm, it is the fastest known implementation for Dijkstra's algorithm.

• Welcome to Computer Science! We expect references to fulfill the minimal scholarly requirements and be as robust over time as possible. Please take some time to improve your post in this regard. We have collected some advice here.
– Raphael
Sep 24 '16 at 14:52
• Also, you can improve your answer by giving a summary of that blog post. The link may go dead at any point in time, which would make your answer that much less useful.
– Raphael
Sep 24 '16 at 14:53