跳到主要内容

京都大学 情報学研究科 数理工学専攻 2025年8月実施 グラフ理論

Author

祭音Myyura

Description

頂点集合 VV,枝集合Aを持つ有向グラフ G=(V,A)G=(V,A) が与えられたものとする.VV に属する頂点の個数を nn, AA に属する枝の本数を mm とする. 任意の2頂点 p,qVp,q \in V について,ppから qq への有向路が存在するとき,pqp \leadsto q とする.以下の問いに答えよ.

(1) GG に有向閉路が存在するか否かを O(n+m)O(n+m) 時間で判定する手順を示せ.

(2) 以下の (a),(b),(c) の各条件について,GG がその条件を満たすか否かを O(n+m)O(n+m) 時 間で判定する手順をそれぞれ示せ.

  • (a) どの2頂点 p,qV (pq)p,q \in V \ (p \neq q) についても,pqp \leadsto qqpq \leadsto p の高々一方が成り立つ.
  • (b) どの2頂点 p,qV (pq)p,q \in V \ (p \neq q) についても,pqp \leadsto qqpq \leadsto p の両方が成り立つ.
  • (c) どの2頂点 p,qV (pq)p,q \in V \ (p \neq q) についても,pqp \leadsto qqpq \leadsto p の少なくとも一方 が成り立つ.

Kai

(i)

Use depth-first search with three colors.

Each vertex is colored as follows:

  • white: not yet discovered;
  • gray: discovered but not finished, so it is currently in the DFS recursion stack;
  • black: finished.

The algorithm is as follows.

for each vertex v in V:
color[v] = white

for each vertex v in V:
if color[v] = white:
DFS(v)

DFS(u):
color[u] = gray
for each arc u -> v:
if color[v] = gray:
report "there is a directed cycle"
if color[v] = white:
DFS(v)
color[u] = black

The running time is O(n+m)O(n+m), since every vertex is visited once and every arc is examined once.

To prove correctness, first suppose that DFS finds an arc uvu\to v such that vv is gray. Since vv is gray, vv is an ancestor of uu in the current DFS recursion stack. Hence there is already a directed path vuv\leadsto u. Together with the arc uvu\to v, this gives a directed cycle.

Conversely, suppose that GG contains a directed cycle

v1v2vkv1.v_1\to v_2\to \cdots \to v_k\to v_1.

Let viv_i be the first vertex on this cycle discovered by DFS. At that moment, all other vertices on the cycle are still white. Before viv_i becomes black, DFS explores all vertices reachable from viv_i, in particular the rest of the cycle. Thus DFS eventually reaches the predecessor of viv_i on the cycle and then examines the arc back to viv_i, while viv_i is still gray. Therefore DFS finds an arc to a gray vertex.

(ii)

(a)

The condition is equivalent to the following

The remaining directed graph has no directed cycle.

Indeed, if there is a directed cycle containing at least two distinct vertices, then any two vertices on the cycle are mutually reachable, contradicting the condition. Conversely, if two distinct vertices pp and qq are mutually reachable, then a path from pp to qq together with a path from qq to pp forms a directed cycle containing at least two distinct vertices.

Therefore, we may apply the algorithm from part (i). If the remaining graph is acyclic, condition (a) holds; otherwise it does not. The running time is O(n+m)O(n+m).

(b)

Tarjan's algorithm

(c)

Let the strongly connected components of GG be

C1,C2,,Ck.C_1,C_2,\dots,C_k.

Contract each strongly connected component into one vertex. The resulting graph is called the condensation graph of GG. It is always a DAG.

Algorithm:

  1. Compute the strongly connected components of GG.

  2. Construct the condensation graph DD.

  3. Compute a topological ordering of DD:

    C1,C2,,Ck.C_1,C_2,\dots,C_k.
  4. For each i=1,2,,k1i=1,2,\dots,k-1, check whether DD contains the arc

    CiCi+1.C_i\to C_{i+1}.
  5. If all such arcs exist, output true. Otherwise, output false.

To implement step 4 in linear time, mark all arcs of DD in a hash set or Boolean table indexed by component numbers. Equivalently, when scanning all arcs of GG, record the arcs between different components. Since each original arc is scanned only a constant number of times, the total running time remains O(n+m)O(n+m).

Now we prove correctness.

First suppose that the algorithm outputs true. Then for every consecutive pair in the topological ordering, there is an arc

CiCi+1.C_i\to C_{i+1}.

Hence for any i<ji<j, there is a directed path

CiCi+1Cj.C_i\to C_{i+1}\to \cdots \to C_j.

Therefore every pair of components is comparable by reachability. Since vertices inside the same strongly connected component are mutually reachable, for any two vertices p,qVp,q\in V, at least one of pqp\leadsto q and qpq\leadsto p holds.

Conversely, suppose that GG satisfies the condition. Then any two strongly connected components of GG are comparable by reachability in the condensation graph DD. Take a topological ordering

C1,C2,,CkC_1,C_2,\dots,C_k

of DD. For each consecutive pair Ci,Ci+1C_i,C_{i+1}, the topological order forbids a path from Ci+1C_{i+1} to CiC_i. Hence, by the assumed condition, there must be a path from CiC_i to Ci+1C_{i+1}. Since CiC_i and Ci+1C_{i+1} are consecutive in a topological ordering, this path cannot pass through any other component. Therefore the path must be a single arc

CiCi+1.C_i\to C_{i+1}.

Thus the algorithm will output true.

The running time is O(n+m)O(n+m), because SCC computation, condensation graph construction, topological sorting, and the final scan are all linear.