跳到主要内容

京都大学 情報学研究科 数理工学専攻 2021年8月実施 アルゴリズム基礎

Author

祭音Myyura

Description

日本語版

G=(V,E)G=(V,E) を点集合 VV ,枝集合 EE から成る単純有向グラフとする. R(u;G)R(u; G)GG において点 uu から有向路で到達できる点の集合と定め,dist(u,v,;G)\text{dist}(u, v,; G) を点 uu から点 vv へ至る GG の有向路の最短の長さとする. vR(u;G)v \notin R(u; G) のときは dist(u,v,;G)V\text{dist}(u, v,; G) \triangleq |V| と定める. 有向グラフ GG から有向枝 eEe \in E を削除した有向グラフを GeG - e と記す. s,ts, tVV の二点とする. GG は隣接リストにより貯えられているとする.以下の問いに答えよ.

(i) tR(s;G)t \in R(s; G) と仮定する. 点 ss から点 tt へ至る有向路で最短のものを求める O(V+E)O(|V| + |E|) 時間アルゴリズムを与えよ.

(ii) dist(s,t;Ge)>dist(s,t;G)\text{dist}(s, t; G - e) > \text{dist}(s, t; G) を満たす有向枝 eEe \in E が存在するかどうかを判定する O(V+E)O(|V| + |E|) 時間アルゴリズムを与えよ.

(iii) dist(s,t;G)=dist(t,s;G)=3<dist(s,t;Ge)=dist(t,s;Ge)\text{dist}(s, t; G) = \text{dist}(t, s; G) = 3 < \text{dist}(s, t; G - e) = \text{dist}(t, s; G - e) である二点 s,tVs, t \in V 有向枝 eEe \in E をもつ有向グラフ G=(V,E)G=(V, E) の例を作成せよ.

English Version

Let G=(V,E)G=(V,E) be a simple directed graph with a vertex set VV and an edge set EE. Let R(u;G)R(u; G) denote the set of vertices reachable from a vertex uu by a directed path in GG and dist(u,v;G)\text{dist}(u, v; G) denote the shortest length of a path from a vertex uu to a vertex vv in GG, where we set dist(u,v,;G)V\text{dist}(u, v,; G) \triangleq |V| if vR(u;G)v \notin R(u; G). Let GeG − e denote the directed graph obtained from GG by removing a directed edge eEe \in E. Let ss and tt be two vertices in VV. Assume that GG is stored in adjacency lists. Answer the following questions.

(i) Assume that tR(s;G)t \in R(s; G). Give an O(V+E)O(|V | + |E|)-time algorithm that computes a directed path with the shortest length from ss to tt.

(ii) Give an O(V+E)O(|V |+|E|)-time algorithm that tests whether there exists a directed edge eEe \in E such that dist(s,t;Ge)>dist(s,t;G)\text{dist}(s, t; G − e) > \text{dist}(s, t; G).

(iii) Construct an example of a directed graph G=(V,E)G = (V, E) that contains two vertices s,tVs, t \in V and a directed edge eEe ∈ E such that dist(s,t;G)=dist(t,s;G)=3<dist(s,t;Ge)=dist(t,s;Ge)\text{dist}(s, t; G) = \text{dist}(t, s; G) = 3 < \text{dist}(s, t; G − e) = \text{dist}(t, s; G − e).

题目描述

G=(V,E)G=(V,E) 为以邻接表存储的简单有向图。令 R(u;G)R(u;G) 为从 uu 出发沿有向路可达的顶点集合,dist(u,v;G)\operatorname{dist}(u,v;G) 为从 uuvv 的最短有向路长度;若 vR(u;G)v\notin R(u;G),约定 dist(u,v;G)=V\operatorname{dist}(u,v;G)=|V|。以 GeG-e 表示删去有向边 eEe\in E 后的图,s,tVs,t\in V。回答:

  1. 假设 tR(s;G)t\in R(s;G),给出在 O(V+E)O(|V|+|E|) 时间内求一条从 sstt 的最短有向路的算法。

  2. 给出在 O(V+E)O(|V|+|E|) 时间内判定是否存在边 eEe\in E 使 dist(s,t;Ge)>dist(s,t;G)\operatorname{dist}(s,t;G-e)>\operatorname{dist}(s,t;G) 的算法。

  3. 构造一个含顶点 s,ts,t 和边 ee 的有向图,使

    dist(s,t;G)=dist(t,s;G)=3<dist(s,t;Ge)=dist(t,s;Ge).\operatorname{dist}(s,t;G)=\operatorname{dist}(t,s;G)=3 <\operatorname{dist}(s,t;G-e) =\operatorname{dist}(t,s;G-e).

Kai

(i)

We use BFS to compute shortest paths in an unweighted graph.

BFS(s, G=(V, E)):
for each v in V set dist(s, v; G) = |V|
for each v in V set visited(v) = 0
for each v in V set pred(v) = -1
dist(s) = 0
visited(s) = 1
set Q to be the empty queue
Q.enqueue(s)
while Q is not empty do:
u = Q.dequeue()
for each neighbor v of u do:
if visited(v) = 0 then:
visited(v) = 1
Q.enqueue(v)
dist(s, v; G) = dist(s, u; G) + 1
pred(v) = u

The time complexity of BFS is O(V+E)O(|V| + |E|) when GG is stored in adjacency lists. Starting at tt, follow pred until ss and reverse the resulting sequence; this outputs a shortest directed path.

(ii)

The idea is to find an edge used by every shortest path from ss to tt. Let Es,tE_{s,t} denote the set of all arcs lying on shortest ss-tt paths. To find Es,tE_{s,t}, do the following:

  • Use BFS in GG to compute dist(s,u;G)\operatorname{dist}(s,u;G) for every uVu\in V.

  • Let GTG^T be obtained by reversing every arc of GG. Use BFS from tt in GTG^T to compute dist(t,v;GT)=dist(v,t;G)\operatorname{dist}(t,v;G^T)=\operatorname{dist}(v,t;G) for every vVv\in V.

  • Scan every arc (u,v)(u,v) and put it in Es,tE_{s,t} exactly when

    dist(s,u;G)+1+dist(t,v;GT)=dist(s,t;G).\operatorname{dist}(s,u;G)+1+\operatorname{dist}(t,v;G^T) =\operatorname{dist}(s,t;G).

The two BFS runs and this scan take O(V+E)O(|V|+|E|) time. For brevity, write

ds(u)=dist(s,u;G),dt(v)=dist(v,t;G),D=ds(t).d_s(u)=\operatorname{dist}(s,u;G),\quad d_t(v)=\operatorname{dist}(v,t;G),\quad D=d_s(t).

If D=VD=|V| or D=0D=0, return false. Otherwise initialize c0,,cD1c_0,\ldots,c_{D-1} to zero and scan every arc (u,v)(u,v). Whenever

ds(u)+1+dt(v)=D,d_s(u)+1+d_t(v)=D,

increment cds(u)c_{d_s(u)}. Return true exactly when some ci=1c_i=1.

Indeed, the tested arcs are precisely the arcs lying on shortest ss-tt paths. Every shortest path uses exactly one such arc from layer ii to layer i+1i+1. Hence an arc lies on every shortest path exactly when it is the unique tested arc between some consecutive layers. Deleting that arc, and only then, strictly increases the distance. The two BFS runs and the edge scan take O(V+E)O(|V|+|E|) time.

(iii)

V={s,t,v1,v2,v3,v4}E={(s,v2),(t,v2),(v2,v1),(v1,s),(v1,t),(v2,v3),(v3,v4),(v4,s),(v4,t)}e=(v2,v1)G=(V,E)\begin{aligned} V &= \{s, t, v_1, v_2, v_3, v_4\} \\ E &= \{(s, v_2), (t, v_2), (v_2, v_1), (v_1, s), (v_1, t), (v_2, v_3), (v_3, v_4), (v_4, s), (v_4, t)\} \\ e &= (v_2, v_1) \\ G &= (V, E) \end{aligned}