本文是一篇笔记,大部分内容取自 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 \to \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,
\begin{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
\begin{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
\begin{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

【Bell-Ford 算法】CLRS Exercise 24.1-4,24.1-6的更多相关文章

  1. Bellman—Ford算法思想

    ---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...

  2. Bellman - Ford 算法解决最短路径问题

    Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...

  3. 证明:一个整数a若不能被6整除,则a2+24必能被24整除。(整除理论,1.1.4)

    证明:一个整数a若不能被6整除,则a2+24必能被24整除. 证明: 因为,a不能被6整除 所以,a不可以同时被2和3整除 所以,a一定是一个奇数, 所以,令a=2k+1,k是整数: 又因为,a2+2 ...

  4. JAVA 基础编程练习题24 【程序 24 根据输入求输出】

    24 [程序 24 根据输入求输出] 题目:给一个不多于 5 位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. package cskaoyan; public class cskaoya ...

  5. Dijkstra算法与Bellman - Ford算法示例(源自网上大牛的博客)【图论】

    题意:题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 poj2387 Description Bessie is out in the field and ...

  6. 【算法设计与分析基础】24、kruskal算法详解

    首先我们获取这个图 根据这个图我们可以得到对应的二维矩阵图数据 根据kruskal算法的思想,首先提取所有的边,然后把所有的边进行排序 思路就是把这些边按照从小到大的顺序组装,至于如何组装 这里用到并 ...

  7. 低水平选手的自我救赎 (1)CLRS Exercise 16.5-2

    题目大意 给定正整数 $n$ 和一个由 $m$ 个正整数构成的可重集合 $A$,满足 $\forall a\in A, a\le n$ 且 $m\le n$ . 定义 $N_t(A) = |\{a\i ...

  8. 利用神经网络进行网络流量识别——特征提取的方法是(1)直接原始报文提取前24字节,24个报文组成596像素图像CNN识别;或者直接去掉header后payload的前1024字节(2)传输报文的大小分布特征;也有加入时序结合LSTM后的CNN综合模型

    国外的文献汇总: <Network Traffic Classification via Neural Networks>使用的是全连接网络,传统机器学习特征工程的技术.top10特征如下 ...

  9. [Swift]LeetCode679. 24点游戏 | 24 Game

    You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...

随机推荐

  1. hdu 5726 GCD GCD+线段树+区间预处理+map

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  2. hdu 5532 Almost Sorted Array nlogn 的最长非严格单调子序列

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  3. flask框架(九): 请求和响应扩展以及中间件

    一:请求响应扩展 # 每一次访问都执行 # 注意请求之前按照顺序执行 # 请求之后按照书写顺序倒序执行 # 请求之前执行 @app.before_request def process_request ...

  4. 2019牛客暑期多校训练营(第八场)A 单调栈

    题意 给一个\(n*m\)的01矩阵,找有多少个全1子矩阵不被其他全1子矩阵包括. 分析 用单调栈找到的全1子矩阵是不能向上扩展和向右扩展的,只需判断该子矩阵能否向左和向下扩展,若四个方向都不能扩展, ...

  5. curl: (35) Server aborted the SSL handshake 解决办法

    # 先删除curl brew uninstall curl # 重新安装curl,带上--with-openssl brew install curl --with-openssl # 然后重启下ph ...

  6. Lock和synchronized的区别

    总结来说,Lock和synchronized有以下几点不同: 1)Lock是一个接口,而synchronized是Java中的关键字,synchronized是内置的语言实现: 2)synchroni ...

  7. JavaWeb-SpringBoot(抖音)_一、抖音项目制作

    JavaWeb-SpringBoot(抖音)_一.抖音项目制作 传送门 JavaWeb-SpringBoot(抖音)_二.服务器间通讯 传送门 JavaWeb-SpringBoot(抖音)_三.抖音项 ...

  8. PHP7 的部分新特性

    1. 运算符(NULL 合并运算符) $a = $_GET['a'] ?? 1; 它相当于: <php$a = isset($_GET['a']) ? $_GET['a'] : 1; 我们知道三 ...

  9. Nginx-rtmp点播之complex handshake

    1. 点播的配置 假设配置文件 nginx.conf 中对 rtmp 配置如下: # 创建的子进程数 worker_processes 1; #error_log logs/error.log; er ...

  10. C#获取实体类属性名和值

    遍历获得一个实体类的所有属性名,以及该类的所有属性的值 //先定义一个类: public class User { public string name { get; set; } public st ...