์ด ํฌ์ŠคํŠธ๋Š” ์œ„ํ‚คํ”ผ๋””์•„์˜ Heap(data structure)์— ์†Œ๊ฐœ๋œ Heap์˜ ๊ตฌํ˜„๋“ค์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์ง€์ ๊ณผ ์กฐ์–ธ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค ใ…Žใ…Ž

24 minute read

์ด ํฌ์ŠคํŠธ๋Š” ์œ„ํ‚คํ”ผ๋””์•„์˜ Heap(data structure)์— ์†Œ๊ฐœ๋œ Heap์˜ ๊ตฌํ˜„๋“ค์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์ง€์ ๊ณผ ์กฐ์–ธ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค ใ…Žใ…Ž

์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” <Heap>์˜ ๊ตฌํ˜„ ๋ฐฉ์‹์„ ์‚ดํŽด๋ณผ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ๊ตฌํ˜„ ์ฝ”๋“œ๋Š” ์ถ”ํ›„์— ๋ณ„๋„์˜ ํฌ์ŠคํŠธ์—์„œ Heap์˜ ์ข…๋ฅ˜๋ณ„๋กœ ๋‹ค๋ฃฐ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค! ๐Ÿ˜

<Binomial Heap>์—์„œ๋ถ€ํ„ฐ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์œ ๋„ํ•˜๊ธฐ ์œ„ํ•ด amortized analysis๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. โ€œAmortized Analysisโ€ ํฌ์ŠคํŠธ๋ฅผ ๋จผ์ € ์ฝ๊ณ  ์˜ค๊ธธ ๊ถŒํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜‰


<Priority Queue>์˜ ๊ตฌํ˜„์ธ <Heap>์€ ๋งŽ์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ํ•ต์‹ฌ์ ์ธ ์—ญํ• ์„ ํ•œ๋‹ค. ๋ณดํ†ต Binary Tree๋กœ ๊ตฌํ˜„ํ•˜๋Š” <Binary Heap>์„ ๋งŽ์ด ์•Œํ…Œ์ง€๋งŒ, ์ •๊ทœ ์ˆ˜์—…์ด๋‚˜ ์œ„ํ‚คํ”ผ๋””์•„์˜ ๋ฌธ์„œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด <Binary Heap> ์™ธ์—๋„ ์—ฌ๋Ÿฌ ํ˜•ํƒœ์˜ <Heap>์— ๋Œ€ํ•œ ๊ตฌํ˜„์ด ์กด์žฌํ•œ๋‹ค!! ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„  ์ด <Heap>์˜ ๊ตฌํ˜„์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณผ ์˜ˆ์ •์ด๋‹ค. ๐Ÿ˜Ž


Unodered Array & Ordered Array

min ๋˜๋Š” max์— ๋นˆ๋ฒˆํžˆ ์ ‘๊ทผํ•˜๋Š” priority queue๋ฅผ ๋ฐฐ์—ด๋กœ naive ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ •๋ง ์•„๋ฌด๋Ÿฐ ํ…Œํฌ๋‹‰์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฟผ๋ฆฌ๊ฐ€ ๋“ค์–ด์˜ฌ ๋•Œ๋งˆ๋‹ค linear search ๋˜๋Š” binary search๋ฅผ ํ†ตํ•ด ์ฟผ๋ฆฌ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.


Unordered Array.

  • $\texttt{getMin}$: $O(N)$
    • ์„ ํ˜• ํƒ์ƒ‰์œผ๋กœ min ์ฐพ์•„์„œ ๋ฆฌํ„ด
  • $\texttt{insert}$: $O(1)$
    • ๊ทธ๋ƒฅ ๋ฐฐ์—ด์˜ ๋งจ ๋’ค์— ๋ถ™์ด๋ฉด OK
  • $\texttt{deleteMin}$: $O(N)$
    • ์„ ํ˜• ํƒ์ƒ‰์œผ๋กœ min ์ฐพ์Œ $O(N)$
    • ๋ฐฐ์—ด์ด๋ฏ€๋กœ ์‚ญ์ œ์— $O(N)$ ์†Œ์š”


Ordered Array.

์›์†Œ๊ฐ€ ํ•˜๋‚˜ ์ถ”๊ฐ€๋  ๋•Œ๋งˆ๋‹ค ์ •๋ ฌํ•ด Ordered ๊ตฌ์กฐ๋ฅผ ์œ ์ง€. ์ด๋•Œ, min-heap์ด๋ผ๋ฉด, min์ด ๋ฐฐ์—ด ๋งจ ๋์— ์žˆ๋Š” ๊ตฌ์กฐ์—ฌ์•ผ ํ•œ๋‹ค. // ๊ทธ ์ด์œ ๋Š” min์ด ๋ฐฐ์—ด ๋งจ ์•ž์— ์žˆ๊ฒŒ ๋˜๋ฉด, $\texttt{deleteMin}$์—์„œ ์†ํ•ด์ž„. ์ง์ ‘ ๋น„๊ตํ•ด๋ณด๋ฉด ๊ธˆ๋ฐฉ ์ดํ•ด๊ฐ€ ๋œ๋‹ค.

  • $\texttt{getMin}$: $O(1)$
    • ๋งจ ๋’ค์— ์žˆ๋Š” min ์›์†Œ๋ฅผ ๋ฆฌํ„ด
  • $\texttt{insert}$: $O(N)$
    • ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ์ž์‹ ์˜ ์œ„์น˜๋ฅผ ์ฐพ์Œ $O(\log N)$.
    • ๋ฐฐ์—ด์ด๋ฏ€๋กœ ์‚ฝ์ž…์— $O(N)$ ์†Œ์š”.
  • $\texttt{deleteMin}$: $O(1)$
    • ๋งจ ๋’ค์— ์žˆ๋Š” min ์›์†Œ๋ฅผ ์‚ญ์ œ. ๋งจ ๋’ค์˜ ์›์†Œ๋ฅผ ์—†์• ๋ฏ€๋กœ, $O(1)$

Binary Heap

์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐฐ์šฐ๋Š” <Heap> ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค. <์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ; Complete Binary Tree>๋ผ๋Š” ํŠธ๋ฆฌ ๊ตฌ์กฐ๋กœ ๊ตฌํ˜„๋œ๋‹ค. ์‚ฌ์‹ค ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ๋ผ๊ณ  ๋ณ„๊ฒŒ ์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„๋œ Binary Tree์—์„œ ๋ถ€๋ชจ-์ž์‹ ์‚ฌ์ด์˜ ๊ด€๊ณ„๊ฐ€

  • ๋ถ€๋ชจ โ†’ ์ž์‹: $k$ โ†’ ($2k$ & $2k+1$)
  • ์ž์‹ โ†’ ๋ถ€๋ชจ: $k$ โ†’ $k/2$

์ธ ๊ตฌ์กฐ์ผ ๋ฟ์ด๋‹ค. ๋‹ค๋งŒ, ์ด ๊ตฌ์กฐ์ผ ๋•Œ, <heapify>๋ผ๋Š” <Heap>์˜ ์—ฐ์‚ฐ์„ $O(\log N)$์˜ ์‹œ๊ฐ„์œผ๋กœ ํšจ์œจ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— <Heap>์˜ ๊ตฌํ˜„์œผ๋กœ ์ž์ฃผ ์†Œ๊ฐœ๋œ๋‹ค. ๋ฌผ๋ก  ์ด๋Ÿฐ ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ , ํ‰๋ฒ”ํ•œ ํŠธ๋ฆฌ ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ•ด๋„ ๋™์ผํ•œ ํšจ์œจ์„ ๋ณด์ธ๋‹ค.

Heap-Order.
๋ถ€๋ชจ ๋…ธ๋“œ์˜ ํ‚ค๊ฐ€ ์ž์‹ ๋…ธ๋“œ์˜ ํ‚ค๋ณด๋‹ค ํ•ญ์ƒ ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

<Binary Heap>์—์„œ์˜ $\texttt{insert}$์™€ $\texttt{deleteMin}$๋Š” ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ๋ง๋‹จ ๋…ธ๋“œ๊นŒ์ง€ ๋˜๋Š” ๊ทธ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ $\texttt{swim}$ & $\texttt{sink}$ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

$\texttt{swim}$์€ ๋ถ€๋ชจ ๋…ธ๋“œ๋ณด๋‹ค ํฐ key๋ฅผ ๊ฐ€์ง„ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ๋ถ€๋ชจ ๋…ธ๋“œ์™€ swap ๋˜์–ด ๋ฃจํŠธ ๋…ธ๋“œ ์ชฝ์œผ๋กœ ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

$\texttt{sink}$๋Š” ์ž์‹ ๋…ธ๋“œ๋ณด๋‹ค ์ž‘์€ key๋ฅผ ๊ฐ€์ง„ ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์ž์‹ ๋…ธ๋“œ์™€ swap ๋˜์–ด ๋ง๋‹จ ๋…ธ๋“œ ์ชฝ์œผ๋กœ ๋‚ด๋ ค๊ฐ€๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ <Binary Heap>์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ์†Œ๊ฐœ๋งŒ ํ•˜๊ณ , ๋™์ž‘๊ณผ ๊ตฌํ˜„์„ ์ž์„ธํžˆ ๋…ผํ•˜์ง€๋Š” ์•Š๊ฒ ๋‹ค. ์ถ”ํ›„์— <Binary Heap>์˜ ๊ตฌํ˜„ ์ฝ”๋“œ์—์„œ ์ข€๋” ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ณ , ์ง€๊ธˆ์€ Time Complexity ์œ„์ฃผ๋กœ ์‚ดํŽด๋ณด๊ฒ ๋‹ค.

  • $\texttt{getMin}$: $O(1)$
    • ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๋ฆฌํ„ด
  • $\texttt{insert}$: $O(\log N)$
    • ์ตœ์•…์˜ ๊ฒฝ์šฐ, <Heap>์˜ ๊นŠ์ด ๋งŒํผ $\texttt{swim}$ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰.
    • Complete Binary Tree์ด๊ธฐ ๋•Œ๋ฌธ์— ๊นŠ์ด $D$๋Š” $O(\log N)$์ด๋‹ค โ†’ $O(\log N)$
  • $\texttt{deleteMin}$: $O(\log N)$
    • ๋งจ ๋’ค ์›์†Œ๋ฅผ ๋ฃจํŠธ๋กœ swap ํ›„, ๋ฃจํŠธ ๋…ธ๋“œ ์˜€๋˜ ๊ฒƒ์„ ์‚ญ์ œ
    • ์ตœ์•…์˜ ๊ฒฝ์šฐ, <Heap>์˜ ๊นŠ์ด ๋งŒํผ ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ๋ถ€ํ„ฐ $\texttt{sink}$ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ โ†’ $O(\log N)$

d-ary Heap

<d-ary Heap>์€ <Binary Heap>์˜ generalization์ด๋‹ค. <Binary Heap>์€ ๊ฐ ๋…ธ๋“œ๊ฐ€ 2๊ฐœ์˜ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๊ฐ€์กŒ๋‹ค๋ฉด, <d-ary Heap>์€ ๊ฐ ๋…ธ๋“œ๊ฐ€ d๊ฐœ์˜ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ง€๋Š” Complete Tree์ด๋‹ค. ๋”ฐ๋ผ์„œ, <Binary Heap>์€ $d=2$์ธ ํŠน์ˆ˜ํ•œ ๊ฒฝ์šฐ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

<d-ary Heap>์˜ ๋™์ž‘์€ <Binary Heap>์˜ ๋™์ž‘๊ณผ ๋™์ผํ•˜๋‹ค. ๊ฐ ๋…ธ๋“œ๋Š” Heap Order๋ฅผ ๋งŒ์กฑํ•ด์•ผ ํ•˜๋ฉฐ, Heapify๋ฅผ ํ†ตํ•ด Heap์„ ๊ตฌ์„ฑํ•œ๋‹ค. ๋‹ค๋งŒ, ์ž์‹ ๋…ธ๋“œ์˜ ์ˆ˜๊ฐ€ ๋งŽ์•„์ ธ, Heap์˜ ๊นŠ์ด $D$๊ฐ€ $\log_d N$์œผ๋กœ ์–•์•„ ์กŒ๋‹ค. ์ด์— ๋”ฐ๋ผ ๊ฐ ์—ฐ์‚ฐ์˜ Time Complexity๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์œ ๋„๋œ๋‹ค.

  • $\texttt{getMin}$: $O(1)$
    • ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๋ฆฌํ„ด
  • $\texttt{insert}$: $O(\log_d N)$
  • $\texttt{deleteMin}$: $O(d \log_d N)$

* $\texttt{deleteMin}$ in d-ary Heap

๋จผ์ € $d=2$์ธ Binary Heap์˜ ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด์ž. ๋งŒ์•ฝ ํ˜„์žฌ min-Heap์ด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๋ฉด,

   3       2       1
  / \  โ†’  / \  โ†’  / \
  2 1     3 1     3 2

์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ตœ๋Œ€ 2๋ฒˆ $\texttt{Heapify}$๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

์ด๋ฒˆ์—๋Š” $d=3$์ธ ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด์ž. ๋งŒ์•ฝ ํ˜„์žฌ min-Heap์ด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๋ฉด,

    4         3         2         1
  / | \  โ†’  / | \  โ†’  / | \  โ†’  / | \
 3  2  1   4  2  1   4  3  1   4  3  2

์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ตœ๋Œ€ 3๋ฒˆ์˜ $\texttt{Heapify}$๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

๊ฐ $\texttt{Heapify}$๋Š” $O(\log_d N)$์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๋ฏ€๋กœ, $\texttt{deleteMin}$์€ $O(d \log_d N)$์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค.

ps) $d=3$์ธ d-ary Heap์„ <Ternary Heap>์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค.

๐Ÿ’ฅ Wikipedia์— ๊ธฐ์ˆ ๋œ ๋‚ด์šฉ์— ๋”ฐ๋ฅด๋ฉด, ์‹ค์ „์—์„œ๋Š” <4-ary Heap>์ด $\texttt{deleteMin}$ ์‹œ๊ฐ„๋ณต์žก๋„์—์„œ ์—ด์„ธ์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ , <Binary Heap> ๋ณด๋‹ค ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚ธ๋‹ค๊ณ  ํ•œ๋‹ค. ๊ทธ ์ด์œ ๋Š” <4-ary Heap>์ด <Binary Heap>๋ณด๋‹ค Cache์—์„œ ๋” ์ด๋“์„ ๋ณด๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ํ•œ๋‹ค ๐Ÿ˜


Binomial Heap

<Binomial Heap>์€ ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ Heap์„ ๋ณ‘ํ•ฉ(merge)ํ•ด ์ƒˆ๋กœ์šด Heap์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์— ํŠนํ™”๋œ Heap ๊ตฌ์กฐ๋‹ค.

<Binomial Heap>์€ <Binomial Tree>๋ผ๋Š” ํŠน๋ณ„ํ•œ ํ˜•ํƒœ์˜ ํŠธ๋ฆฌ์˜ ์ง‘ํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ๋จผ์ € <Binomial Tree>์— ๋Œ€ํ•ด ์‚ดํŽด๋ณธ ํ›„, <Binomial Heap>์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๊ฒ ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŠธ์˜ ๋‚ด์šฉ์€ ์•„๋ž˜ ์œ ํŠœ๋ธŒ ์˜์ƒ์˜ ๋‚ด์šฉ์„ ์ ์ ˆํžˆ ์ •๋ฆฌํ•œ ๊ฒƒ์ž„์„ ๋ฏธ๋ฆฌ ๋ฐํžŒ๋‹ค.

Binomial Tree

<Binomial Tree>๋Š” degree๊ฐ€ ๊ฐ™์€ ๋‘ <Binomial Tree>๋ฅผ ๋ณ‘ํ•ฉ(merge)ํ•จ์œผ๋กœ์จ ๊ตฌ์ถ•ํ•œ๋‹ค. ๋ฐฉ๋ฒ•์€ ๊ทธ๋ ‡๊ฒŒ ์–ด๋ ต์ง€ ์•Š๊ณ , ์•„๋ž˜์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ๋ฐ”๋กœ ์ดํ•ด ๋  ๊ฒƒ์ด๋‹ค. ๐Ÿ˜Š

(deg 0)   (deg 0)       (deg 1)       |    (deg 1)   (deg 1)       (deg 2)
   4    +    10     โ†’       4         |       1    +    7     โ†’       1
                            |         |       |         |           / |
                            10        |       9         15         7  9
                                      |                            |
                                      |                           15

<Binomial Tree>๋Š” ์•„๋ž˜์˜ 4๊ฐ€์ง€ ์„ฑ์งˆ์„ ๋งŒ์กฑํ•œ๋‹ค.

1. degree $k$์˜ <Binomial Tree>์˜ ๋ฃจํŠธ ๋…ธ๋“œ๋Š” ์ •ํ™•ํžˆ $k$๊ฐœ์˜ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๊ฐ–๋Š”๋‹ค.

2. degree $k$์˜ <Binomial Tree>๋Š” ์ •ํ™•ํžˆ $2^k$๊ฐœ์˜ ๋…ธ๋“œ์™€ ๊นŠ์ด $k$๋ฅผ ๊ฐ–๋Š”๋‹ค.

3. <Binomial Tree>์˜ ๋ฃจํŠธ ๋…ธ๋“œ๋Š” degree $k-1$, $k-2$, โ€ฆ, $0$์˜ Binomial Tree๋ฅผ ์ž์‹์œผ๋กœ ๊ฐ–๋Š”๋‹ค.

4. degree $k$์˜ <Binomial Tree>์—์„œ depth $d$๋ฅผ ๊ฐ–๋Š” ๋…ธ๋“œ์˜ ์ˆ˜๋Š” $\displaystyle\binom{k}{d}$์ด๋‹ค. ์ด ์„ฑ์งˆ ๋•Œ๋ฌธ์— <Binomial Tree>๋ผ๋Š” ์ด๋ฆ„์ด ๋ถ™์—ˆ๋‹ค ๐Ÿ˜Ž

Binomial Heap

๋‹ค์‹œ <Binomial Heap>์œผ๋กœ ๋Œ์•„์˜ค์ž. <Binomial Heap>์€ ์•„๋ž˜์˜ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ชจ์ธ <Binomial Tree>์˜ ์ง‘ํ•ฉ์ด๋‹ค.

1. Heap์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐ Binomial Tree๋Š” Heap Order๋ฅผ ๋”ฐ๋ฅธ๋‹ค.

2. degree 0์„ ํฌํ•จํ•ด ๊ฐ order๋ณ„๋กœ 0๊ฐœ ๋˜๋Š” 1๊ฐœ, ์ฆ‰ ๊ฐ order๋ณ„ ์ตœ๋Œ€ 1๊ฐœ์˜ ์ดํ•ญ ํŠธ๋ฆฌ๊ฐ€ Heap์— ์กด์žฌํ•œ๋‹ค.

2๋ฒˆ์žฌ ์กฐ๊ฑด์€ ์ „์ฒด $n$๊ฐœ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•  ๋•Œ, <Binomial Heap>์ด ์ตœ๋Œ€ $(1 + \log n)$๊ฐœ์˜ ์ดํ•ญ ํŠธ๋ฆฌ๋กœ ๊ตฌ์„ฑ๋จ์„ ๋งํ•œ๋‹ค. ๊ทธ ์ด์œ ๋Š” degree $k$์ธ Binomial Tree๊ฐ€ $2^k$ ๋งŒํผ์˜ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค! ๋˜ํ•œ, Heap์— ์กด์žฌํ•˜๋Š” ๊ฐ Binomial Tree์˜ degree๋Š” ์œ ์ผํ•˜๊ฒŒ ๊ฒฐ์ •๋œ๋‹ค. ์ „์ฒด ๋…ธ๋“œ์˜ ์ˆ˜ $n$์„ ์ด์ง„์ˆ˜๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋ฉด, ๊ฐ digit์€ Heap์— ์กด์žฌํ•˜๋Š” Binomial Tree์˜ ๊ฐฏ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, ์ „์ฒด ๋…ธ๋“œ ์ˆ˜ $n$์— ๋Œ€ํ•ด Binomial Heap์˜ ๊ตฌ์„ฑ์€ ์œ ์ผํ•˜๋‹ค! ๐Ÿ’ฅ


Merge.

Binomial Heap์€ $\texttt{Merge}$๋ผ๋Š” ์—ฐ์‚ฐ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ Heap๊ณผ ๋‹ฌ๋ฆฌ ๋น ๋ฅธ Merge๋ฅผ ์ง€์›ํ•œ๋‹ค๋Š” ๊ฒŒ ์žฅ์ ์ด๋‹ค.

Image from Wikipedia

์œ„์™€ ๊ฐ™์ด ๋‘ Binomial Heap์„ ๋ณ‘ํ•ฉํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด์ž. ์ด๋•Œ, ๋‘ Heap์˜ ๋…ธ๋“œ ์ˆ˜๊ฐ€ $N$, $M$์ด๋ผ๋ฉด, ๋‘ Heap์— ์กด์žฌํ•˜๋Š” Tree์˜ ์ˆ˜๋Š” $1 + \log N$, $1 + \log M$์ด ๋œ๋‹ค.

์ด๋•Œ, ๋‘ Binomial Heap์„ ๋ณ‘ํ•ฉํ•˜๋Š” ๊ฒƒ์€ ๊ฐ™์€ degree๋ฅผ ๊ฐ–๋Š” Binomial Tree๋ฅผ ๋ณ‘ํ•ฉํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์ด Tree ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์€ $O(1)$์— ์ˆ˜ํ–‰๋˜๊ณ , ๋˜ ์ด ๊ณผ์ •์ด ์ตœ๋Œ€ Tree์˜ ๊ฐฏ์ˆ˜ ๋งŒํผ ํ•„์š”ํ•˜๋ฏ€๋กœ ์ด $O(\log N + \log M)$ ๋งŒํผ์˜ Time Complexity๋ฅผ ๊ฐ–๋Š”๋‹ค.

์•ž์—์„œ๋„ ์–ธ๊ธ‰ํ–ˆ๋“ฏ Binomial Heap์€ ๊ทธ ๊ตฌ์„ฑ์— ๋”ฐ๋ผ ์ด์ง„์ˆ˜๋กœ ์ธ์ฝ”๋”ฉ ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ, Heap Merge ๊ณผ์ •์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ด์ง„์ˆ˜ ๋ง์…ˆ์˜ ๊ด€์ ์—์„œ ํ•ด์„ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. $N$-์ž๋ฆฟ์ˆ˜ ์ด์ง„์ˆ˜์™€ $M$-์ž๋ฆฟ์ˆ˜ ์ด์ง„์ˆ˜์˜ ๋ง์…ˆ์€ $O(N + M)$์˜ Time Complexity๋ฅผ ๊ฐ–๋Š” ๊ฑธ ์ƒ๊ฐํ•˜๋ฉด, ๋‘ ๊ณผ์ •์ด ์ •๋ง ์œ ์‚ฌํ•จ์„ ๊นจ๋‹ฌ์„ ์ˆ˜ ์žˆ๋‹ค ๐Ÿคฉ


Insert.

Binomial Heap์— ์ƒˆ๋กœ์šด ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ๋‹จ์ˆœํžˆ degree 0์˜ Binomial Tree๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๊ทธ๋ž˜์„œ ์•ž์—์„œ ์–ธ๊ธ‰ํ•œ $\texttt{merge}$์˜ ์ƒํ™ฉ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, Time Complexity๋Š” $O(\log N)$์ด๋‹ค.


getMin.

Binomial Heap์„ ์ด๋ฃจ๋Š” Tree์˜ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋œ๋‹ค. ๋”ฐ๋ผ์„œ, Time Complexity๋Š” $O(\log N)$. ๋งŒ์•ฝ Heap์— Min-elt๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, $O(1)$์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. ๋ณดํ†ต์€ $O(1)$๋กœ ๊ตฌํ˜„ํ•œ๋‹ค ๐Ÿ˜‰


deleteMin.

๋จผ์ € $\texttt{getMin}$์œผ๋กœ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•  Binomial Tree๋ฅผ ํŠน์ •ํ•œ๋‹ค. Tree์˜ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด, ํ•ด๋‹น Tree์˜ ์ž์‹ ํŠธ๋ฆฌ๋“ค์ด ๋‚จ๊ฒŒ ๋œ๋‹ค. ๋งŒ์•ฝ $k$๋ฒˆ์งธ binomial tree์˜ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ–ˆ๋‹ค๋ฉด, $k$๊ฐœ์˜ children binomial tree๋ฅผ ์–ป๊ฒŒ ๋œ๋‹ค. ์ƒˆ๋กœ ์ƒ๊ธด $k$๊ฐœ์˜ Children Binomial Tree๋“ค์„ ๋ฐ”ํƒ•์œผ๋กœ ๋‹ค์‹œ $\texttt{merge}$ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ด๋•Œ, BIN Heap์— ์กด์žฌํ•˜๋Š” $1 + \log N$๊ฐœ์˜ ํŠธ๋ฆฌ ์ค‘ ๋งˆ์ง€๋ง‰ ๋ฒˆ์งธ ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ๋…ธ๋“œ๊ฐ€ ์ œ๊ฑฐ๋˜์—ˆ๋‹ค๋ฉด, $log N$ ๋งŒํผ์˜ binomial tree ๋˜๋Š” $\log N$ ๋งŒํผ์˜ binomial tree๋ฅผ ๊ฐ–๋Š” BIN heap์ด ์ƒˆ๋กœ ์ƒ๊ธด๋‹ค. ๊ธฐ์กด์˜ BIN heap๊ณผ ์ƒˆ๋กœ ์ƒ๊ธด BIN heap์„ merge ํ•˜๋ฉด, Time Complexity๋Š” $O(\log N + \log N) = O(\log N)$์ด๋‹ค.


Image from Wikipedia

์ผ๋ฐ˜์ ์œผ๋กœ Binomial Heap์€ Linked List๋กœ ๊ตฌํ˜„ํ•œ๋‹ค. ์ด๋•Œ, ์ผ๋ฐ˜์ ์ธ ํ˜•ํƒœ์˜ Linked List Tree์—์„œ sibling ์š”์†Œ๋ฅผ ์ถ”๊ฐ€๋กœ ์ €์žฅํ•ด ๊ด€๋ฆฌํ•œ๋‹ค. <Binomial Heap>์˜ ๊ตฌํ˜„์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ถ”ํ›„์— ๋ณ„๋„์˜ ํฌ์ŠคํŠธ์—์„œ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ๋‹ค ๐Ÿ˜Ž


Lazy-Binomial Heap

์ด๋ฒˆ ํฌ์ŠคํŠธ์˜ ๋‚ด์šฉ์€ ์•„๋ž˜ ์œ ํŠœ๋ธŒ ์˜์ƒ์˜ ๋‚ด์šฉ์„ ์ ์ ˆํžˆ ์ •๋ฆฌํ•œ ๊ฒƒ์ž„์„ ๋ฏธ๋ฆฌ ๋ฐํžŒ๋‹ค.

<Lazy-Binomial Heap>์€ ์•ž์—์„œ ์‚ดํŽด๋ณธ <Binomial Heap>๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Heap์˜ $\texttt{merge}$ ์—ฐ์‚ฐ์— ํŠนํ™”๋œ ์ž๋ฃŒ ๊ตฌ์กฐ๋‹ค! ์ „์ฒด์ ์ธ ๊ฐœ๋…์€ <Binomial Heap>๊ณผ ์œ ์‚ฌํ•˜๊ณ , $\texttt{marge}$์™€ $\texttt{extractMin}$ ์—ฐ์‚ฐ์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

์ผ๋‹จ <Lazy-Binomial Heap>์˜ ์•„์ด๋””์–ด๋Š” โ€œmerge lazily!โ€์ด๋‹ค. <Lazy-Binomial Heap>์˜ $\texttt{merge}$ ์—ฐ์‚ฐ์€ ๊ทธ๋ƒฅ ๋‘ BIN Heap์„ concatenate๋กœ ๋ถ™์ด๋Š” ๊ฒƒ์— ๋ถˆ๊ณผํ•˜๋‹ค. ๊ทธ๋ž˜์„œ $O(\log n + \log m)$์˜ ๋น„์šฉ์ด ๋“œ๋Š” <BIN Heap>๊ณผ ๋‹ฌ๋ฆฌ, <Lazy-BIN Heap>์—์„œ๋Š” $O(1)$์˜ ๋น„์šฉ์ด ๋“ ๋‹ค! ๐Ÿ˜ฒ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์›์†Œ๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•˜๋Š” $\texttt{insert}$ ์—ฐ์‚ฐ ์—ญ์‹œ $O(1)$์˜ ๋น„์šฉ์ด ๋“ ๋‹ค.

ํ•˜์ง€๋งŒ, ์œ„์™€ ๊ฐ™์ด $\texttt{merge}$๋ฅผ ์ˆ˜ํ–‰ํ•  ๊ฒฝ์šฐ, ์•ž์—์„œ ์‚ดํŽด๋ณธ <BIN Heap>์˜ ์•„๋ฆ„๋‹ต๊ณ  ์ข‹์€ ์„ฑ์งˆ์ด ๊นจ์ง€๊ฒŒ ๋œ๋‹ค ๐Ÿ˜ฅ <Lazy-BIN Heap>์—์„œ๋Š” BIN Tree์˜ degree๋Š” ๊ทœ์น™์„ฑ ์—†์ด ์ž์œ ๋กญ๊ฒŒ ๋ถ„ํฌ๋˜์–ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

์ด๋ฒˆ์—๋Š” <Lazy-BIN Heap>์˜ $\texttt{extractMin}$ ์—ฐ์‚ฐ์„ ์‚ดํŽด๋ณด์ž. ์ด ์—ฐ์‚ฐ์€ Heap์—์„œ ๊ฐ€์žฅ ์ž‘์€ ์›์†Œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. <Lazy-BIN Heap>์€ ์ด $\texttt{extractMin}$ ์—ฐ์‚ฐ์ด ์ˆ˜ํ–‰๋  ๋•Œ, $\texttt{consolidation}$(= ํ•ฉ๋ณ‘, ๋ณ‘ํ•ฉ)์„ ์ˆ˜ํ–‰ํ•˜์—ฌ <Lazy-BIN Heap>์—์„œ BIN Heap์˜ ๊ตฌ์กฐ๋ฅผ ๋‹ค์‹œ ๊ตฌ์ถ•ํ•œ๋‹ค.

์ด $\texttt{consolidation}$์ด ์ˆ˜ํ–‰๋˜๋Š” ๊ณผ์ •์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋ถ„์„ํ•ด๋ณด์ž. degree๊ฐ€ ์ž์œ ๋กญ๊ฒŒ ๋ถ„ํฌ๋˜์–ด ์žˆ๋Š” ์ƒํ™ฉ์—์„œ $\texttt{consolidation}$์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ฐ BIN Tree๋ฅผ degree ๋ณ„๋กœ ์ •๋ ฌํ•˜์—ฌ ์•ž์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ Tree๋ฅผ ํ•ฉ์น˜๋Š” ๊ฒƒ์ด๋‹ค. ์›๋ž˜๋Š” ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ, $O(t \log t)$ ($t$๋Š” Heap์— ์กด์žฌํ•˜๋Š” BIN Tree์˜ ์ˆ˜) ๋งŒํผ์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ •๋ ฌ ๊ณผ์ •์—์„œ <Bucket Sort>๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, BIN Tree ํƒ์ƒ‰์— $O(t)$, Bucket์˜ ์ˆ˜ $O(\log n)$ ๋งŒํผ BIN tree merge ๋น„์šฉ์ด ๋“ค์–ด $O(t + \log n)$์˜ ๋น„์šฉ์œผ๋กœ $\texttt{consolidation}$์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜์˜ ์˜์ƒ์„ ์ฐธ๊ณ ํ•˜์ž!

๐Ÿ‘‰ Jeff Zhang - Lazy Binomial Heap Intro Part 1 of 2

$\texttt{extractMin}$ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด์„œ๋Š” ๊ทธ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ โ€œamortized $O(\log n)$โ€๋ผ๊ณ  ํ•œ๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„์— <Potential Method>๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, Potential Method์— ์†Œ๊ฐœํ•˜๋Š” lecture note Lecture 20: Amortized Analysis์™€ Lazy-BIN Heap์„ Potential Method๋กœ ๋ถ„์„ํ•œ ์ด lecture note Lecture 3: Fibonacci Heaps๋ฅผ ์ž˜ ์ฝ์–ด๋ณด๋ฉด ๊ทธ๋Ÿญ์ €๋Ÿญ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค ๐Ÿคž

Operation Binomial Heap Lazy-Binomial Heap
$\texttt{insert}$ $O(\log n)$ $O(1)$
$\texttt{getMin}$ $O(1)$ $O(1)$
$\texttt{extractMin}$ $O(\log n)$ amortized $O(\log n)$
$\texttt{merge}$ $O(\log n)$ $O(1)$

Fibonacci Heap

์ด๋ฒˆ ํฌ์ŠคํŠธ์˜ ๋‚ด์šฉ์€ ์•„๋ž˜ ์œ ํŠœ๋ธŒ ์˜์ƒ์˜ ๋‚ด์šฉ์„ ์ ์ ˆํžˆ ์ •๋ฆฌํ•œ ๊ฒƒ์ž„์„ ๋ฏธ๋ฆฌ ๋ฐํžŒ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹ค๋ฃฐ Heap ๊ตฌ์กฐ๋Š” <Fibonacci Heap>์ด๋‹ค ๐Ÿ˜Ž ๊ฐœ์ธ์ ์œผ๋กœ ๋งˆ์ง€๋ง‰์— ์œ ๋„๋˜๋Š” ๋งŒํผ ๊ฐ€์žฅ ์–ด๋ ค์šด Heap ๊ตฌ์กฐ๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค ๐Ÿ˜ฑ

๋จผ์ € <Fibonacci Heap>์€ ์•ž์—์„œ ๋‹ค๋ฃฌ <Lazy-Binomial Heap>๋ณด๋‹ค ๋” Lazy ํ•œ ๋…€์„์ด๋‹ค! <Lazy-BIN Heap>๋ณด๋‹ค $\texttt{decreaseKey}$ ์—ฐ์‚ฐ์„ Lazy ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ด์„œ ๋น„์šฉ์„ ๋” ์ค„์ธ๋‹ค!

์‚ฌ์‹ค โ€œLazy decreaseKeyโ€ ์—ฐ์‚ฐ์˜ ์•„์ด๋””์–ด ์ž์ฒด๋Š” ๊ฐ„๋‹จํ•˜๋‹ค. $\texttt{decreaseKey}$๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ, heap-order๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ์ž˜๋ผ๋‚ธ๋‹ค๋Š” ๊ฒŒ ์ „๋ถ€๋‹ค.

<BIN Heap>์—์„œ $\texttt{decreaseKey}$ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด $\texttt{Heapify}$ ๋•Œ๋ฌธ์— ํŠธ๋ฆฌ์˜ ๋†’์ด ๋งŒํผ, ์ฆ‰ $O(\log n)$์˜ ๋น„์šฉ์ด ๋“ค์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ <Fibonacci Heap>์€ ๋Œ€์ƒ์ด ๋˜๋Š” BIN tree๋ฅผ ์ž˜๋ผ๋‚ด๊ธฐ ๋•Œ๋ฌธ์— $\texttt{Heapify}$ ์—ฐ์‚ฐ ์—†์ด $O(1)$์˜ ๋น„์šฉ๋งŒ ๋“ ๋‹ค.


์—ฌ๊ธฐ์„œ ์ž ๊น ์ง€๊ธˆ๊นŒ์ง€ <BIN Heap>์—์„œ ๊ฐœ์„ ๋œ ๋ถ€๋ถ„๋“ค์„ ์งš๊ณ  ๋„˜์–ด๊ฐ€์ž.

  • Merge: $O(\log n + \log m) \rightarrow O(1)$ (by Lazy-BIN Heap)
  • Insert: $O(\log n) \rightarrow O(1)$ (by Lazy-BIN Heap)
  • extractMin: $O(\log n) \rightarrow \text{amoritzed} \; O(\log n)$ (by Lazy-BIN Heap)
  • deleteKey: $O(\log n) \rightarrow O(1)$ (by Fibonacci Heap!) ๐Ÿ”ฅ

์œ„ ์—ฐ์‚ฐ๋“ค์— ๋Œ€ํ•œ ๋น„์šฉ์ด ์ค„์—ˆ์ง€๋งŒ, ๋˜ ์œ„ ์—ฐ์‚ฐ๋“ค์ด Heap ๊ตฌ์กฐ๋ฅผ ์—‰๋ง์œผ๋กœ ๋งŒ๋“œ๋Š” ์ฃผ๋ฒ”์ด๊ธฐ๋„ ํ•˜๋‹ค ๐Ÿคฆโ€โ™‚๏ธ ๊ทธ๋ฆฌ๊ณ  Fibo Heap์˜ ๋ณ€ํ™”๋œ $\texttt{deleteKey}$ ์—ฐ์‚ฐ์€ $\texttt{extractMin}$ ์—ฐ์‚ฐ์˜ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ๋ผ์นœ๋‹ค ๐Ÿ˜ฑ

๊ทธ.๋Ÿฌ.๋‚˜. ์•ˆ์‹ฌํ•˜๋ผ! Lazy decreaseKey๋ฅผ ๋„์ž…ํ•˜๋”๋ผ๋„ $\texttt{extractMin}$์˜ ๋น„์šฉ์€ ์—ฌ์ „ํžˆ โ€œamortized $O(\log n)$โ€์ด๋‹ค! ๐Ÿ˜ฒ


<Fibonacci Heap>์—์„œ ์ˆ˜ํ–‰๋˜๋Š” $\texttt{extractMin}$์„ ์ข€๋” ์‚ดํŽด๋ณด์ž. $\texttt{extractMin}$ ์ดํ›„ ์ˆ˜ํ–‰๋˜๋Š” $\texttt{consolidate}$ ์—ฐ์‚ฐ์€ Heap์„ ์ด๋ฃจ๋Š” ํŠธ๋ฆฌ๋ฅผ degree $k$์— ๋”ฐ๋ผ โ€œBucket Sortโ€๋กœ ์ •๋ ฌํ•˜์—ฌ ์ฐจ๋ก€๋กœ ์ƒˆ๋กœ์šด ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค.

ํ•˜์ง€๋งŒ, Lazy decreaseKey ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋”์ด์ƒ Heap์— ์กด์žฌํ•˜๋Š” Tree๋Š” BIN Tree์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด, BIN Tree๊ฐ€ ๋˜๋ ค๋ฉด degree $k$์ผ ๋•Œ, $2^k$ ๊ฐœ์˜ ๋…ธ๋“œ๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (Fibo Heap์—์„œ๋Š” $2^k$ ๋ณด๋‹ค ์ ์€ ์ˆ˜์˜ ๋…ธ๋“œ๊ฐ€ ํŠธ๋ฆฌ์— ๋‚จ๊ฒŒ ๋œ๋‹ค.)

์˜ˆ๋ฅผ ๋“ค์–ด ์™ผ์ชฝ์˜ BIN tree์—์„œ โ€˜10โ€™์˜ ํ‚ค๋ฅผ ๊ฐ€์ง„ ๋ง๋‹จ ๋…ธ๋“œ์˜ ํ‚ค๋ฅผ โ€˜3โ€™์œผ๋กœ ๋ฐ”๊พผ๋‹ค๋ฉด, ๊ทธ ๋…ธ๋“œ๋ฅผ BIN tree์—์„œ ๋ถ„๋ฆฌ(cut)ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ $\texttt{decreaseKey}$ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ด๊ฒƒ์€ $\texttt{decreaseKey}$ ์—ฐ์‚ฐ์˜ ๋Œ€์ƒ์ด ๋˜๋Š” BIN tree๊ฐ€ ๋”์ด์ƒ BIN tree์˜ ์„ฑ์งˆ์„ ๋งŒ์กฑํ•˜์ง€ ๋ชป ํ•˜๋„๋ก ๋งŒ๋“ ๋‹ค.

์ด๋Ÿฐ ๋ฌธ์ œ ๋•Œ๋ฌธ์— ํŠธ๋ฆฌ๋ฅผ degree $k$๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•ด $\texttt{consolidate}$ ๋‹จ๊ณ„์—์„œ Bucket Sort๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๋‹ค ๐Ÿ˜ฑ ๊ทธ๋ž˜์„œ <Fibonacci Heap>์—์„œ๋Š” ํŠธ๋ฆฌ์˜ degree๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒˆ๋กญ๊ฒŒ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

A tree has degree $k$, if its root has $k$ children.

Fibo Heap์—์„œ๋Š” ์œ„์˜ ์ •์˜๋ฅผ ์‚ฌ์šฉํ•ด Bucket Sort๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, ์—‰๋ง์ด ๋œ BIN Heap์„ ์ •๋ฆฌ(clean-up)ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜โ€ฆ


๊ทธ๋Ÿฌ๋‚˜! ์œ„์™€ ๊ฐ™์ด $\texttt{decreaseKey}$์™€ $\texttt{consolidate}$๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด, ์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋Š” $\texttt{decreaseKey}$์— ์˜ํ•ด Heap์ด degree 0์˜ ํŠธ๋ฆฌ $n$๊ฐœ๋กœ ์ด๋ค„์ง„ ํ›„์— $\texttt{consolidate}$๊ฐ€ ์ด๋ค„์งˆ ์ˆ˜ ์žˆ๋‹ค ๐Ÿคฆโ€โ™‚๏ธ ์ด ๊ฒฝ์šฐ, ๋น„์šฉ์€ $O(n)$์ด๋‹คโ€ฆ

BIN tree๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” <BIN Heap>๊ณผ <Lazy-BIN Heap>์€ ํŠธ๋ฆฌ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ $2^k$๋กœ exponential ํ•˜๊ฒŒ ์ฆ๊ฐ€ํ•ด์„œ $\text{amortized} \; O(\log n)$๋กœ $\texttt{consolidate}$ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ–ˆ๋‹ค.


์œ„์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด, Fibo Heap์—์„œ๋Š” $\texttt{decreaseKey}$ ์—ฐ์‚ฐ์— ์ƒˆ๋กœ์šด ๊ทœ์น™์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

Paraents can lose at most one children.

If a parent loses two children, we also cut the parent off from the grand-parent.

์œ„ ๊ทœ์น™์€ Heap์„ ์—‰์„ฑํ•˜๊ฒŒ๋‚˜๋งˆ โ€œlogarithmicโ€ํ•˜๋„๋ก ๋งŒ๋“ ๋‹ค. ์ด๊ฒƒ์— ๋Œ€ํ•œ ๊ตฌํ˜„์€ ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•˜๋‹ค. ๊ทธ๋ƒฅ ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ $\texttt{decreaseKey}$์— ์˜ํ•ด ์ž์‹ ๋…ธ๋“œ๋ฅผ ์žƒ์œผ๋ฉด ๊ทธ ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ โ€œ๋งˆํ‚นโ€ ํ•ด๋‘”๋‹ค. ์ดํ›„์— ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ๋˜ ํ•œ๋ฒˆ ์ž์‹ ๋…ธ๋“œ๋ฅผ ์žƒ๋Š”๋‹ค๋ฉด, ๊ทธ๋•Œ๋Š” ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ์กฐ๋ถ€๋ชจ ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๋ถ„๋ฆฌ์‹œํ‚จ๋‹ค! // ์˜์ƒ์—์„œ ์ž˜ ์„ค๋ช…ํ•˜๋‹ˆ ์ด ๋ถ€๋ถ„์€ ์˜์ƒ์„ ๋ณด์ž!

๊ธ€๋กœ๋Š” ๊ฐ์ด ์•ˆ ์žกํžˆ๋‹ˆ, ๊ทธ๋ฆผ์œผ๋กœ ์‚ดํŽด๋ณด์ž!

BIN tree๊ฐ€ child๋ฅผ ํ•˜๋‚˜ ์žƒ์–ด ๋ถ‰์€์ƒ‰์œผ๋กœ ๋งˆํ‚น ๋˜์—ˆ๋‹ค.

๋ถ‰์€์ƒ‰์œผ๋กœ ๋งˆํ‚น๋œ ๋…ธ๋“œ๊ฐ€ ๋˜ ํ•˜๋‚˜์˜ ์ž์‹ ๋…ธ๋“œ๋ฅผ ์žƒ์—ˆ๋‹ค!

๊ทธ๋Ÿฌ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๋ถ€๋ชจ ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๋ถ„๋ฆฌ์‹œํ‚จ๋‹ค!

์ถ”๊ฐ€๋œ ๊ทœ์น™์€ BIN Heap์˜ โ€œroot listโ€์˜ ํฌ๊ธฐ๋ฅผ approximately logarithmic ํ•˜๊ฒŒ ์œ ์ง€ํ•ด์ค€๋‹ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ <Fibonacci Heap>์˜ $\texttt{extractMin}$๋„ $\text{amortized} \; O(\log n)$์˜ ์‹œ๊ฐ„์œผ๋กœ ์ˆ˜ํ–‰๋œ๋‹ค! ๐Ÿ˜ ์ž์„ธํ•œ ๋‚ด์šฉ์€ Lecture 3: Fibonacci Heaps์˜ โ€œ5. Analysisโ€๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


์ด ์ •๋„๋ฉด ์ถฉ๋ถ„ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ, <Fibonacci Heap>์€ ์—ฌ๊ธฐ์„œ <Maximally Damaged Tree>๋ผ๋Š” ๊ฐœ๋…์„ ๋˜ ์†Œ๊ฐœํ•œ๋‹ค! ๐Ÿ˜ฑ ์ด ๋…€์„์— ์˜ํ•ด ์ด Heap ๊ตฌ์กฐ๊ฐ€ โ€œFibonacciโ€ Heap์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๊ฒŒ ๋˜์—ˆ์œผ๋‹ˆ ์กฐ๊ธˆ๋งŒ ๋” ํž˜์„ ๋‚ด๋ณด์ž! ๐Ÿคฆโ€โ™‚๏ธ (์‹œ๊ฐ„๋ณต์žก๋„์™€ ๊ด€๋ จ๋œ ๊ฐœ๋…์€ ์•„๋‹ˆ๋‹ˆ ๊ฐ€๋ฒผ์šด ๋งˆ์Œ์œผ๋กœ ์ฝ์ž ๐Ÿ˜„)

A <maximally damaged tree> is a binomial tree of degree $k$1 which has been maximally damaged by cutting off subtrees from the lazy decreaseKey operation.

<Maximally damaged tree>๋ž€ degree $k$์˜ BIN Tree์— $\texttt{decreaseKey}$๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ, Tree์˜ degree $k$๋ฅผ ํ›ผ์†ํ•˜์ง€ ์•Š์„๋•Œ๊นŒ์ง€ $\texttt{decreaseKey}$๋ฅผ ์ˆ˜ํ–‰ํ•œ ํŠธ๋ฆฌ๋ฅผ ๋งํ•œ๋‹ค. ์ด ๊ฐœ๋…์€ ์šฐ๋ฆฌ๊ฐ€ ์•ž์—์„œ ์ •์˜ํ•œ ์ƒˆ๋กœ์šด $\texttt{decreaseKey}$์˜ ๋ฐฉ์‹์—์„œ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์œ ๋„๋˜๋Š” ๊ฐœ๋…์ด๋‹ค. // ์ด ๋ถ€๋ถ„ ์—ญ์‹œ ์˜์ƒ์—์„œ ์ž˜ ์„ค๋ช…ํ•˜๋‹ˆ ์˜์ƒ์„ ๋ณด์ž!

BIN tree์˜ degree๊ฐ€ ์ค„์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ $\texttt{decreaseKey}$๋ฅผ ์ˆ˜ํ–‰ํ•œ ๋ชจ์Šต

Corollary.

A <maximally-damaged tree of degree $k$> is a tree whose children are maximally-damaged trees of degrees $0, 0, 1, 2, 3, \dots, k-2$.

์œ„์˜ ๋”ฐ๋ฆ„ ์ •๋ฆฌ์— ์˜ํ•ด <maximally damaged tree>์—์„œ๋Š” ์•„๋ž˜์˜ ์ •๋ฆฌ๊ฐ€ ์„ฑ๋ฆฝํ•œ๋‹ค! ๐Ÿ˜ฒ

Theorem.

The #. of nodes in a <maximally damaged tree of degree $k$> is $F_{k+2}$.

์ฆ๋ช…์€ ์œ„์˜ ๋”ฐ๋ฆ„ ์ •๋ฆฌ์˜ ์‚ฌ์‹ค์„ ๊ทธ๋Œ€๋กœ ์ˆ˜์‹์œผ๋กœ ๊ธฐ์ˆ ํ•˜๋ฉด ๋œ๋‹ค. ๐Ÿ˜‰


์ง€๊ธˆ๊นŒ์ง€ ์‚ดํŽด๋ณธ <BIN Heap>๊ณผ ๊ทธ ๋ณ€ํ˜•๋“ค์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค ๐Ÿ™Œ

Operation Binomial Heap Lazy-Binomial Heap Fibonacci Heap
$\texttt{insert}$ $O(\log n)$ $O(1)$ $O(1)$
$\texttt{getMin}$ $O(1)$ $O(1)$ $O(1)$
$\texttt{extractMin}$ $O(\log n)$ amortized $O(\log n)$ amortized $O(\log n)$
$\texttt{merge}$ $O(\log n)$ $O(1)$ $O(1)$
$\texttt{decreaseKey}$ $O(\log n)$ $O(\log n)$ $O(1)$

์ง€๊ธˆ๊นŒ์ง€ <Heap> ๋˜๋Š” <Priority Queue>์˜ ์ฃผ์š”ํ•œ ๊ตฌ์กฐ๋“ค์„ ์‚ดํŽด๋ดค๋‹ค. ์†”์งํžˆ ๋งํ•ด ์‹ค์ „์—์„œ ์ž์ฃผ ์“ฐ๋Š” ๊ตฌ์กฐ๋“ค์€ ์•„๋‹ˆ์ง€๋งŒ, <์•Œ๊ณ ๋ฆฌ์ฆ˜> ์ˆ˜์—…๊ณผ ์ž๋ฃŒ๋“ค์—์„œ ์ข…์ข… ๋“ฑ์žฅํ•ด์„œ ์ด๋ฒˆ ๊ธฐํšŒ์— ์ญ‰ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค.

<Heap>์˜ ๋ณ€ํ˜•๋“ค์ด ๋ญ๊ฐ€ ์ค‘์š”ํ•œ๊ฐ€ ์‹ถ์ง€๋งŒ <Fibo Heap>์ด ์†Œ๊ฐœ๋œ ์•„ํ‹ฐํด์— ๋”ฐ๋ฅด๋ฉด <Fibo Heap>์„ ๋„์ž…ํ•จ์œผ๋กœ์จ Dijkstraโ€™s single-source shortest path algorithm์˜ ์„ฑ๋Šฅ์ด $O(E \log E)$์—์„œ $O(E + V \log V)$๋กœ ํš๊ธฐ์ ์œผ๋กœ ์ค„์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค! ๐Ÿ˜ฒ


์ฐธ๊ณ ์ž๋ฃŒ


  1. ์ด๋•Œ degree์˜ ์˜๋ฏธ๋Š” <Fibonacci Heap>์—์„œ ์ƒˆ๋กญ๊ฒŒ ๋„์ž…ํ•œ, children์˜ ๊ฐฏ์ˆ˜์˜ $k$๋‹ค.ย