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

3 minute read

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


์ผ๋ฐ˜์ ์œผ๋กœ ํ–‰๋ ฌ ๊ณฑ์…ˆ $A_1 \times A_2 \times \cdots \times A_n$์€ Associative ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ ์ˆœ์„œ๋ฅผ ์–ด๋–ป๊ฒŒ ์ง„ํ–‰ํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ์„œ ์ „์ฒด ์—ฐ์‚ฐ์˜ ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด,

  • $A: 50 \times 20$
  • $B: 20 \times 1$
  • $C: 1 \times 10$
  • $D: 10 \times 100$

4๊ฐ€์ง€ ํ–‰๋ ฌ์ด ์žˆ์„ ๋•Œ,

  • $A \times ((B \times C) \times D) = 120,200$
  • $(A \times B) \times (C \times D) = 7,000$

์ด ๋œ๋‹ค. ์ฆ‰, ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ ์ˆœ์„œ๋ฅผ ์ž˜ ์กฐ์ •ํ•ด ํ•„์š”ํ•œ ์—ฐ์‚ฐ์˜ ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค!!


naive approach

๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜์ง€๋งŒ, ๊ฐ€์žฅ ๋น„์šฉ์ด ๋งŽ์ด ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๋ชจ๋“  ํ–‰๋ ฌ ๊ณฑ์…ˆ ์ˆœ์„œ๋ฅผ ๋ชจ๋‘ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์€ binary operation์ด๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด ํ–‰๋ ฌ ์—ฐ์‚ฐ์€ ์–ด๋–ค binary tree์˜ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ, ์šฐ๋ฆฌ๋Š” $n$๊ฐœ์˜ ํ–‰๋ ฌ์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ด binary tree๋Š” $n$๊ฐœ์˜ leaf node๋ฅผ ๊ฐ€์ง„ ํŠธ๋ฆฌ๊ฐ€ ๋œ๋‹ค. ๋˜ํ•œ, ๊ทธ ๊ฐ€์ง“์ˆ˜๋Š” exponentially manyํ•œ $2^n$๊ฐœ๊ฐ€ ๋œ๋‹ค.


Dynamic Programming

DP์—์„œ๋Š” ์ด ๋ฌธ์ œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ ‘๊ทผํ•œ๋‹ค.

"For a tree to be optimal, its subtree must also be optimal!"

๋‹ค๋ฅธ ๊ณณ์—์„œ๋Š” ๋ถ€๋ถ„์ˆ˜์—ด(sebsequence)์„ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค๊ณ ๋„ ์„ค๋ช…ํ•œ๋‹ค.

  1. ์ „์ฒด ์ˆ˜์—ด์„ ๊ธธ์ด 2์˜ ๋ถ€๋ถ„ ์ˆ˜์—ด๋กœ ๋ถ„๋ฆฌํ•œ๋‹ค.
  2. ๊ธธ์ด 2์˜ ๋ถ€๋ถ„ ์ˆ˜์—ด์—์„œ (์ตœ์†Œ) ๋น„์šฉ์„ ๊ตฌํ•œ๋‹ค.
  3. ๊ธธ์ด 3์˜ ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ์‚ดํŽด๋ณธ๋‹ค. ์ด๋•Œ, ๊ธธ์ด 2์˜ ๋ถ€๋ถ„ ์ˆ˜์—ด์—์„œ ๊ตฌํ•œ ๋น„์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ตœ์†Œ ๋น„์šฉ์„ ๊ตฌํ•œ๋‹ค.
  4. ๊ธธ์ด 4๋Š” ๊ธธ์ด 3์—์„œ ๊ตฌํ•œ ์ตœ์†Œ ๋น„์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ตœ์†Œ ๋น„์šฉ์„ ๊ตฌํ•œ๋‹ค.
  5. ์ด ๊ณผ์ •์„ ๊ธธ์ด $n$๊นŒ์ง€ ๋ฐ˜๋ณต

์ด ๊ณผ์ •์„ ์ •๋ฆฌํ•ด ๊ธฐ์ˆ ํ•˜๋ฉด,

  • $C(i, j)$ = min cost of multiplying $A_i \times \cdots A_j$

for $\ell = 1, 2, \dots, n$
โ€ƒโ€ƒfor $i=1, 2, \dots, n - \ell + 1$
โ€ƒโ€ƒโ€ƒโ€ƒ$j = i + \ell$
โ€ƒโ€ƒโ€ƒโ€ƒfor $k=i, \dots, j-1$
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ$\texttt{cost} = C(i, k) + C(k+1, j) + m_{i-1} \times m_k \times m_j$
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ$C(i, j) = \min \{ C(i, j), \; \texttt{cost}\}$

return $C(1, n)$

์œ„์˜ ๊ณผ์ •์„ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

Image from here

๋งŒ์•ฝ best-split ์ˆœ์„œ๋ฅผ ๊ธฐ๋กํ•ด ์‹ค์ œ binary opr tree๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, $C(i, j)$๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ๊ณผ์ •์—์„œ ์–ธ์ œ minimum์„ ๋‹ฌ์„ฑํ•˜๋Š”์ง€ ๊ธฐ๋กํ•ด๋‘๋ฉด ๋œ๋‹ค.

โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒif $\texttt{cost} < C(i, j)$
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ$C(i, j) = \texttt{cost}$
โ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒโ€ƒ$S(i, j) = k$

๊ทธ๋ฆฌ๊ณ  binary opr tree๋ฅผ ์žฌํ˜„ํ•  ๋•Œ๋Š” $S(1, n)$์œผ๋กœ splitting point๋ฅผ ์ฐพ๊ณ , ๊ทธ๋ฆฌ๊ณ  $S(1, k)$, $S(k, n)$์˜ ๊ฐ’์œผ๋กœ ๊ทธ ๋‹ค์Œ splitting point์„ ์ฐพ๊ณ , โ€ฆ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด ๋œ๋‹ค ๐Ÿ˜


์ถ”์ฒœ ๋ฌธ์ œ


์ฐธ๊ณ ์ž๋ฃŒ