Shortest Path — Minimum Total Weight Between Two Vertices

Words: 236

Single-source shortest path

Finding the shortest path from one vertex to every other vertex.

Shortest path with BFS

Competitive Programming
Chapter 6, the graph part — zzyafyj’s column

Dijkstra

Algorithms on the toilet, part 7: Dijkstra’s shortest path

We find dis[3] = 12, while dis[2] + e[2][3] = 1 + 9 = 10. Since dis[3] > dis[2] + e[2][3], dis[3] must be updated to 10. This process has a name: “relaxation”. The distance from vertex 1 to vertex 3 — dis[3] — is successfully relaxed through the edge 2->3. That is the main idea of Dijkstra: relax the distances from vertex 1 to all the other vertices through edges.
Each time, take the vertex closest to the source (vertex 1 in the example), expand from it as the new centre, and you eventually get the shortest paths from the source to all remaining vertices.

All-pairs shortest paths

Finding the shortest path between every pair of vertices.

Floyd–Warshall

Algorithms on the toilet: Floyd’s shortest path in five lines

At first only vertex 1 may be used as an intermediate point, then only vertices 1 and 2, … and finally all vertices 1..n. Put in one sentence: the shortest path from vertex i to vertex j using only the first k vertices as intermediates.

Reference:
Programming Contest Challenge Book
Algorithms on the toilet: Floyd’s shortest path in five lines
Algorithms on the toilet, part 7: Dijkstra’s shortest path

Loading...