京都大学 情報学研究科 数理工学専攻 2019年8月実施 アルゴリズム基礎
Author
祭音Myyura
Description
日本語版
(1) 任意の点
(2)
(3) 異なる二点
English Version
Let
(1) Let
(2) Show how to test whether
(3) Let
Kai
Let
(1)
Let
Since both paths start at
Denote by
Let
We show that
Since
Also,
and
Since every subpath of a shortest path is also a shortest path, both
Combining these equalities, we obtain
Now consider the cycle formed by the path
Because
The length of this cycle is
Since
which is odd.
(2)
The algorithm can be established from the following statement and (1):
- A simple undirected graph
is bipartite iff it contains no odd length cycle.
(
Assume that there exists a cycle
W.l.o.g we assume that
Therefore, if
(
For every edge
By (1), this edge lies on an odd cycle, so G is not bipartite. If no such edge exists, all edges go between the two color classes, hence the graph is bipartite.
Algorithm
Let
algorithm IsBipartite(G):
for each v in V:
color[v] <- null
choose an arbitrary vertex s
color[s] <- RED
Q <- create an empty queue
Q.enqueue(s)
while Q is not empty:
n1 <- Q.dequeue()
for each n2 in n1.Adj():
if color[n2] = null:
if color[n1] = RED:
color[n2] <- BLUE
else:
color[n2] <- RED
Q.enqueue(n2)
else:
if color[n2] = color[n1]:
return "Graph is not bipartite"
return "Graph is bipartite"
Since BFS scans each vertex and each edge a constant number of times, the running time is
(3)
Let
for each v in V:
dist[v] = INF
cnt[v] = 0
dist[s] = 0
cnt[s] = 1
Q.push(s)
while Q is not empty:
u = Q.pop()
for each v in Adj[u]:
if dist[v] == INF:
dist[v] = dist[u] + 1
cnt[v] = cnt[u]
Q.push(v)
else if dist[v] == dist[u] + 1:
cnt[v] = min(2, cnt[v] + cnt[u])
if cnt[t] == 1:
return "unique"
else:
return "not unique"