The shortest route from one place to another depends on what an edge means. If every edge counts as one step, BFS finds fewest-edge paths. If edges have costs, travel times, or other weights, we need a method that adds those weights. A route is shortest when no other route from the same start has a smaller total weight.
Background for this page. A weighted graph assigns a cost to each edge. A path’s cost is the sum of its edge weights. Infinity represents an unreachable or not-yet-reached vertex in the distance table. Related explanations: Graph traversal: BFS, DFS, and topological order · Binary heaps, priority queues, and heapsort.
Most shortest-path methods use relaxation. We keep a current distance estimate for each vertex. For an edge from u to v with weight w, if distance[u] + w improves distance[v], we replace the estimate and remember u as the predecessor of v. Repeating the right relaxations turns estimates into shortest distances.
# A route through u may improve the route to v.
if distance[u] + weight(u, v) < distance[v]:
distance[v] = distance[u] + weight(u, v)
predecessor[v] = u
Dijkstra’s algorithm repeatedly settles the unsettled vertex with the smallest current distance, then relaxes its outgoing edges. It is correct when edge weights are nonnegative. A negative edge can invalidate the idea that a settled distance will never improve. With a binary heap and adjacency lists, a common bound is O((V + E) log V).
If the graph is a DAG, topological order gives another route. Process vertices in that order and relax each outgoing edge. No edge can later lead back to an earlier vertex, so one pass is enough. This takes Θ(V + E) time and can handle negative edge weights because the graph has no cycle.
Bellman–Ford handles negative edges in a general directed graph by repeatedly relaxing all edges. After at most V − 1 full passes, it has shortest distances if no reachable negative-weight cycle exists. A further improving relaxation reveals such a cycle. Its time is O(VE). If a reachable negative cycle can be repeated, there is no finite shortest walk to vertices reachable from it; we can keep making the total weight smaller.
These methods answer one-source questions. If we need routes between every pair, we can repeat a one-source method or use all-pairs shortest-path algorithms. First state the graph’s assumptions: directed or undirected, weighted or unweighted, whether negative weights exist, and whether unreachable vertices are possible. An undirected negative edge already allows a negative closed walk if retracing edges is permitted.
Follow Dijkstra’s settled distances
Use directed edges A→B of weight 4, A→C of weight 9, and B→C of weight 2. Initialize A to zero and the others to infinity. Settle A and relax its edges: B becomes four and C becomes nine. Settle B next because four is the smallest unsettled estimate. Its edge improves C to six. Settle C last.
Nonnegative weights justify settling B: any route through an unsettled vertex cannot later supply a cheaper route to it. A negative edge can destroy that argument. Initialize predecessors separately and leave unreachable vertices without a predecessor. To recover a route, walk predecessors backward from the destination and reverse the resulting sequence.
Practice. Which method is a sound first choice for each case: an unweighted social graph, a weighted road graph with nonnegative travel times, or a directed graph with negative costs but no negative cycle?
Answer. Use BFS for fewest edges, Dijkstra for nonnegative travel times, and Bellman–Ford for the general directed graph with negative edges. If the last graph is a DAG, topological relaxation is faster.
Try another case. Add a vertex D with no incoming route from A. Should its distance become zero when the queue empties?
Reasoning. No. Zero would claim a free route. Its distance remains infinity, and route reconstruction should report it unreachable.