京都大学 情報学研究科 数理工学専攻 2023年8月実施 グラフ理論
Author
祭音Myyura
Description
日本語版
節点
(i)
が成り立つことを示せ。
(ii)
English Version
Let
Suppose that a vertex
(i) Prove that there is no negative cycle in
(ii) Show an
Kai
(i)
(a)
Prove by contradiction:
Assume that there exists an edge
Let
Let
W.l.o.g we assume that
Assume that
and
Hence
Therefore, if there is no negative cycle, then
(b)
Prove by contradiction: Assume that there exists a negative cycle
From the condition we know that
Hence
sum over all the equations,
which is contradictory to the fact that
Therefore, if
(ii)
Bellman-Ford algorithm (Wiki)
function BellmanFord(list vertices, list edges, vertex source) is
// This implementation takes in a graph, represented as
// lists of vertices (represented as integers [0..n-1]) and edges,
// and fills two arrays (distance and predecessor) holding
// the shortest path from the source to each vertex
distance := list of size n
predecessor := list of size n
// Step 1: initialize graph
for each vertex v in vertices do
// Initialize the distance to all vertices to infinity
distance[v] := inf
// And having a null predecessor
predecessor[v] := null
// The distance from the source to itself is, of course, zero
distance[source] := 0
// Step 2: relax edges repeatedly
repeat |V|−1 times:
for each edge (u, v) with weight w in edges do
if distance[u] + w < distance[v] then
distance[v] := distance[u] + w
predecessor[v] := u
// Step 3: check for negative-weight cycles
for each edge (u, v) with weight w in edges do
if distance[u] + w < distance[v] then
predecessor[v] := u
// A negative cycle exists; find a vertex on the cycle
visited := list of size n initialized with false
visited[v] := true
while not visited[u] do
visited[u] := true
u := predecessor[u]
// u is a vertex in a negative cycle, find the cycle itself
ncycle := [u]
v := predecessor[u]
while v != u do
ncycle := concatenate([v], ncycle)
v := predecessor[v]
error "Graph contains a negative-weight cycle", ncycle
return distance, predecessor