【Bell-Ford 算法】CLRS Exercise 24.1-4,24.1-6

本文是一篇笔记,大部分内容取自 CLRS 第三版,第 24.1 节。

Exercise 24.1-4

Modify the Bellman-Ford algorithm so that it sets $v.d$ to $-infty$ for all vertices $v$ for which there is a negative-weight cycle on some path from the source to $v$.

Theorem 24.4 (Correctness of the Bellman-Ford algorithm)

Let Bellman-Ford be run on a weighted, directed graph $G = (V, E)$ with source $s$ and weight function $w : E o mathbb{R}.$ If $G$ contains no negative-weight cycles that are reachable from $s$, then the algorithm returns true, we have $v.d = delta(s, v)$ for all vertices $v in V$, and the predecessor subgraph $G_{pi}$ is a shortest-paths tree rooted at $s$. If $G$ does contain a negative-weight cycle reachable from $s$, then the algorithm returns false.

Proof 只证明后半部分,即「If $G$ does contain a negative-weight cycle reachable from $s$, then the algorithm returns false.」

Suppose that graph $G$ contains a negative-weight cycle that is reachable from the source $s$; let this cycle be $c = < v_0, v_1, dots, v_k >$, where $v_0 = v_k$. Then,
egin{equation}
sum_{i=1}^{k} w(v_{i - 1}, v_i) < 0. label{E:1}
end{equation}
Assume for the purpose of contradiction that the Bellman-Ford algorithm returns true. Thus, $v_i.d le v_{i - 1}.d + w(v_{i - 1}, v_i)$ for $i = 1, 2, dots, k$. Summing the inequalities around cycle $c$ gives us
egin{aligned}
sum_{i = 1}^{k} v_i.d &le sum_{i = 1}^{k} (v_{i - 1}.d + w(v_{i - 1}, v_i)) \
&= sum_{i = 1}^{k} v_{i - 1}.d + sum_{i = 1}^{k} w(v_{i - 1}, v_i).
end{aligned}
Since $v_0 = v_k$, each vertex in $c$ appears exactly once in each of the summations $sum_{i = 1}^{k} v_i.d$ and $sum_{i =1}^k v_{i-1}.d$, and so
egin{equation*}
sum_{i = 1}^{k} v_i.d = sum_{i = 1}^{k} v_{i - 1}.d
end{equation*}
Moreover, the fact that cycle $c$ is reachable from $s$ implies that $v_i.d$ is finite for $i = 1, 2, dots, k$. Thus,
$$ 0 le sum_{i = 1}^{k} w(v_{i - 1}, v_i), $$
which contradicts inequality eqref{E:1}. We conclude that the Bellman-Ford algorithm returns true if graph $G$ contains no negative-weight cycles reachable from the source, and false otherwise.

从上述证明过程可以看出,任意「negative-weighted cycle reachable from the source」中至少有一条边 $(u, v)$ 在伪代码的第 6 行使得 $v.d > u.d + w(u,v)$ 成立。换言之,每个「negative-weighted cycle reachable from the source」的所有边之中,至少有一条边会在伪代码的第 6 行被探测到。

exercise 24.1-4 的答案:
对于伪代码第 6 行发现的每条边 $(u, v)$,从 $v$ 或 $u$ 开始 DFS 或 BFS,把经过的点的最短路长度都置为 $-infty$。

Exercise 24.1-6

Suppose that a weighted, directed graph $G = (V, E)$ has a negative-weight cycle. Give an efficient algorithm to list the vertices of one such cycle. Prove that your algorithm is correct.

若边 $(u, v)$ 在第 $|V|$ 轮迭代仍能被松弛,则从 $s$ 到 $v$ 的“最短路”至少有 $|V|$ 条边,也就是说从 $s$ 到 $v$ 的“最短路”一定经过 negative-weight cycle。
从 $v$ 开始沿着 predecessor 向上走,第一个重复经过的节点 $t$ 一定在 negative-weight cycle 中。再从 $t$ 开始沿着 predecessor 走一遍直到再次经过 $t$,即可找出这个环。

References

http://courses.csail.mit.edu/6.046/fall01/handouts/ps9sol.pdf

原文地址:https://www.cnblogs.com/Patt/p/11645509.html