東京大学 情報理工学系研究科 創造情報学専攻 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 |
题目描述
给定含 个顶点的有向图 ,求任意两顶点之间的最短路径长度。用 表示从 到 的有向边, 表示其长度。图中允许负权边,但不存在负权环;约定 ,若 到 无边,则 。
原文所给算法 1 求单源最短路:源点为 , 存储 到 的最短距离,并用 存储中间结果。算法 2 求全源最短路矩阵 ,其中 存储 到 的最短距离,并用 存储中间结果。
-
对图 1 的 ,以 为源点执行算法 1。初值表为
终点 写出 。
-
对同一图执行算法 2。初始矩阵为
源点 \ 终点 写出主循环各轮选中的 ,以及相应的 。
-
定义算法 1-ALL:把 中每个顶点依次作为源点运行算法 1,以得到全源最短路。比较算法 1-ALL 与算法 2。算法正文及图 沿用原文图片。