Shortest
Minimize total weight. An unreachable vertex has distance +∞. A reachable negative cycle can make the cost unbounded below for vertices reachable from that cycle.
Algorithms · Weighted graphs
The path with the fewest edges is not always the one with the least weight. Assumptions about weights and cycles determine both the right algorithm and whether a finite answer exists.
For graph, edge and traversal basics, see Graphs: BFS and DFS.
In a directed weighted graph G = (V, E), the weight of a path is the sum of its edge weights. Given a source s, we want the best weight for each reachable vertex v and, through predecessors, a path attaining it.
Minimize total weight. An unreachable vertex has distance +∞. A reachable negative cycle can make the cost unbounded below for vertices reachable from that cycle.
Maximize total weight. In a DAG every reachable vertex has a finite optimum. If walks may repeat vertices, a usable positive cycle can make the weight unbounded above.
Keep an estimate d[v] of the shortest cost from s to v: d[s] = 0 and d[v] = +∞ for every other vertex. If a known route to u followed by edge u → v improves v, update its distance and predecessor.
Also set prev[v] = u. To recover the route to t, follow predecessors from t back to s and reverse the sequence. If d[t] = +∞, t is unreachable.
Dijkstra repeatedly chooses the unsettled vertex with the smallest estimate, then relaxes its outgoing edges. Every reachable edge must have weight ≥ 0: otherwise a later route could improve an already settled vertex.
d[s] = 0; every other d[v] = +∞; prev[v] = undefined
push (0, s) into a min-priority queue
while the queue is not empty:
pop (cost, u)
if cost ≠ d[u]: continue // stale entry
for every edge u → v of weight w:
if d[u] + w < d[v]:
d[v] = d[u] + w; prev[v] = u
push (d[v], v)
Example: s → a (4), s → b (1), b → a (2), a → t (1), b → t (7). The valid extraction order is s, b, a, t. From s we get a = 4 and b = 1; from b we improve a to 3 and find t = 8; from a we improve t to 4.
| Vertex | s | a | b | t |
|---|---|---|---|---|
| d | 0 | 3 | 1 | 4 |
| prev | — | b | s | a |
The shortest route to t is s → b → a → t with weight 1 + 2 + 1 = 4. The greedy choice is safe because appending nonnegative edges cannot produce a later cheaper route to an extracted vertex.
Bellman–Ford relaxes all edges for |V| − 1 passes. After pass k, estimates are optimal for paths using at most k edges. A simple path has at most |V| − 1 edges, so these passes suffice when no reachable negative cycle exists.
d[s] = 0; every other d[v] = +∞
repeat |V| − 1 times:
for every edge u → v of weight w:
if d[u] is finite and d[u] + w < d[v]:
d[v] = d[u] + w; prev[v] = u
if no estimate changed: stop the passes
for every edge u → v of weight w:
if d[u] is finite and d[u] + w < d[v]:
report a reachable negative cycle
Example with a negative edge: s → a (4), s → b (5), a → t (2), b → t (6), b → a (−3). Scanning edges in this order, the first pass gives a = 2 and t = 6; the second improves t to 4. The result is s → b → a → t, weight 5 − 3 + 2 = 4. The version of Dijkstra that settles extracted vertices would settle a at cost 4 too early.
| Pass | s | a | b | t |
|---|---|---|---|---|
| 0 | 0 | ∞ | ∞ | ∞ |
| 1 | 0 | 2 | 5 | 6 |
| 2 | 0 | 2 | 5 | 4 |
| 3 | 0 | 2 | 5 | 4 |
For example, s → a (1), a → b (−2), b → a (1), b → t (2): a → b → a weighs −1. Repeating it before reaching t makes the cost to a, b and t unbounded below.
In a DAG (directed acyclic graph), every path is simple. Compute a topological order, set L[s] = 0 and L[v] = −∞ for all other vertices, then process vertices in that order. For each edge u → v set L[v] = max(L[v], L[u] + w(u,v)) if L[u] is finite. Save the predecessor whenever the estimate improves.
This also works with negative weights: acyclicity is the essential condition. In the same DAG, replacing max with min and −∞ with +∞ computes shortest paths in O(|V| + |E|).
| Order | s | a | b | t |
|---|---|---|---|---|
| L | 0 | 3 | 7 | 12 |
| prev | — | s | a | b |
The longest route is s → a → b → t, weight 3 + 4 + 5 = 12. On general graphs, negating weights and applying Bellman–Ford solves the walk variant only if no positive cycle is reachable; it does not solve the longest simple path problem, which is NP-hard in general.
| Goal and assumptions | Method | Time |
|---|---|---|
| Shortest, all edge weights are 1 | BFS | O(|V| + |E|) |
| Shortest, DAG even with negative weights | Topological order + min | O(|V| + |E|) |
| Shortest, nonnegative weights | Dijkstra | O((|V| + |E|) log |V|) |
| Shortest, negative weights possible | Bellman–Ford | O(|V| + |V|·|E|) |
| Longest, DAG | Topological order + max | O(|V| + |E|) |
Bounds assume adjacency lists and simple graphs; Dijkstra uses a binary priority queue. Times include the vertices even for disconnected graphs. Bellman–Ford can stop early when a pass changes nothing, but its worst case remains O(|V| + |V|·|E|).
A minimum spanning tree connects every vertex at the lowest total network cost; it does not guarantee shortest routes from s. See minimum spanning tree.
s → b → a → t, weight 4. The two-edge route s → a → t weighs 5.
No. The final check requires finite d[u]; distances from s remain valid in the reachable portion.
Shortest: s → t, weight 1. Longest: s → a → t, weight −2 + 4 = 2. Topological order s, a, t supports both calculations.
No. The cycle must lie on a walk that can eventually reach t. It may make other vertices unbounded, but not t.
Need distances between every pair of vertices? Continue with Johnson and Floyd–Warshall.