2020-1ํ•™๊ธฐ, ๋Œ€ํ•™์—์„œ โ€˜์•Œ๊ณ ๋ฆฌ์ฆ˜โ€™ ์ˆ˜์—…์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋ฐ”๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ง€์ ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :)

6 minute read

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 ๊ฒฝ์šฐ๋„ ๋Š˜ ์ค€๋น„ํ•ด์•ผ ํ•œ๋‹ค. ๐Ÿคฉ


references


  1. ์ด update() rule์„ edge-relaxation ๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค.ย 

  2. $s$์™€ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์ฒซ iteration์—์„œ๋„ ๋ฐ”๋กœ dist ๊ฐ’์ด correct set ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋…ธ๋“œ๋Š” negative edge๊ฐ€ ํฌํ•จ๋œ ๋‹ค๋ฅธ ๊ฒฝ๋กœ๊ฐ€ ์žˆ์–ด ๊ทธ๊ฒƒ์ด shortest path๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๋‹ค.ย 

Categories:

Updated: