Algorithm Search⌘K

Proofs: Longest path in a directed acyclic graph

These are the human proofs of Longest path in a directed acyclic graph, at full length.

DAG-LONGEST-PATH computes the heaviest path from the source full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) be a directed multigraph with real weights ww and a source ss. For v∈Vv \in V let λ(v)\lambda(v) be the largest weight of a directed path from ss to vv, and λ(v)=−∞\lambda(v) = -\infty if there is none.

Termination and cycles. Kahn's algorithm stops, and it returns an order of all nn vertices if and only if GG is acyclic (see Kahn's algorithm). If GG has a cycle, line L1 stops the run. From now on GG is acyclic, and T=(t1,…,tn)T = (t_1, \ldots, t_n) is a topological order: each arc (u,v)(u, v) has uu before vv. The loops of lines L4 and L6 are finite, so the run stops.

λ\lambda is finite or −∞-\infty. In an acyclic graph a directed walk never repeats a vertex, since the part between two visits of the same vertex would be a cycle. So there are finitely many paths from ss, and the maximum exists when the set is not empty.

Lemma 1 (the recurrence). λ(s)=0\lambda(s) = 0, and for v≠sv \ne s, λ(v)=max⁡(u,v)∈E(λ(u)+w(u,v))\lambda(v) = \max_{(u, v) \in E} (\lambda(u) + w(u, v)), with the convention that the maximum over an empty set is −∞-\infty and −∞+w=−∞-\infty + w = -\infty.

Proof. The only path from ss to ss is the path of one vertex, since any other would be a cycle, so λ(s)=0\lambda(s) = 0. For v≠sv \ne s, a path PP from ss to vv has a last arc (u,v)(u, v), and PP without this arc is a path from ss to uu of weight at most λ(u)\lambda(u). So w(P)≤λ(u)+w(u,v)w(P) \le \lambda(u) + w(u, v), and λ(v)\lambda(v) is at most the right-hand side. Conversely, for an arc (u,v)(u, v) with λ(u)>−∞\lambda(u) > -\infty, take a path QQ from ss to uu of weight λ(u)\lambda(u). The vertex vv is not on QQ: if it were, the part of QQ from vv to uu with the arc (u,v)(u, v) would be a cycle. So QQ followed by (u,v)(u, v) is a path from ss to vv of weight λ(u)+w(u,v)\lambda(u) + w(u, v), and λ(v)\lambda(v) is at least the right-hand side. □\square

Lemma 2 (values are always weights of paths). At each moment, if L[v]>−∞L[v] > -\infty, then following π\pi from vv gives a path from ss to vv of weight L[v]L[v]. In particular, L[v]≤λ(v)L[v] \le \lambda(v).

Proof. Initially only L[s]=0L[s] = 0 is finite, and the path of one vertex ss has weight 0. Line L8 sets L[v]=L[u]+w(u,v)L[v] = L[u] + w(u, v) and π[v]=u\pi[v] = u while uu is the vertex being processed. By the induction hypothesis there is a path from ss to uu of weight L[u]L[u], and uu is before vv in TT. After uu is processed, L[u]L[u] and π[u]\pi[u] never change, because line L8 only changes vertices that are heads of arcs out of the vertex being processed, and those are later in TT than it, while uu is already passed. So the parent chain of uu stays a path of weight L[u]L[u], and adding the arc (u,v)(u, v) gives a walk of weight L[v]L[v], which is a path because GG is acyclic. □\square

Lemma 3 (invariant of line L4). When the loop of line L4 reaches tkt_k, L[tk]=λ(tk)L[t_k] = \lambda(t_k).

Proof. By induction on kk. Assume it for t1,…,tk−1t_1, \ldots, t_{k-1}, and let v=tkv = t_k. Every arc (u,v)(u, v) has u=tiu = t_i with i<ki < k. When uu was processed, L[u]=λ(u)L[u] = \lambda(u) by the induction hypothesis. If λ(u)>−∞\lambda(u) > -\infty, line L5 let uu pass, and line L7 at the arc (u,v)(u, v) made L[v]≥λ(u)+w(u,v)L[v] \ge \lambda(u) + w(u, v); values only go up afterwards. If λ(u)=−∞\lambda(u) = -\infty, this arc does not count in Lemma 1. So, for v≠sv \ne s, L[v]≥max⁡(u,v)(λ(u)+w(u,v))=λ(v)L[v] \ge \max_{(u, v)} (\lambda(u) + w(u, v)) = \lambda(v) by Lemma 1, and for v=sv = s, L[s]≥0=λ(s)L[s] \ge 0 = \lambda(s) by line L3. With Lemma 2, L[v]≤λ(v)L[v] \le \lambda(v), so L[v]=λ(v)L[v] = \lambda(v). □\square

Theorem. If GG has a cycle, DAG-LONGEST-PATH reports it. Otherwise it stops with L[v]=λ(v)L[v] = \lambda(v) for every vertex vv, and for each vv with L[v]>−∞L[v] > -\infty the parent pointers give a path from ss to vv of the largest weight.

Proof. The first part is the statement on line L1. A vertex keeps its value after its turn in the loop, as the proof of Lemma 2 shows, so the final L[v]L[v] is the value of Lemma 3, which is λ(v)\lambda(v). Lemma 2 then gives the path. ■\blacksquare

Back to Longest path in a directed acyclic graph