本文是一篇笔记,大部分内容取自 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. Mockito 2 让我们校验一些行为

    在下面的示例中,我们将会模拟(Mock)一个 List 列表. 这是因为绝大部分的人对列表这个接口比较熟悉(例如 add(), get(), clear() 方法). 在实际情况中,请不要 mock ...

  2. MySQL的安装教程

    一.MYSQL的安装 首先登入官网下载mysql的安装包,官网地址:https://dev.mysql.com/downloads/mysql/ 一般下载这个就好,现在的最新版本是5.8,但是据说已经 ...

  3. JavaWeb_(Hibernate框架)Hibernate中重要的api

    Hibernate中重要的api Configuration SessionFactory Session(重点) Transaction 在Dao层中UserDao.java使用Hibernate向 ...

  4. JavaWeb_ Servlet API(中文)

       Java Servlet 中文API Java Servlet API由两个软件包组成:一个是对应HTTP的软件包,另一个是不对应HTTP的通用的软件包.这两个软件包的同时存在使得Java Se ...

  5. hive序列化和反序列化serde

    一.简介 SerDe是Serializer/Deserializer的缩写.SerDe允许Hive读取表中的数据,并将其以任何自定义格式写回HDFS. 任何人都可以为自己的数据格式编写自己的SerDe ...

  6. ftp协议在linux上的配置

    第一步:安装 yum install -y vsftpd 第二步:启动ftp服务:vsftpd [root@localhost ~]# systemctl start vsftpd 第三步:查看启动状 ...

  7. Springboot集成MongoDB实现CRUD

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  8. Java环境变量的操作系统原理[浅谈]

    从知乎摘抄过来的文章,图没有复制过来,当作自己的笔记.其中我认为重要的部分(涉及操作系统的)已经加粗了. 本文源自:你应该知道的 Windows 环境变量 - 麓山南人的文章 - 知乎 https:/ ...

  9. laravel查询构造器DB还是ORM,这两者有什么区别,各该用在什么场景中

    解答一: 我们所有操作都是走的orm,因为操作简单 直观明了 好维护,性能是低一些 但还没有多致命,真有并发需要优化了 用DB也不一定能解决问题.还是要了解orm每个方法的意思,不然你可能一不小心就会 ...

  10. linux系统创建windows启动盘

    平时工作中用到linux的操作命令较多,因此为了方便,就给电脑装了双系统,一般工作的时候,都选择进入linux系统.但是今天有件工作之外的事情需要解决下:创建一个windows启动盘.如果按照往常来说 ...