京都大学 情報学研究科 知能情報学専攻 2023年2月実施 基礎科目 F2-2
Author
祭音Myyura (co-authored with GPT 5.6 SOL)
Description
Let be a simple directed graph with and . Every edge has a positive integer weight . The following algorithm computes shortest-path weights from :
for i = 1 to n do
D[i] = +infinity
P[i] = -1
D[1] = 0
P[1] = 0
for k = 1 to n - 1 do
for every (vi, vj) in E do
if D[j] > D[i] + d(vi, vj) then
D[j] = D[i] + d(vi, vj)
P[j] = i
- Let , , and let every edge weight be . Give after the algorithm.
- Assuming an edge weight is obtained in time, analyze the running time.
- Explain how to output a shortest path from to a specified using , or
No Pathif none exists. - Let be the minimum number of outer-loop repetitions that guarantees all shortest-path weights for every ordering of . All edge weights are . Construct for each case:
- and ;
- and .
题目描述
设 为简单有向图,,,每条边 的权 均为正整数。题给对所有边重复松弛 轮的 Bellman-Ford 主体代码,数组 保存最短距离, 保存前驱。
- 当 、 且边权全为 时,求算法结束后的 。
- 设边权可在 时间取得,分析算法时间复杂度。
- 说明如何利用 输出从 到指定 的一条最短路;不存在时输出
No Path。 - 记 为对任意边扫描顺序都能保证求出全部最短距离所需的最少外层轮数。在边权全为 时,分别构造:
- 、 的图;
- 、 的图。
Kai
Q.1
The shortest paths are the direct edges to and the path . Hence
Q.2
Initialization takes time. The algorithm then scans all edges in each of repetitions, and each relaxation takes time. Thus
for .
Q.3
If , output No Path. Otherwise, follow the predecessor indices backward and use a stack to reverse their order:
OUTPUT-PATH(i):
if P[i] == -1:
output "No Path"
return
S = empty stack
u = i
while u != 1:
push u onto S
u = P[u]
output v1
while S is not empty:
output v(pop(S))
Every assignment is made together with a relaxation through , so these predecessor edges reconstruct a shortest path.
Q.4
(i)
For , take the complete simple directed graph and delete only :
Its edge count is . After one complete scan, every with has distance ; during the second scan, an edge sets . Thus two scans always suffice.
They are sometimes necessary: order every edge entering before all edges leaving . Then no predecessor of is reachable when those entering edges are scanned in the first repetition, so remains infinite until the second repetition. Therefore
(ii)
Take all edges directed from a higher index to a lower index, together with the forward chain:
The two sets are disjoint, so
The only way to reach a larger-indexed vertex from is along the forward chain. If those chain edges are scanned in reverse order,
only one new chain vertex becomes reachable per repetition. Thus needs repetitions. Bellman--Ford always propagates a shortest simple path of at most edges within repetitions, so