本文是一篇笔记,大部分内容取自 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. 牛客训练41D最小相似度bfs

    最小相似度 题目大意:对于位数相同的两个二进制串,SIM(A,B)为它们的相似度,也就是A^B中0的个数.现在给定一堆串,找出一个T使得max{SIM(S1,T),SIM(S2,T),......,S ...

  2. 洛谷比赛 U5442 买(最长链)

    U5442 买 题目提供者bqsgwys 标签 树形结构 树的遍历 洛谷原创 题目背景 小E是个可爱的电路编码员. 题目描述 一天小E又要准备做电路了,他准备了一个电路板,上面有很多个电路元器件要安装 ...

  3. js输入密文弹出数字键盘

    我们经常被产品要求,在移动端的web页面上的输入框输入密码时要弹出数字键盘,而不是全键盘,这个该怎么实现呢? 1.首先要弹出数字键盘,我们只能把input框的type从password改为tel 2. ...

  4. vim8.1安装

    win下直接就有gvim8.1.exe安装.但linux下直接从apt-get里面下载的vim都是远古版本,需要手动编译安装. 首先,下载vim源代码 git clone https://github ...

  5. Scrapy 设置随机 User-Agent

    方式一:在每个 Spider中设置(针对单个Spider) class TencentSpider(scrapy.Spider): name = 'tencent' allowed_domains = ...

  6. 【面试】Redis

    1.如果在setnx之后执行expire之前进程意外crash或者要重启维护了,那会怎么样? set指令有非常复杂的参数,这个应该是可以同时setnx和expire合成一条指令来用的! 2.使用过Re ...

  7. Nginx-rtmp点播之业务流程分析

    1. 点播的播放流程分析 1.1 ngx_rtmp_cycle 在握手结束后,即进入该函数中做进一步处理. void ngx_rtmp_cycle(ngx_rtmp_session_t *s) { n ...

  8. PHP中Smarty的fetch()方法

    fetch 取得输出的内容 string fetch (string template [, string cache_id [, string compile_id]]) 返回一个模板输出的内容(H ...

  9. 开源运维自动化平台-opendevops

    开源运维自动化平台-opendevops 简介 官网 | Github|  在线体验 CODO是一款为用户提供企业多混合云.自动化运维.完全开源的云管理平台. CODO前端基于Vue iview开发. ...

  10. ORA-39095: Dump file space has been exhausted

    ORA-39095: Dump file space has been exhausted Table of Contents 1. 简述 2. 错误信息 3. 分析 4. 解决 5. 扩展 1 简述 ...