跳到主要内容

東京大学 新領域創成科学研究科 メディカル情報生命専攻 2016年8月実施 問題10

Author

zephyr

Description

We define the shortest distance from a vertex ii to a vertex jj on a graph as the number of edges in a path from ii to jj that contains the smallest number of edges, except that the shortest distance is ++\infty when no such path exists and that it is 00 when ii and jj are identical.

(1) Let us consider the directed graph shown below.

  • (A) Show the adjacency matrix.
  • (B) Show a matrix S\mathbf{S}, whose element si,js_{i,j} is the shortest distance from a vertex ii to a vertex jj.

(2) Suppose we are given a simple directed graph G=(V,E)G = (V, E), where the vertex set V={1,2,,n}V = \{1, 2, \ldots, n\} and EE is the edge set. EE is represented by a matrix D(0)=(di,j(0))\mathbf{D^{(0)}} = (d_{i,j}^{(0)}), where

di,j(0)={0(if i=j)1(if an edge ij exists)+(otherwise)d_{i,j}^{(0)} = \begin{cases} 0 & \text{(if } i = j \text{)} \\ 1 & \text{(if an edge } i \to j \text{ exists)} \\ +\infty & \text{(otherwise)} \end{cases}
  • (A) Let Vi,j(k)={1,2,,k}{i,j}\mathbf{V_{i,j}^{(k)}} = \{1, 2, \ldots, k\} \cup \{i, j\}. Let Ei,j(k)\mathbf{E_{i,j}^{(k)}} be the set of edges in EE that start from and end at a vertex in Vi,j(k)\mathbf{V_{i,j}^{(k)}}. Let di,j(k)d_{i,j}^{(k)} be the shortest distance from a vertex ii to a vertex jj on a directed graph Gi,j(k)=(Vi,j(k),Ei,j(k))G_{i,j}^{(k)} = (\mathbf{V_{i,j}^{(k)}}, \mathbf{E_{i,j}^{(k)}}), and let D(k)=(di,j(k))\mathbf{D^{(k)}} = (d_{i,j}^{(k)}). Express D(1)\mathbf{D^{(1)}} in terms of D(0)\mathbf{D^{(0)}}.

  • (B) D(k+1)\mathbf{D^{(k+1)}} can be computed from D(k)\mathbf{D^{(k)}} as follows. Fill in the two blanks.

di,j(k+1)=min(di,j(k),ddd+ddd)d_{i,j}^{(k+1)} = \min \left( d_{i,j}^{(k)}, \boxed{\phantom{ddd}} + \boxed{\phantom{ddd}} \right)
  • (C) Given GG, show an algorithm to compute the all-pair shortest distances, and find its time complexity with regard to nn.

我们将从顶点 ii 到顶点 jj 的最短距离定义为图中从 iijj 的包含最少边数的路径中的边数,除了当不存在这样的路径时最短距离为 ++\infty,以及当 iijj 相同时为 00

(1) 让我们考虑下图所示的有向图。

  • (A) 显示邻接矩阵。
  • (B) 显示一个矩阵 S\mathbf{S},其元素 si,js_{i,j} 是从顶点 ii 到顶点 jj 的最短距离。

(2) 假设我们有一个简单的有向图 G=(V,E)G = (V, E),其中顶点集 V={1,2,,n}V = \{1, 2, \ldots, n\} 和边集 EEEE 由矩阵 D(0)=(di,j(0))\mathbf{D^{(0)}} = (d_{i,j}^{(0)}) 表示,其中

di,j(0)={0(如果 i=j1(如果存在边 ij+(否则)d_{i,j}^{(0)} = \begin{cases} 0 & \text{(如果 } i = j \text{)} \\ 1 & \text{(如果存在边 } i \to j \text{)} \\ +\infty & \text{(否则)} \end{cases}
  • (A) 设 Vi,j(k)={1,2,,k}{i,j}\mathbf{V_{i,j}^{(k)}} = \{1, 2, \ldots, k\} \cup \{i, j\}。设 Ei,j(k)\mathbf{E_{i,j}^{(k)}} 为从顶点 Vi,j(k)\mathbf{V_{i,j}^{(k)}} 中的顶点出发并结束于顶点的边集。设 di,j(k)d_{i,j}^{(k)} 为有向图 Gi,j(k)=(Vi,j(k),Ei,j(k))G_{i,j}^{(k)} = (\mathbf{V_{i,j}^{(k)}}, \mathbf{E_{i,j}^{(k)}}) 上从顶点 ii 到顶点 jj 的最短距离,并设 D(k)=(di,j(k))\mathbf{D^{(k)}} = (d_{i,j}^{(k)})。用 D(0)\mathbf{D^{(0)}} 表示 D(1)\mathbf{D^{(1)}}
  • (B) D(k+1)\mathbf{D^{(k+1)}} 可以从 D(k)\mathbf{D^{(k)}} 计算如下。填写两个空格。
di,j(k+1)=min(di,j(k),ddd+ddd)d_{i,j}^{(k+1)} = \min \left( d_{i,j}^{(k)}, \boxed{\phantom{ddd}} + \boxed{\phantom{ddd}} \right)
  • (C) 给定 GG,展示一个算法来计算所有对的最短距离,并找到其关于 nn 的时间复杂度。

Kai

(1)

(A)

The adjacency matrix A\mathbf{A} for the graph is a square matrix where the element ai,ja_{i,j} is 1 if there is an edge from vertex ii to vertex jj, and 0 otherwise.

A=[0100000001000010000101000000100100000010000000110]\mathbf{A} = \begin{bmatrix} 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 & 1 & 0 \\ 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 1 & 0 \\ \end{bmatrix}

(B)

The matrix S\mathbf{S} will be computed using the Floyd-Warshall algorithm. The element si,js_{i,j} is the shortest distance from vertex ii to vertex jj.

  1. Initialize S\mathbf{S} with:

    • si,j=0s_{i,j} = 0 if i=ji = j
    • si,j=1s_{i,j} = 1 if there is an edge from ii to jj
    • si,j=+s_{i,j} = +\infty otherwise
  2. Update S\mathbf{S} using the Floyd-Warshall algorithm:

    si,j=min(si,j,si,k+sk,j)s_{i,j} = \min(s_{i,j}, s_{i,k} + s_{k,j})

The final S\mathbf{S} matrix is:

S=[01243201321202112304123104234102342110]\mathbf{S} = \begin{bmatrix} 0 & 1 & 2 & 4 & \infty & 3 & \infty \\ 2 & 0 & 1 & 3 & \infty & 2 & \infty \\ 1 & 2 & 0 & 2 & \infty & 1 & \infty \\ 1 & 2 & 3 & 0 & \infty & 4 & \infty \\ 1 & 2 & 3 & 1 & 0 & 4 & \infty \\ 2 & 3 & 4 & 1 & \infty & 0 & \infty \\ 2 & 3 & 4 & 2 & 1 & 1 & 0 \\ \end{bmatrix}

(2)

(A)

The matrix D(1)\mathbf{D^{(1)}} is computed by considering paths that may pass through the vertex 1.

di,j(1)=min(di,j(0),di,1(0)+d1,j(0))d_{i,j}^{(1)} = \min(d_{i,j}^{(0)}, d_{i,1}^{(0)} + d_{1,j}^{(0)})

(B)

To find D(k+1)\mathbf{D^{(k+1)}} from D(k)\mathbf{D^{(k)}}, use:

di,j(k+1)=min(di,j(k),di,k+1(k)+dk+1,j(k))d_{i,j}^{(k+1)} = \min(d_{i,j}^{(k)}, d_{i,k+1}^{(k)} + d_{k+1,j}^{(k)})

(C)

The Floyd-Warshall algorithm is suitable for computing all-pair shortest distances:

  1. Initialize D\mathbf{D} where di,jd_{i,j} is 0 if i=ji = j, 1 if there is an edge iji \to j, and ++\infty otherwise.
  2. Update D\mathbf{D} using: di,j=min(di,j,di,k+dk,j)d_{i,j} = \min(d_{i,j}, d_{i,k} + d_{k,j}) for all vertices kk from 1 to nn.

Algorithm:

function FloydWarshall(V, E):
let D be a |V| x |V| matrix of minimum distances
for each vertex v in V:
D[v][v] = 0
for each edge (u, v) in E:
D[u][v] = 1
for each k from 1 to |V|:
for each i from 1 to |V|:
for each j from 1 to |V|:
D[i][j] = min(D[i][j], D[i][k] + D[k][j])
return D

Time Complexity:

The time complexity of the Floyd-Warshall algorithm is O(V3)O(|V|^3) because it involves three nested loops, each running nn times.

Knowledge

最短路径 Floyd-Warshall算法 图论

重点词汇

  • adjacency matrix 邻接矩阵
  • shortest distance 最短距离
  • edge 边
  • vertex 顶点

参考资料

  1. 《算法导论》 第 25 章 最短路径算法