Why does Dijkstra’s algorithm use decrease-key?

The reason for using decrease-key rather than reinserting nodes is to keep the number of nodes in the priority queue small, thus keeping the total number of priority queue dequeues small and the cost of each priority queue balance low. In an implementation of Dijkstra’s algorithm that reinserts nodes into the priority queue with their … Read more

Find cycle of shortest length in a directed graph with positive weights

You can easily modify Floyd-Warshall algorithm. (If you’re not familiar with graph theory at all, I suggest checking it out, e.g. getting a copy of Introduction to Algorithms). Traditionally, you start path[i][i] = 0 for each i. But you can instead start from path[i][i] = INFINITY. It won’t affect algorithm itself, as those zeroes weren’t … Read more

Dijkstra Shortest Path with VertexList = ListS in boost graph

BGL actually has an example of using dijkstra_shortest_paths with listS/listS, but it’s not linked to from the HTML documentation: http://www.boost.org/doc/libs/release/libs/graph/example/dijkstra-example-listS.cpp What the error message is trying to tell you (error: no match for ‘operator=’ in ‘index.boost::adj_list_vertex_property_map…ValueType = boost::detail::error_property_not_found…) is that there is no per-vertex storage for the vertex_index_t property, which is what adj_list_vertex_property_map needs. To … Read more

Dijkstra’s algorithm with negative weights

As long as the graph does not contain a negative cycle (a directed cycle whose edge weights have a negative sum), it will have a shortest path between any two points, but Dijkstra’s algorithm is not designed to find them. The best-known algorithm for finding single-source shortest paths in a directed graph with negative edge … Read more

Why use Dijkstra’s Algorithm if Breadth First Search (BFS) can do the same thing faster?

Dijkstra allows assigning distances other than 1 for each step. For example, in routing the distances (or weights) could be assigned by speed, cost, preference, etc. The algorithm then gives you the shortest path from your source to every node in the traversed graph. Meanwhile BFS basically just expands the search by one “step” (link, … Read more

Find the shortest path in a graph which visits certain nodes

Everyone else comparing this to the Travelling Salesman Problem probably hasn’t read your question carefully. In TSP, the objective is to find the shortest cycle that visits all the vertices (a Hamiltonian cycle) — it corresponds to having every node labelled ‘mustpass’. In your case, given that you have only about a dozen labelled ‘mustpass’, … Read more