有向グラフ G=(V,E,l) を考える。
V は頂点集合、E は辺集合、l(e) は辺 e の長さとする。
また、Nv を頂点 v の隣接頂点集合とする。
この時、以下の Shortest(G,s) は、G 上のある頂点 s を始点とし、頂点 s から他の各頂点までの最短距離を求めるアルゴリズムである。
(2) G のすべての辺の長さが非負である場合に、Shortest(G,s) で得られる d(v) が、すべての頂点 v∈V について頂点 s からの最短距離となることを証明せよ。
(3) G に負の長さの辺が存在する場合には、Shortest(G,s) は s からの最短距離を求められない場合があることを証明せよ。
(4) Shortest(G,s) の最悪計算時間とその理由を述べよ。
Let G=(V,E,l) be a directed graph, where V is a set of nodes, E is a set of edges, and l(e) is the non-negative length of edge e.
Let Nv be a set of adjacent nodes of node v. The following algorithm Shortest(G,s) computes the shortest distance from a node s to each of the other nodes.
(1) Table 1 shows the value of d(v) for each v∈V in the process of execution of Shortest(G1,1), where the input graph is G1 and the starting node is 1. Complete Table 1.
(2) Prove that d(v) for each v∈V stores the shortest distance from s to v when Shortest(G,s) terminates.
(3) Prove that Shortest(G,s) may not find the shortest distance if some of the edges take negative length.
Let δ(v) denote the length of a shortest path from s to v.
Suppose that u is the first vertex extracted from T for which d(u)=δ(u).
Let s→P1→x→y→P2→u be a shortest path from s to u, where x satifies d(x)=δ(x) but y does not.
When x is extracted from T, since y is adjacent to x, d(y) will be updated
d(y)=d(x)+l(xy)≤δ(x)+l(xy)+l(y→P2→u)=δ(u)≤d(u)
Now both y and u are in T when u is chosen.
Note that by assumption u is the first vertex extracted from T for which d(u)=δ(u), hence either d(y)=δ(y) or y is chosen after u, which means that d(u)≤d(y).
But by assumption we have d(y)=δ(y), hence we have
If we stores the vertex set T as a heap, and edges as an adjacent list, then finding u of minimum d(u) takes O(log∣V∣).
Note that we need update d(v) of every v∈T∩Nu, which can be done by inserting a "new vertex" v of updated d(v) into heap T,
hence the size of T is at most ∣E∣.
Therefore, the time complexity of Shortest(G,s) is O(∣E∣log∣V∣).