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

6 minute read

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



Multiplication Algorithm

๊ณ„์‚ฐ์ ์ธ ๊ด€์ ์—์„œ ๋ฐ”๋ผ๋ณผ ๋•Œ, ๊ณฑ์…ˆ์€ ๋ง์…ˆ๋ณด๋‹ค ํ›จ์”ฌ ๋น„์šฉ์ด ๋งŽ์ด ๋“œ๋Š” ์ž‘์—…์ด๋‹ค. ์ธ๊ฐ„์˜ ๊ด€์ ์—์„œ๋„ ์ง€๊ธˆ ๋‹น์žฅ $2$์ž๋ฆฟ์ˆ˜ ์ˆ˜๋ฅผ ๋ง์…ˆํ•˜๋Š” ๊ฒƒ๊ณผ ๊ณฐ์…‰ํ•˜๋Š” ๊ฑธ ๋น„๊ตํ•ด๋ณด๋ฉด, $2$์ž๋ฆฟ์ˆ˜ ๊ณฑ์…ˆ์ด ๋” ํฐ ๋น„์šฉ์ด ๋“ ๋‹ค.

๊ณ„์‚ฐ์ ์ธ ๊ด€์ ์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. $2$์ž๋ฆฟ์ˆ˜๋ฅผ ๋ง์…ˆํ•˜๋Š” ๊ฒƒ์€ ๋งŽ์•„๋ด์•ผ $3$๋ฒˆ ๋ฐ˜ํผ์˜ iteration์„ ๋Œ๋ฉด ๋œ๋‹ค. ํ•˜์ง€๋งŒ, $2$์ž๋ฆฟ์ˆ˜ ๊ณฐ์…ˆ์€ $2 \times 2=4$, ์ด 4๋ฒˆ ๋งŒํผ์˜ iteration์„ ์š”๊ตฌํ•œ๋‹ค. ์‚ฌ์ด์ฆˆ๋ฅผ ๋Š˜๋ ค, $n$์ž๋ฆฟ์ˆ˜ ๋ง์…ˆ์€ $n+1$๋ฒˆ ๋งŒํผ์˜ iteration์„, $n$์ž๋ฆฟ์ˆ˜ ๊ณฐ์…ˆ์€ $n \times n=n^2$ ๋งŒํผ์˜ iteration์„ ์š”๊ตฌํ•œ๋‹ค. Big-O notation์„ ์“ฐ๋ฉด,

  • ๋ง์…ˆ: $O(n)$
  • ๊ณฑ์…ˆ: $O(n^2)$

์ธ๊ฐ„์€ ๊ณฑ์…ˆ์„ ์ด์ค‘ for๋ฌธ์˜ ๋ฐฉ์‹์œผ๋กœ cascade ํ•˜๊ฒŒ ๊ณฑ์…ˆ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ํ•˜์ง€๋งŒ, ๊ณฑ์…ˆ์„ <์žฌ๊ท€ recursion>์„ ์ด์šฉํ•ด ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํ•œ๋ฒˆ ์‚ดํŽด๋ณด์ž.


๋จผ์ € โ€œ๋ณต์†Œ์ˆ˜ ๊ณฑ์…ˆโ€์—์„œ ์˜๊ฐ์„ ์–ป์–ด๋ณด์ž.

For two complex numbers $x=a+by$, $y=c+di$,

\[xy = (a+bi)(c+di) = (ac - bd) + (ad + bc)i\]

์œ„์˜ ๊ณผ์ •์—์„œ $xy$๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„  $ac$, $bd$, $ad$, $bc$ ์ด 4๋ฒˆ์˜ ๊ณฑ์…ˆ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ, ์•ฝ๊ฐ„์˜ ํŠธ๋ฆญ์„ ์“ฐ๋ฉด, 3๋ฒˆ๋งŒ์˜ ๊ณฑ์…ˆ์œผ๋กœ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค!!

\[xy = (a+bi)(c+di) = (ac-bd) + \left( (a+b)(c+d) - ac - bd \right) i\]

์œ„์˜ ์‹์—์„  $ac$, $bd$, $(a+b)(c+d)$ ์ด 3๋ฒˆ์˜ ๊ณฑ์…ˆ์„ ์ˆ˜ํ–‰ํ•ด ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์—ˆ๋‹ค! ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ณผ์ •์—์„œ $ac$, $bd$๋ฅผ ์žฌํ™œ์šฉํ–ˆ๋‹ค. ๋ช‡๋ฒˆ์˜ ๋ง์…ˆ์ด ์ถ”๊ฐ€๋˜์—ˆ์ง€๋งŒ, ๊ณฑ์…ˆ์˜ ๊ณ„์‚ฐ ์ฝ”์ŠคํŠธ๋ฅผ ์ƒ๊ฐํ•˜๋ฉด ๊ดœ์ฐฎ์€ ๊ฑฐ๋ž˜๋‹ค!


์ด๊ฒƒ์„ โ€œ์ž์—ฐ์ˆ˜ ๊ณฑ์…ˆโ€์—๋„ ์ ์šฉํ•ด๋ณผ ์ˆ˜ ์žˆ์„๊นŒ?

๋‘ $n$-bit number $x$, $y$๊ฐ€ ์žˆ์„ ๋•Œ, $xy$๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

\[xy = \left(2^{n/2} \cdot x_L + x_R\right) \left(2^{n/2} \cdot y_L + y_R\right)\]

$n$-bit์˜ ์ˆ˜ $x$๋ฅผ ์ ˆ๋ฐ˜์ธ $n/2$์—์„œ $x_L$, $x_R$๋กœ <๋ถ„ํ•  divide>ํ–ˆ๋‹ค!!

์‹์„ ์ „๊ฐœํ•˜๋ฉด,

\[\begin{aligned} xy &= \left(2^{n/2} \cdot x_L + x_R\right) \left(2^{n/2} \cdot y_L + y_R\right) \\ &= 2^n \cdot x_L y_L + 2^{n/2} \cdot \left(x_L y_R + x_R y_L \right) + x_R y_R \end{aligned}\]

์œ„ ์‹์—์„œ $n/2$-bit ๊ณฑ์…ˆ์€ $x_L y_L$, $x_L y_R$, $x_R y_L$, $x_R y_R$ ์ด 4๋ฒˆ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๊ฐ๊ฐ์˜ $n/2$-bit ๊ณฑ์…ˆ์— ๋Œ€ํ•ด์„œ๋„ <๋ถ„ํ•  ์ •๋ณต>์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ ํ™”์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

\[T(n) = 4 \cdot T(n/2) + O(n) = O(n^2)\]

๊ทธ๋Ÿฐ๋ฐ, ์ด๋•Œ $\left(x_L y_R + x_R y_L \right)$ ๋ถ€๋ถ„์„ ์‚ดํŽด๋ณด๋ฉด โ€œ๋ณต์†Œ์ˆ˜ ๊ณฑ์…ˆโ€์ฒ˜๋Ÿผ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ์žฌํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์ด ์กด์žฌํ•œ๋‹ค!!

\[\left(x_L y_R + x_R y_L \right) = (x_L + x_R)(y_L + y_R) - x_L y_L - x_R y_R\]

์ฆ‰, $n$-bit ์ž์—ฐ์ˆ˜์˜ ๊ณฑ์…ˆ ์—ญ์‹œ ๊ฐ ๊ณผ์ •์—์„œ 3๋ฒˆ์˜ ๊ณฑ์…ˆ๋งŒ์œผ๋กœ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค!! ๐Ÿ˜†

์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ ํ™”์‹์„ ๋‹ค์‹œ ์“ฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

\[T(n) = 3 \cdot T(n/2) + O(n) = O(?)\]

<Master Theorem>์„ ์ ์šฉํ•˜๊ธฐ ์ „์— ์žฌ๊ท€๋ฅผ ์ด์šฉํ•œ <Multiplication Algorithm>์ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€๋ถ€ํ„ฐ ์ฝ”๋“œ-๋ ˆ๋ฒจ์—์„œ ์‚ดํŽด๋ณด์ž.

function multiply($x$, $y$)
Input: Two $n$-bit integers $x$ and $y$
Output: Their product $xy$


if $n=1$ then
โ€ƒโ€ƒ return $xy$
end if

$x_L$, $x_R$ = leftmost $\lceil n/2 \rceil$, rightmost $\lfloor n/2 \rfloor$ bits of $x$
$y_L$, $y_R$ = leftmost $\lceil n/2 \rceil$, rightmost $\lfloor n/2 \rfloor$ bits of $y$

$P_1$ = multiply($x_L$, $y_L$)
$P_2$ = multiply($x_R$, $y_R$)
$P_3$ = multiply($x_L + x_R$, $y_L + y_R$)

return $P_1 \times 2^n + \left(P_3 - P_1 - P_2\right) \times 2^{n/2} + P_2$

์ด์ œ <Master Theorem>์„ ์ด์šฉํ•ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์œ ๋„ํ•˜์ž.

๋งค ๊ณผ์ •๋งˆ๋‹ค ๋ฌธ์ œ๊ฐ€ $3$๊ฐœ์˜ ํ•˜์œ„ ๋ฌธ์ œ๋กœ โ€œ๋ถ„ํ• โ€๋˜๊ณ , ๋ถ„ํ• ๋œ ํ•˜์œ„ ๋ฌธ์ œ๋Š” ๋ณธ๋ž˜ ๋ฌธ์ œ์˜ $n/2$ ๋งŒํผ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„๋‹ค. โ€œat $k$-th level, there are $3^k$ subproblems, each of size $n/2^k$โ€

\[3^k \cdot O(n/2^k)\]

๋ฌธ์ œ๊ฐ€ ๋งค๋ฒˆ ์ ˆ๋ฐ˜์”ฉ ๋ถ„ํ• ๋˜๋ฏ€๋กœ, ๋ถ„ํ• ์€ ์ด $\log_2 n$๋ฒˆ ์ด๋ค„์ง„๋‹ค.

๋”ฐ๋ผ์„œ

\[\sum^{\log_2 n}_{k=0} {3^k \cdot O(n/2^k)}\]

์‹์„ ์ „๊ฐœํ•˜๋ฉด,

\[\begin{aligned} \sum^{\log_2 n}_{k=0} {3^k \cdot O(n/2^k)} &= \sum^{\log_2 n}_{k=0} {(3/2)^k \cdot O(n)} \\ &= O(n) \cdot \sum^{\log_2 n}_{k=0} {(3/2)^k } \\ &= O(n) \cdot \frac{(3/2)^{\log_2 {n} + 1} - 1}{3/2-1} \\ &= O(n) \cdot O\left( (3/2)^{\log_2 {n}} \right) \\ &= O(n) \cdot O\left( \frac{3^{\log_2 {n}}}{2^{\log_2 {n}}} \right) \\ &= O(n) \cdot O\left( \frac{n^{\log_2{3}}}{n} \right) \\ &= O(n^{\log_2 {3}}) \end{aligned}\]

์ฆ‰, <๋ถ„ํ•  ์ •๋ณต>์„ ์ด์šฉํ•ด ๊ณฑ์…ˆํ•  ๊ฒฝ์šฐ, ๊ธฐ์กด์˜ $O(n^2)$๋ณด๋‹ค ๊ฐœ์„ ๋œ $O(n^{\log_2{3}})$๋งŒ์— ๊ณฑ์…ˆ์„ ๋ˆ„๋ฆด ์ˆ˜ ์žˆ๋‹ค!! ๐Ÿ‘


์ด ๋น ๋ฅธ ๊ณฑ์…ˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ <์นด๋ผ์ธ ๋ฐ”์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜; Karatsuba Algorithm>์ด๋ผ๊ณ  ํ•œ๋‹ค. 1960๋…„์— ์นด๋ผ์ธ ๋ฐ”๊ฐ€ ์ œ์‹œํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ–‰๋ ฌ ๊ณฑ์…ˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ์ŠˆํŠธ๋ผ์„ผ(Stressen) ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ์นด๋ผ์ธ ๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋น„์Šทํ•œ ๋งฅ๋ฝ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ๋ณ‘ํ•ฉ(merge) ํ•˜๋Š” ๋‹จ๊ณ„์—์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ธ ์ผ€์ด์Šค์ด๋‹ค.

์ด <์นด๋ผ์ธ ๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜>๋ณด๋‹ค ๋” ๋น ๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ์กด์žฌํ•œ๋‹ค! <FFT; ๊ณ ์† ํ‘ธ๋ฆฌ์— ๋ณ€ํ™˜>์„ ์“ฐ๋ฉด ๋” ๋น ๋ฅด๊ฒŒ ๋‘ ์ˆ˜์˜ ๊ณฑ์…ˆ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋‚ด์šฉ์€ <FFT; ๊ณ ์† ํ‘ธ๋ฆฌ์— ๋ณ€ํ™˜> ํฌ์ŠคํŠธ์—์„œ ๋‹ค์‹œ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ๋‹ค ๐Ÿ™Œ


references

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ•ด๊ฒฐ์ „๋žต: ์นด๋ผ์ธ ๋ฐ”์˜ ๋น ๋ฅธ ๊ณฑ์…ˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜

Categories:

Updated: