algorithm - Floyd-Warshall algorithm on a directed graph in which every edge's length is either -1, 0, or 1
问题描述
I'm taking the Algorithms: Design and Analysis II course, and one of the questions is as follows:
Suppose we run the Floyd-Warshall algorithm on a directed graph
G = (V,E)
in which every edge's length is either -1, 0, or 1. Suppose further thatG
is strongly connected, with at least oneu-v
path for every pairu,v
of vertices. The graphG
may or may not have a negative-cost cycle. How large can the final entriesA[i,j,n]
be, in absolute value? Choose the smallest number that is guaranteed to be a valid upper bound. (As usual,n
denotes|V|
.) [WARNING: for this question, make sure you refer to the implementation of the Floyd-Warshall algorithm given in lecture, rather than to some alternative source.]
- 2^n
- n -1
- n^2
- infinity
The lecture video that's been referred to is on YouTube. For reference, the algorithm is as follows:
for k = 1 to n
for i = 1 to n
for j = 1 to n
A[i,j,k] = min {A[i,j,k-1], A[i,k,k-1] + A[k,j,k-1]}
The correct answer is the first one, 2^n
, and the hint says it can be proved by induction. I can't seem to wrap my head around it. Can someone help me understand?
解决方案
考虑所有节点都连接到所有其他节点的图,边的长度为-1
。
可以对 k 进行归纳。让我们证明以下表达式:
A[i,j,k] = -2 ** k
对于k = 0
,A[i,j,k] = -1
(根据图的定义)。因此,基本条件检查。
现在,A[i,j,k] = min(A[i,j,k-1], A[i,k,k-1] + A[k,j,k-1])
. 使用归纳假设,右边的所有项都是-2 ** (k - 1)
。
因此,A[i,j,k] = -2 ** k
和abs(A[i,j,n]) = 2 ** n
。
推荐阅读
- python - 我有一个 MIP 模型,我正在使用 Python 和 Pulp 来解决它。在我的问题表述中,我有一个包含嵌套总和和二进制变量的约束
- latex - 删除 LaTeX 中的章节边距
- c++ - 用 C++ 编写并使用不同编译器构建的 tcp 客户端和服务器是否会导致写入/读取失败
- swiftui - SwiftUI DatePicker 月份显示难题
- sql - 如何使用 R 将数据快速批量保存到 SQL 数据库?
- visual-studio - Nuget Packages依赖问题
- json - 包含 HTML 标签的 SwiftUI 中的 JSON 解析
- javascript - 如何映射两个对象数据并从中提取细节?
- python - 如何使用 xgboost 模型在没有响应值的情况下从预测器进行预测
- php - 查询按月分组的 SQL 结果