Bellman-Ford Algorithm
2020-1ํ๊ธฐ, ๋ํ์์ โ์๊ณ ๋ฆฌ์ฆโ ์์ ์ ๋ฃ๊ณ ๊ณต๋ถํ ๋ฐ๋ฅผ ์ ๋ฆฌํ ๊ธ์ ๋๋ค. ์ง์ ์ ์ธ์ ๋ ํ์์ ๋๋ค :)
๐ฅ <Bellman-Ford Algorithm>์ negative edge๊ฐ ์๋ Acyclic Directed Graph์์์ Shortest path๋ฅผ ์ฐพ๋ ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค. ๋ง์ฝ negative cycle์ด ์๋ค๋ฉด, <Bellman-Ford Algorithm>์ ์ธ ์ ์์ต๋๋ค ๐ฅ
Shortest Path with Negative Edge
<Dijkstraโs Algorithm>์ ์๋์ Invariant๋ฅผ ๊ฐ๋๋ค.
The shortest path from the starting point $s$ to any node $v$ must pass exclusively through nodes that are closer than $v$.
์ด๋, โpass exclusivelyโ๋ $s$ to $v$์ shortest path๊ฐ ๊ฐ ๋ ธ๋๋ฅผ ๋จ ํ ๋ฒ์ฉ๋ง ๋ฐฉ๋ฌธํ๋ค๋ ๋ง์ด๋ค! ์ด ์ฑ์ง์ ๊ทธ๋ํ์ positive edge๋ง ์กด์ฌํ๋ค๋ฉด, ์ฑ๋ฆฝํ๋ค. ํ์ง๋ง, negative edge๊ฐ ์กด์ฌํ๋ค๋ฉด, negative cycle์ ์ํด ์ด๋ฏธ ๋ฐฉ๋ฌธํ ๋ ธ๋๋ฅผ ํ๋ฒ ๋ ๋ฐฉ๋ฌธํ ๊ฐ๋ฅ์ฑ์ด ์๊ณ , ์ด๊ฒ์ โpass exclusiveโ๋ฅผ ์๋ฐฐํ๋ค!
Golden rule for the shortest path
shortest path์๋ ๋ค์์ ์ค์ํ ์ฑ์ง์ด ์๋ค.
- Shortest path is one branch of path tree.
update()
gives the correct dist($v$) value when- $u$ is the second-last node of the shortest path to $v$.
- dist($u$) is correctly set.
์์ ๋ ์ฑ์ง๋ก๋ถํฐ Bellman-Ford ์๊ณ ๋ฆฌ์ฆ์ ์ด๋ป๊ฒ ์ ๋ํ๋์ง ์ดํด๋ณด์.
Shortest path is one branch of path tree

Graph and its Shortest path tree
diagram from ใAlgorithms(Dasgupta)ใ
Graph๊ฐ Negative Cycle์ ๊ฐ์ง ์๋๋ค๋ฉด, Graph์ shortest path๋ Shortest Path Tree๋ฅผ ์ด๋ฃฌ๋ค.
distance update rule
procedure update((u, v) โ E)
if dist(v) > dist(u) + w(u, v):
dist(v) = dist(u) + w(u, v)
update()
1 gives the correct dist($v$) value when
- $u$ is the second-last node of the shortest path to $v$.
- dist($u$) is correctly set.
Proof.
Consider the shortest path $\pi = su_1u_2u_3\cdots u_k t$ from $s$ to $t$.
Then every subpath $\pi_i = su_1\cdots u_i$ is
(1) the shortest path from $s$ to $u_i$, otherwise we can decrease $\texttt{dist}(t)$
(2) it is simple! otherwise there is a cycle, and we can still decrease $\texttt{dist}(t)$
์์ ๋ ๋ช ์ ๋ฅผ ๋ค์ ๊ธฐ์ ํ๋ฉด, โthe shortest path from $s$ to $u_i$ consists of exactly $i$ edges.โ
์์ ์ฌ์ค์ ์ํด $\texttt{u_1}$์ ๊ฒฝ์ฐ๋ฅผ ์๊ฐํ์ ๋, $u_1$์ ๊ทธ๋ํ์ ์กด์ฌํ๋ ๋ชจ๋ edge์ ๋ํด ๋งจ์ฒ์ update ํ์ ๋, ๊ทธ ๊ฐ์ด final value๋ก ์ค์ ๋ ๊ฒฝ์ฐ์ผ ๊ฒ์ด๋ค.
์ด๋ฐ $\texttt{dist}$ update ๊ณผ์ ์ ๋ฐ๋ณตํด์, $i$๋ฒ ์งํํ๋ค๋ฉด, $\texttt{dist}(u_i)$๊น์ง๋ final value๊ฐ ์ ๋๋ก ์ค์ ๋ ๊ฒ์ด๋ค. (๊ท๋ฉ๋ฒ์ด๋ค, ๊ท๋ฉ๋ฒ!)
์์ ๊ฐ์ ์๋ฆฌ์ ์ํด ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๋ ธ๋์๊น์ง final value๋ฅผ ์ค์ ํ๋ ค๋ฉด, shortest path์ ์ต๋ ๊ธธ์ด์ธ $V-1$๋ฒ ๋งํผ ์ด update ๊ณผ์ ์ ๋ฐ๋ณตํ๋ฉด ๋๋ค! $\blacksquare$ ๐
์ด์ Bellman-Ford ์๊ณ ๋ฆฌ์ฆ์์ ์ด ๋ช ์ ๋ฅผ ์ด๋ป๊ฒ ํ์ฉํ๋์ง๋ฅผ ์ดํด๋ณด์!
Algorithm.
์์์ $s$๋ $\texttt{dist}(s)=0$์ผ๋ก, ๋๋จธ์ง ๋
ธ๋๋ ๋ชจ๋ INF
๋ก ์ด๊ธฐํํ๋ค. ์ด์ ์์์ $s$์ ์ฐ๊ฒฐ๋ ๋
ธ๋๋ค์ ์ดํด๋ณด์. See node $u_i$ s.t. $(s, u_i) \in E$
$\texttt{dist}(u_i)$์ ๊ฐ์ update()
๊ท์น์ ๋ฐ๋ผ $\texttt{dist}(u_i) = \texttt{dist}(s) + w(u_i, v)$๊ฐ ๋๋ค. ์์ ๋ช
์ ๋ฅผ ๋ค์ ๋ณด์. ๋ง์ฝ $\texttt{dist}(u_i)$์ ๋ํด $s$๊ฐ second last ๋
ธ๋์ด๊ณ , $\texttt{dist}(s)$๊ฐ correctly set๋ผ๋ฉด, $\texttt{dist}(u_i)$์๋ update()
๋ฅผ ํตํด shortest distance๊ฐ ์ ์ฅ๋๋ค.
์ด์ , ๋ชจ๋ edge $E$์ ๋ํด์ update()
rule์ ์ ์ฉํด๋ณด์. $\texttt{dist}(s)$ ์ธ์๋ ๋ชจ๋ INF
๋ก ์ด๊ธฐํ ๋์ด ์๊ธฐ ๋๋ฌธ์ $s$์ ์ฐ๊ฒฐ๋ ๋
ธ๋๋ค์ $\texttt{dist}$ ๊ฐ๋ง ๊ฐฑ์ ๋๋ค. ์ด๋์ ๊ฐฑ์ ๋๋ ๋
ธ๋๋ค ์ค ์ ์ด๋ ํ๋๋ $s$๋ฅผ second last ๋
ธ๋๋ก ๊ฐ๋๋ค.2
๋ฐ๋ผ์ ์ฒซ iter์ update()
๋ฅผ ํตํด correctly set๋ ๋
ธ๋ ๋ช๊ฐ๊ฐ ์ถ๊ฐ๋๋ค.
Shortest Path Tree๋ ํธ๋ฆฌ์ ์ฑ์ง์ ๋ฐ๋ผ $V-1$ ๋งํผ์ edge๋ฅผ ๊ฐ๋๋ค. iter ํ๋ฒ์ shortest path์ ํ ๊ฐ๊ฒฉ์ ์ปค๋ฒํ๋ ๊ฒ์ด๋ฏ๋ก ์ฐ๋ฆฌ๋ $V-1$๋ฒ์ iter์ด ํ์ํ๋ค! (๋๋ ์์ ์ฆ๋ช ์์ ์ธ๊ธํ๋๋ก, shortest path์ ์ต๋ ๊ธธ์ด์ธ $V-1$๋ฒ ๋งํผ iterํ๋ค.)
Finding Negative Cycle
Bellman-Ford ์๊ณ ๋ฆฌ์ฆ์ Negative edge๋ฅผ ํ์ฉํ์ง๋ง, Negative Cycle์ ํ์ฉํ์ง ์๋๋ค. ๊ทธ๋์ Bellman-Ford ์๊ณ ๋ฆฌ์ฆ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฅํ๊ธฐ ์ํด์ ๊ทธ๋ํ์ Negative Cycle์ด ์์์ ํ์ธํด์ผ ํ๋ค!!
๊ทธ๋์ ๋ง์ฝ $V-1$๋ฒ ieteration์ ์งํํ ํ ํ ๋ฒ ๋ iteration์ ์งํํ์ ๋์๋ update()
ํจ์๊ฐ ํธ์ถ๋๋ค๋ฉด, ์ฃผ์ด์ง ๊ทธ๋ํ์ Negative Cycle์ด ์๋ค๊ณ ํ๋จํ๋ค.
๋งบ์๋ง
Dijkstra ์๊ณ ๋ฆฌ์ฆ๊ณผ ๋ฌ๋ฆฌ Bellman-Ford ์๊ณ ๋ฆฌ์ฆ์ ๊ทธ ์๋ฆฌ๊ฐ ํ๋ฒ์ ์ ์๋ฟ์ง ์๋ ๊ฒ ๊ฐ๋ค. ํ์ง๋ง ์ค์ ์์ positive edge๋ง ๋์ค๋ ๊ฒ์ ์๋๊ธฐ ๋๋ฌธ์ negative edge ๊ฒฝ์ฐ๋ ๋ ์ค๋นํด์ผ ํ๋ค. ๐คฉ