Algorithm Search⌘K

Proofs: Bellman-Ford algorithm

These are the human proofs of Bellman-Ford algorithm, at full length.

Bellman-Ford returns the distances or a negative cycle full

This proof shows: correctness, termination.

Claim. Let G=(V,E)G = (V, E) be a directed graph with nn vertices, real weights ww, and a source ss. BELLMAN-FORD(G,w,s)(G, w, s) ends. If no cycle of negative weight is reachable from ss, it returns nil for the cycle, d[v]=δ(s,v)d[v] = \delta(s, v) for each vv, and d[v]=d[π[v]]+w(π[v],v)d[v] = d[\pi[v]] + w(\pi[v], v) for each v≠sv \ne s with d[v]<∞d[v] < \infty. If such a cycle is reachable, it returns a cycle of GG of negative weight.

Termination. Each loop has a fixed bound: n−1n - 1 passes of mm edges, then at most mm edges, nn steps of line L11 and at most nn steps of line L12 (justified in Step 4). So the algorithm ends.

Notation. δk(v)\delta_k(v) is the least weight of a walk from ss to vv with at most kk edges (∞\infty if there is none). dk[v]d_k[v] is the value of d[v]d[v] at the end of pass kk, with d0d_0 the values after line L2.

Step 1: every finite estimate is the weight of a walk, so d[v]≥δ(s,v)d[v] \ge \delta(s, v). This is the upper-bound property: at the start only d[s]=0d[s] = 0 is finite, and line L7 sets d[v]=d[u]+w(u,v)d[v] = d[u] + w(u, v) with d[u]d[u] finite, the weight of a walk to uu plus one edge. The same holds for the relaxation in line L11.

Step 2: dk[v]≤δk(v)d_k[v] \le \delta_k(v) for 0≤k≤n−10 \le k \le n - 1 (as long as the passes run). By induction on kk. For k=0k = 0, δ0(s)=0=d0[s]\delta_0(s) = 0 = d_0[s] and δ0(v)=∞\delta_0(v) = \infty for v≠sv \ne s. For k≥1k \ge 1, take a walk WW to vv with at most kk edges and weight δk(v)<∞\delta_k(v) < \infty (if δk(v)=∞\delta_k(v) = \infty there is nothing to show). If WW has fewer than kk edges, dk[v]≤dk−1[v]≤δk−1(v)=w(W)d_k[v] \le d_{k-1}[v] \le \delta_{k-1}(v) = w(W) by the hypothesis, because estimates never increase. Else WW is a walk W′W' to some uu with k−1k - 1 edges, then the edge (u,v)(u, v). By the hypothesis, dk−1[u]≤w(W′)d_{k-1}[u] \le w(W'), so at the moment pass kk relaxes (u,v)(u, v), d[u]≤w(W′)d[u] \le w(W') is finite. After line L6 and L7, d[v]≤d[u]+w(u,v)≤w(W)d[v] \le d[u] + w(u, v) \le w(W). Later steps of the pass only lower d[v]d[v]. So dk[v]≤w(W)=δk(v)d_k[v] \le w(W) = \delta_k(v). If line L8 stops the loop early after pass kk, then no edge was tense in pass kk, so the values do not change in any later pass, and the bound d[v]≤δj(v)d[v] \le \delta_j(v) holds for all jj; Step 3 uses this.

Step 3: no reachable negative cycle. Then for each reachable vv, a shortest walk can be chosen without a repeated vertex: removing a cycle from a walk does not increase its weight, because each cycle has weight at least 00. So δ(s,v)=δn−1(v)\delta(s, v) = \delta_{n-1}(v). By Steps 1 and 2, d[v]=δ(s,v)d[v] = \delta(s, v) at the end of the passes, also when line L8 stops early. Now each edge (u,v)(u, v) with d[u]<∞d[u] < \infty has d[v]=δ(s,v)≤δ(s,u)+w(u,v)=d[u]+w(u,v)d[v] = \delta(s, v) \le \delta(s, u) + w(u, v) = d[u] + w(u, v), so the check of line L10 fails for every edge, and line L13 returns nil. When line L8 returns, the same values are returned. For v≠sv \ne s with d[v]<∞d[v] < \infty: when line L7 last set d[v]d[v], it set π[v]=u\pi[v] = u with d[v]=d[u]+w(u,v)d[v] = d[u] + w(u, v) at that time; afterwards d[u]d[u] can only decrease, so d[u]+w(u,v)≤d[v]d[u] + w(u, v) \le d[v], and if it were strictly less, the edge (u,v)(u, v) would be tense at the end, which we excluded. So d[v]=d[π[v]]+w(π[v],v)d[v] = d[\pi[v]] + w(\pi[v], v). Following π\pi from vv then gives a walk whose weight telescopes; since d[s]=0d[s] = 0 and all values are finite, the walk ends at ss (a predecessor cycle would have weight 00 by telescoping, but Step 5 shows that every predecessor cycle has negative weight, so there is none). Thus the predecessor edges form a shortest-path tree.

Step 4: a reachable negative cycle is always found. Let C=⟨c0,c1,…,ck=c0⟩C = \langle c_0, c_1, \ldots, c_k = c_0 \rangle be a negative cycle reachable from ss. Then pass n−1n - 1 cannot have stopped early by line L8: that would mean no edge is tense, and the page on relaxation proves that some edge is tense after any finite sequence of relaxations when a negative cycle is reachable. For the same reason the check of lines L9 and L10 finds a tense edge (u,v)(u, v). Line L11 relaxes it, so now d[v]<dn−1[v]≤δn−1(v)d[v] < d_{n-1}[v] \le \delta_{n-1}(v) by Step 2.

We show that following π\pi from vv never reaches nil. Throughout the run, each vertex xx with π[x]≠\pi[x] \ne nil satisfies

d[π[x]]+w(π[x],x)≤d[x],(A)d[\pi[x]] + w(\pi[x], x) \le d[x], \tag{A}

because the assignment of π[x]\pi[x] makes it an equality, later only d[π[x]]d[\pi[x]] can fall while π[x]\pi[x] stays, and a change of d[x]d[x] also sets π[x]\pi[x] again. Also $\pi[s] = $ nil implies d[s]=0d[s] = 0, because d[s]d[s] changes only together with π[s]\pi[s]. Now assume the chain v=x0,x1=π[x0],…v = x_0, x_1 = \pi[x_0], \ldots reaches nil without repeating a vertex, at xrx_r with $\pi[x_r] = $ nil. All xix_i have finite estimates, so xr=sx_r = s (every other vertex with a finite estimate has a predecessor), and d[s]=0d[s] = 0. Adding (A) along the chain gives d[v]≥d[s]+w(P)=w(P)d[v] \ge d[s] + w(P) = w(P), where PP is the path s=xr→⋯→x0=vs = x_r \to \cdots \to x_0 = v with r≤n−1r \le n - 1 edges. So d[v]≥δn−1(v)d[v] \ge \delta_{n-1}(v), a contradiction. Hence the chain repeats a vertex. A chain with at most nn distinct vertices enters its cycle after at most n−1n - 1 steps, so after the nn steps of line L11 the vertex xx lies on a cycle of predecessor edges, and line L12 follows π\pi from xx back to xx in at most nn steps.

Step 5: every cycle of predecessor edges has negative weight. Consider the moment a cycle D=⟨y0,y1,…,yk=y0⟩D = \langle y_0, y_1, \ldots, y_k = y_0 \rangle of predecessor edges (π[yi]=yi−1\pi[y_i] = y_{i-1}) first appears. It is created by a relaxation that sets π[yj]=yj−1\pi[y_j] = y_{j-1} for one jj. Just before it, the edge was tense: d[yj−1]+w(yj−1,yj)<d[yj]d[y_{j-1}] + w(y_{j-1}, y_j) < d[y_j]. For the other edges of DD, (A) gives d[yi−1]+w(yi−1,yi)≤d[yi]d[y_{i-1}] + w(y_{i-1}, y_i) \le d[y_i]. All these values are finite. Adding the kk inequalities, the sum of the dd values is the same on both sides, so w(D)<0w(D) < 0. The weight of DD does not depend on dd, so DD stays negative while it exists. With Step 4, the cycle that line L12 returns has negative weight. ■\blacksquare

Back to Bellman-Ford algorithm