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

7 minute read

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

<๋‹ค์ต์ŠคํŠธ๋ฅด๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜; Dijkstraโ€™s Algorithm>์€ ๊ทธ๋ž˜ํ”„์—์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋Œ€ํ‘œ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  weight๊ฐ€ ๋™์ผํ•œ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค๋ฉด, BFS๋ฅผ ํ†ตํ•ด์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜๋Š” ๊ฐ edge์˜ weight ๊ฐ’์ด ๋‹ค๋ฅด๋”๋ผ๋„, ์•„๋ž˜์™€ ๊ฐ™์ด dummy node๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋ฉด, BFS๋กœ๋„ ์ถฉ๋ถ„ํžˆ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ, ๋งŒ์•ฝ $\max w_i$์˜ ๊ฐ’์ด 10,000์ผ ์ •๋„๋กœ ์ •๋ง ํฐ ๊ฐ’์ด๋ผ๋ฉด, ์šฐ๋ฆฌ๋Š” dummy node๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ์— ๋„ˆ๋ฌด ๋งŽ์€ overhead๋ฅผ ์“ฐ๊ฒŒ ๋œ๋‹ค.

Dijsktraโ€™s Algorithm

BFS์˜ ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด <alarm clock algorithm>์„ ๋„์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.

1. Set $\texttt{alaram}(s)=0$, and for all other nodes $v$, set $\texttt{alarm}(v)=\infty$.

2. Repeat until thereโ€™re no more alarms:

2.1. Say the next alarm rings at time $T$, for node $u$. Then, the distance from $s$ to $u$ is $T$.

2.2. For each neighbor $v$ of $u$, $\texttt{alarm}(v) = \min \left\{ \texttt{alarm}(v), T + \ell (u, v) \right\}$

์œ„์—์„œ ์ œ์‹œ๋œ <alarm clock algorithm>์€ <์šฐ์„ ์ˆœ์œ„ ํ; priority queue>๋ฅผ ํ†ตํ•ด ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค!

Algorithm: dijkstra($G$, $\ell$, $s$)
โ€ป ์ฃผ์˜: ๊ทธ๋ž˜ํ”„ $G$์˜ ๋ชจ๋“  edge๋Š” positive edge์—ฌ์•ผ ํ•œ๋‹ค.


// initialization
for all $u \in V$ do
โ€ƒโ€ƒ $\texttt{dist}(u) = \infty$
โ€ƒโ€ƒ $\texttt{prev}(u) = \texttt{nil}$
end for

// construct heap
$\texttt{dist}(s) = 0$
$H = \texttt{makequeue}(V)$

// explore frontier!
while $H$ is not empty do
โ€ƒโ€ƒ $u = \texttt{deletemin}(H)$
โ€ƒโ€ƒ for all edges $(u, v) \in E$ do
โ€ƒโ€ƒโ€ƒโ€ƒ if $\texttt{dist}(v) > \texttt{dist}(u) + \ell(u, v)$ then
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ $\texttt{dist}(v) = \texttt{dist}(u) + \ell(u, v)$
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ $\texttt{prev}(v) = u$
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ $\texttt{decreasekey}(H, v)$
โ€ƒโ€ƒโ€ƒโ€ƒend if
โ€ƒโ€ƒend for
end while

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ Explored ์˜์—ญ์—์„œ Frontier ์˜์—ญ์„ ํƒ์ƒ‰ํ•˜๊ณ  ํ™•์žฅํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด, shortest path๋กœ ์•ˆ๋‚ดํ•˜๋Š” <shortest path tree>๋ฅผ ๊ตฌ์ถ•ํ•  ๋•Œ, ์ด๋ฏธ ์™„์„ฑ๋œ shortest path tree์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค์ธ Frontier๋ฅผ ์‚ดํŽด๋ณด๋ฉฐ, SPT๋ฅผ ํ™•์žฅํ•œ๋‹ค๋Š” ๋ง์ด๋‹ค. ์ด๋•Œ, SPT growing rule์€ ํ˜„์žฌ์˜ heap์—์„œ ๊ฐ€์žฅ ์ž‘์€ cost๋ฅผ ๊ฐ€์ง€๋Š” ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ™•์žฅ์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค!

์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•˜์—ฌ ์•ž์—์„œ ์†Œ๊ฐœํ•œ ๋ฐฉ์‹์˜ ๋™์น˜์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ œ์‹œํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

Algorithm: dijkstra($G$, $\ell$, $s$)


// initialization
for all $u \in V$ do
โ€ƒโ€ƒ $\texttt{dist}(u) = \infty$
โ€ƒโ€ƒ $\texttt{prev}(u) = \texttt{nil}$
end for

// construct heap
$\texttt{dist}(s) = 0$
$R = \left\{ \right\}$ (the โ€œknown regionโ€)

// explore frontier!
while $R \ne V$ do
โ€ƒโ€ƒ Pick the node $v \notin R$ with smallest $\texttt{dist}$ value
โ€ƒโ€ƒ Add $v$ to $R$

โ€ƒโ€ƒ for all edges $(v, z) \in E$
โ€ƒโ€ƒโ€ƒโ€ƒ if $\texttt{dist}(z) > \texttt{dist}(v) + \ell(v, z)$ then
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ $\texttt{dist}(z) = \texttt{dist}(v) + \ell(v, z)$
โ€ƒโ€ƒโ€ƒโ€ƒend if
โ€ƒโ€ƒend for
end while


Complexity

๋‹น์—ฐํ•˜๊ฒŒ๋„ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ BFS๋ณด๋‹ค ๋” ๋Š๋ฆฌ๋‹ค. ์™œ๋ƒํ•˜๋ฉด, Priority Queue์˜ ์—ฐ์‚ฐ์ด ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (PQ์˜ ์—ฐ์‚ฐ์€ ๊ธฐ๋ณธ $\log_2 n$๋Š” ๋จน๋Š”๋‹ค.)

๋‹ค์ต์ŠคํŠธ๋ผ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ธฐ ๋•Œ๋ฌธ์—, ๋…ธ๋“œ ๊ฐฏ์ˆ˜์ธ $\left| V \right|$๋ฒˆ ๋งŒํผ PQ์— $\texttt{insertion}$ ํ•˜๊ฒŒ ๋œ๋‹ค.

๋˜, PQ๋ฅผ ๋น„์šฐ๊ธฐ ์œ„ํ•ด PQ์— ๋„ฃ์€ ๋…ธ๋“œ๋ฅผ $\texttt{deletemin}$์œผ๋กœ ํ•œ๋ฒˆ์”ฉ ๋ชจ๋‘ ๊บผ๋‚ด์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— $\left| V \right|$๋ฒˆ ๋งŒํผ PQ์— $\texttt{deletemin}$ ํ•˜๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ฐ fronter point์—์„œ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  edge๋ฅผ ์‚ดํŽด๋ณด๊ธฐ ๋•Œ๋ฌธ์— $\left| E \right|$๋ฒˆ ๋งŒํผ $\texttt{decreasekey}$ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.


๋งŒ์•ฝ Binary Heap๋กœ ๊ตฌํ˜„๋œ PQ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด,

  • $\texttt{deletemin}$์— $O(\log \left| V \right|)$ ๋งŒํผ์˜ ๋น„์šฉ์ด ๋“ค๊ณ ,
  • $\texttt{insert}$์™€ $\texttt{decreasekey}$๋Š” $O(\log \left| V \right|)$ ๋งŒํผ์˜ ๋น„์šฉ์ด ๋“ ๋‹ค.

๋”ฐ๋ผ์„œ, ์ „์ฒด ๋น„์šฉ์€

\[O(V \log V) + O((V + E) \log V) = O((V+E) \log V)\]


PQ ๋˜๋Š” Heap์„ Binary Heap์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๋ฐฉ์‹๋“ค, ์˜ˆ๋ฅผ ๋“ค๋ฉด, <$d$-ary heap>, <Fibonacci heap> ๋“ฑ์œผ๋กœ ๊ตฌํ˜„ํ•ด ์‹œ๊ฐ„์„ ๋” ์ค„์ผ ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

* ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์œ„ํ‚คํ”ผ๋””์•„์˜ ํ•ด๋‹น ํ•ญ๋ชฉ์„ ์ฐธ๊ณ  ๐Ÿ‘‰ link


์ถ”๊ฐ€ ํ•ด์„ค

โ€ป Note: Dijkstraโ€™s Algorithm์„ <UCS; Universal Cost Search>๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค.

Q. UCS๋Š” ์ •๋ง minimum cost path๋ฅผ ๋ณด์žฅํ•˜๋Š”๊ฐ€?

"When a state $s$ is popped from the frontier and moved to explored, its priority is $\texttt{PastCost}(s)$, the minimum cost to $s$."
โ†’ ์ฆ‰, PQ์—์„œ $\texttt{pop}$๋˜๋Š” ๋…€์„์€ ๊ทธ๋•Œ์˜ $\texttt{PastCost}(s)$ minimum cost์ž„์„ ๋ณด์žฅํ•œ๋‹ค.

A. (๊ท€๋ฅ˜๋ฒ•) $s$๊ฐ€ PQ์—์„œ $\texttt{pop}$๋ ๋•Œ, ๊ทธ๋•Œ์˜ $\texttt{PastCost}(s)$๊ฐ€ minimum cost๊ฐ€ ์•„๋‹ˆ๋ผ๊ณ  ๊ฐ€์ •ํ•˜์ž. ์ด๊ฒƒ์€ $s$ ์ดํ›„์— $\texttt{pop}$๋˜๋Š” $u$๋ผ๋Š” ์–ด๋–ค ๋…ธ๋“œ๊ฐ€ ์žˆ๊ณ , $u \rightarrow s$๋กœ ๊ฐ€๋Š” path๊ฐ€ minimum cost๋ฅผ ๊ฐ€์ง์„ ์˜๋ฏธํ•œ๋‹ค.

ํ•˜์ง€๋งŒ, PQ๋Š” $\texttt{PastCost}(\cdot)$์ด ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ดํ›„์— $\texttt{pop}$๋˜๋Š” $u$์˜ $\texttt{PastCost}(u)$๋Š” $\texttt{PastCost}(s)$๋ณด๋‹ค ํด ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์€ $\texttt{PastCost}(u) + \ell(u, s) < \texttt{PastCost}(s)$๋ผ๋Š” $u$์˜ ์กด์žฌ์™€ ๋ชจ์ˆœ๋œ๋‹ค. ๊ทธ๋ž˜์„œ $u$๋ฅผ ๊ฑฐ์ณ $s$๋กœ ๊ฐ€๋Š” path๋Š” ์ ˆ๋Œ€ minimum cost path๊ฐ€ ๋  ์ˆ˜ ์—†๋‹ค.

๋”ฐ๋ผ์„œ $s$๊ฐ€ $\texttt{pop}$๋œ ๋•Œ์˜ $\texttt{PastCost}(s)$๋ณด๋‹ค ์ž‘์€ minimum cost path๋Š” ์กด์žฌํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ $s$๊ฐ€ $\texttt{pop}$๋  ๋•Œ์˜ $\texttt{PastCost}(s)$๊ฐ€ minimum cost์ด๋‹ค.


Q. ์™œ UCS๋Š” negative cost๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๊ฐ€?

A. ๋งŒ์•ฝ negative cost๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, PQ๋ฅผ ์ด์šฉํ•ด min-cost tree๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์—์„œ ์ด๋ฏธ ํƒ์ƒ‰์„ ์™„๋ฃŒํ•œ Explored ๋…ธ๋“œ์˜ min-cost๊ฐ€ ๋ฐ”๋€” ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ ๊ตฌ์ถ•ํ•œ min-cost tree๋ฅผ ๋ฌด๋„ˆ๋œจ๋ฆฌ๊ฒŒ ๋œ๋‹ค. ์ด๊ฒƒ์€ ์•ž์„  ๋ช…์ œ์—์„œ ์–ธ๊ธ‰ํ•œ โ€œ์ดํ›„์— $\texttt{pop}$๋˜๋Š” $u$์˜ $\texttt{PastCost}(u)$๋Š” $\texttt{PastCost}(s)$๋ณด๋‹ค ํด ๊ฒƒ์ด๋‹ค.โ€๋ผ๋Š” ๋ช…์ œ๋ฅผ ์œ„๋ฐ˜ํ•˜๋Š” ๊ฒƒ์ด๊ณ , ๋”์ด์ƒ UCS์˜ correctness๋ฅผ ๋ณด์žฅํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋ง์ด ๋œ๋‹ค.


<๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜>์˜ ๋‹จ์ ์€ Negative Edge๋ฅผ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด์–ด์ง€๋Š” ํฌ์ŠคํŠธ์—์„œ๋Š” Negative Edge๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ <Bellman-Ford Algorithm>์— ๋Œ€ํ•ด ์‚ดํŽด๋ณธ๋‹ค.

๐Ÿ‘‰ Bellman-Ford Algorithm

Categories:

Updated: