【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 \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的更多相关文章
- Bellman—Ford算法思想
---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...
- Bellman - Ford 算法解决最短路径问题
Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...
- 证明:一个整数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 ...
- JAVA 基础编程练习题24 【程序 24 根据输入求输出】
24 [程序 24 根据输入求输出] 题目:给一个不多于 5 位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. package cskaoyan; public class cskaoya ...
- Dijkstra算法与Bellman - Ford算法示例(源自网上大牛的博客)【图论】
题意:题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 poj2387 Description Bessie is out in the field and ...
- 【算法设计与分析基础】24、kruskal算法详解
首先我们获取这个图 根据这个图我们可以得到对应的二维矩阵图数据 根据kruskal算法的思想,首先提取所有的边,然后把所有的边进行排序 思路就是把这些边按照从小到大的顺序组装,至于如何组装 这里用到并 ...
- 低水平选手的自我救赎 (1)CLRS Exercise 16.5-2
题目大意 给定正整数 $n$ 和一个由 $m$ 个正整数构成的可重集合 $A$,满足 $\forall a\in A, a\le n$ 且 $m\le n$ . 定义 $N_t(A) = |\{a\i ...
- 利用神经网络进行网络流量识别——特征提取的方法是(1)直接原始报文提取前24字节,24个报文组成596像素图像CNN识别;或者直接去掉header后payload的前1024字节(2)传输报文的大小分布特征;也有加入时序结合LSTM后的CNN综合模型
国外的文献汇总: <Network Traffic Classification via Neural Networks>使用的是全连接网络,传统机器学习特征工程的技术.top10特征如下 ...
- [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 ...
随机推荐
- Java当中的IO流-时间api(下)-上
Java当中的IO流(下)-上 日期和时间 日期类:java.util.Date 系统时间: long time = System.currentTimeMillis(); public class ...
- jmeter下载安装
jmeter运行依靠java环境 一.根据jmeter版本不同要求java环境则不同 jmeter官网下载地址:http://jmeter.apache.org/download_jmeter.cgi ...
- Pytorch:使用GPU训练
1.模型转为cuda gpus = [0] #使用哪几个GPU进行训练,这里选择0号GPU cuda_gpu = torch.cuda.is_available() #判断GPU是否存在可用 net ...
- Python最高效爬虫框架
Overview Scrapy is a fast high-level screen scraping and web crawling framework, used to crawl websi ...
- 打开下载CA root 证书的链接失败
下载CA root 证书 http://adip/certsrv server error or refuse to connect 这是由于AD上没有安装Active Directory Certi ...
- linux开启数据库远程连接
1.阿里云开启数据库端口 3306 2. 修改 Mysql-Server 用户配置 mysql -uroot -p(注意此用户必须要有最高级权限才行 默认root用户) mysql> USE m ...
- Laravel find in set排序
做项目遇到个需求,需要对结果集中的数据进行指定规则的顺序排列.例如,用户状态有四种: 0=>未激活:1=>正常:2=>禁用:3=>软删除 现在的需求是,我要按照:正常-> ...
- HashMap三两事
前言 JDK8中对HashMap做了优化,依然是用数组存储数据,但是扩容时采用双链表的方式避免了高并发情况下导致出现循环链表的问题,另外也引入了红黑树,提高碰撞元素的搜索速度. 一段代码 下面这段代码 ...
- jquery 复合事件 toggle()方法的使用
定义和用法 toggle() 方法用于绑定两个或多个事件处理器函数,以响应被选元素的轮流的 click 事件. 语法: $(selector).toggle(function1(),function2 ...
- nginx的root 指令
好长时间都没搞清nginx的root路径: location /img/ { alias /var/www/image/; } #若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动 ...