東京大学 情報理工学系研究科 創造情報学専攻 2009年8月実施 筆記試験 第1問
Author
Description
Given a directed graph , we would like to find all-pairs shortest path lengths which are the all shortest path lengths between every pair of vertices, where the size of the set . Let denote a directed edge from a vertex to a vertex , and denote the length of the edge . The graph may have a negative length edge but does not have any negative length cycle. The length of the edge from the vertex to the same vertex , and when there exists no edge from the vertex to the vertex , .
Algorithm 1 on the next page outputs the single-source shortest path lengths. Let be a single source vertex, the shortest path length from the vertex to a vertex is stored in . Algorithm 2 outputs the all-pairs shortest path lengths table , where the length of the shortest path from a vertex to a vertex is stored in . Each algorithm uses and to store interim results, respectively. Answer the following questions.
(1) Apply Algorithm 1 to the graph in Figure 1 to obtain the shortest path length from a single-source vertex . Table 1 shows in Algorithm 1. Show the single-source path length , and from the single-source vertex .
(2) Apply Algorithm 2 to the graph in Figure 1 to obtain the all-pairs shortest path lengths. Table 2 shows in Algorithm 2. Show the selected vertex in the Main Loop and the corresponding table , and .
(3) To obtain all-pairs shortest path lengths, consider Algorithm 1-ALL which applies Algorithm 1 for all vertices in as a single-source vertex. Compare Algorithm 1-ALL and Algorithm 2.
Table 1: in Algorithm 1
| destination | |
|---|---|
| 0 | |
Table 2: in Algorithm 2
| source\destination | |||||
|---|---|---|---|---|---|
| 0 | 1 | 5 | 9 | ||
| 0 | 1 | 3 | |||
| 0 | -1 | ||||
| 1 | 0 | 1 | |||
| 1 | 0 |
Algorithm 1
for all v ∈ V do
d^(0)(v) = ∞
end for
d^(0)(s) = 0
/* Main Loop */
for k = 1 .. n − 1 do
for all e_uv ∈ E do
d^(k)(v) = min(d^(k−1)(v), d^(k−1)(u) + δ_uv)
end for
end for
Algorithm 2
k = 0
for all u ∈ V do
for all v ∈ V do
D^(k)(u,v) = δ_uv
end for
end for
/* Main Loop */
for all w ∈ V do
for all u ∈ V do
for all v ∈ V do
D^(k+1)(u,v) = min(D^(k)(u,v), D^(k)(u,w) + D^(k)(w,v))
end for
end for
k = k + 1
end for
题目描述
给定含 个顶点的有向图 ,求任意两顶点之间的最短路径长度。用 表示从 到 的有向边, 表示其长度。图中允许负权边,但不存在负权环;约定 ,若 到 无边,则 。
原文所给算法 1 求单源最短路:源点为 , 存储 到 的最短距离,并用 存储中间结果。算法 2 求全源最短路矩阵 ,其中 存储 到 的最短距离,并用 存储中间结果。
-
对图 1 的 ,以 为源点执行算法 1。初值表为
终点 写出 。
-
对同一图执行算法 2。初始矩阵为
源点 \ 终点 写出主循环各轮选中的 ,以及相应的 。
-
定义算法 1-ALL:把 中每个顶点依次作为源点运行算法 1,以得到全源最短路。比较算法 1-ALL 与算法 2。
Algorithm 1
for all v ∈ V do
d^(0)(v) = ∞
end for
d^(0)(s) = 0
/* Main Loop */
for k = 1 .. n − 1 do
for all e_uv ∈ E do
d^(k)(v) = min(d^(k−1)(v), d^(k−1)(u) + δ_uv)
end for
end for
Algorithm 2
k = 0
for all u ∈ V do
for all v ∈ V do
D^(k)(u,v) = δ_uv
end for
end for
/* Main Loop */
for all w ∈ V do
for all u ∈ V do
for all v ∈ V do
D^(k+1)(u,v) = min(D^(k)(u,v), D^(k)(u,w) + D^(k)(w,v))
end for
end for
k = k + 1
end for
Kai
(1)
原巻の Algorithm 1 は,各辺について を前ラウンドの値から代入する形で記されている。このままでは,同じ頂点への候補を上書きしてしまう。以下では各ラウンドを で初期化し,各辺について
と候補を累積する Bellman–Ford 法として計算する。 は高々 本の辺を使って到達する最短距離である。
| 0 | 0 | ||||
| 1 | 0 | 1 | 5 | 9 | |
| 2 | 0 | 1 | 2 | 4 | 6 |
| 3 | 0 | 1 | 2 | 1 | 5 |
| 4 | 0 | 1 | 2 | 1 | 2 |
例えば への距離は により 、 へはさらに長さ1の辺を使って2となる。負閉路がないため最短路には単純路を選べ、必要な辺数は高々 本である。
(2)
の順で選ぶ。各行・列は の順である。
各段では、新しく許された中継点 を通らない経路と、 と分けられる経路の最小値を比較する。全頂点を許した が全点対最短距離である。
(3)
Algorithm 1は修正済みBellman–Ford法、Algorithm 2はFloyd–Warshall法として比較する。どちらも負辺を扱えるが、ここでは負閉路がないことを使っている。
Algorithm 1は1始点につき ラウンドで全 辺を調べる。各ラウンドの長さ の初期化も数えると 、全始点なら である。通常の の範囲では、それぞれ 、 と書ける。Algorithm 2は中継点と始終点の三重ループにより 。
従って の密なグラフでは、全始点Bellman–Fordの に対してFloyd–Warshallが で有利である。 の疎なグラフでは両者とも だが、前者は辺リストを直接利用できる。作業領域はBellman–Fordが2本の距離ベクトルで 、Floyd–Warshallが行列を再利用して 。全点対の結果自体を保存するなら、いずれも の出力領域が必要となる。