Divide and Conquer
2020-1νκΈ°, λνμμ βμκ³ λ¦¬μ¦β μμ μ λ£κ³ 곡λΆν λ°λ₯Ό μ 리ν κΈμ λλ€. μ§μ μ μΈμ λ νμμ λλ€ :)
Divide and Conquer
<λΆν μ 볡 Divide and Conquer>λ λ¬Έμ λ₯Ό ν΄κ²°νλ μ κ·Όλ² μ€ νλλ€. κ·Έ κ³Όμ μ κ°λ΅ν κΈ°μ νλ©΄ μλμ κ°λ€.
- Break the problem into βsubproblemsβ.
- Solve them recursively.
- Combine the solutions to get the answer to the problem.
λΆν μ 볡μ κΈ°λ³Έμ μΌλ‘ <μ¬κ· Recursion>μ κΈ°λ°μΌλ‘ νλ€. λ¬Έμ λ₯Ό <νμ λ¬Έμ subproblem>λ‘ λΆν νκ³ , κ·Έ νμ λ¬Έμ λ₯Ό λ νμμ λ¬Έμ λ‘ λΆν νλ€. λΆν μ μμ½κ² λ€λ£° μ μλ μμ€μΈ λ² μ΄μ€ μΌμ΄μ€ μ§νλλ€. μ΄νμ λΆν ν κ·μΉμ λ°λΌ νμ λ¬Έμ μμ μ»μ κ²°κ³Όλ₯Ό μμ λ¬Έμ μμ μ°¨λ‘λλ‘ μ‘°ν©(Combine) ν΄μ£Όλ©΄ μλ λ¬Έμ μ λν μ λ΅μ μ»λλ€.
Master Theorem: Recurrence Relations
μ΄ λ¨λ½μμλ <λΆν μ 볡>μ μκ° λ³΅μ‘λλ₯Ό μ λνλ μ’μ λκ΅¬μΈ <Master Theorem>μ λν΄ λ€λ£¬λ€.
λ¨Όμ μμ κ°μ΄ size $n$μ λ¬Έμ λ₯Ό size $n/b$μ subproblem $a$κ°λ‘ λΆν νλ κ²½μ°λ₯Ό κ³ λ €ν΄λ³΄μ. μ’λ νΉμ νμλ©΄, <branching fator>κ° $a$, <λΆν μ κΉμ΄ depth>κ° $\log_b {n}$μΈ μν©μ΄λ€. $O(n^c)$λ κ° κ³Όμ μμ λλ κ³ μ λΉμ©μ΄λ€.
μ΄κ²μ μ¬κ·μμΌλ‘ νννλ©΄ μλμ κ°λ€.
\[T(n) = a \cdot T(\lceil n/b \rceil) + O(n^c)\]<Master Theorem>μ λΆν μ 볡μ κ³μ° 볡μ‘λκ° $a$μ $b$, $c$μ κ΄κ³μ λ°λΌ μλμ κ°μ΄ μ λλ¨μ κΈ°μ νλ€.
\[T(n) = \begin{cases} O(n^c) &\mbox{if } c > \log_b {a} \\ O(n^d \log {n}) & \mbox{if } c = \log_b {a} \\ O(n^{\log_b {a}}) & \mbox{if } c < \log_b {a} \end{cases}\]$a$, $b$, $c$μ λν κ°λ§ μλ€λ©΄, κ³μ° 볡μ‘λλ₯Ό λ°λ‘ μ»μ μ μλ€λ! μΌλ§λ νΈλ¦¬ν μ 리μΈκ°!! νλ² μ¦λͺ ν΄λ³΄μ! π€©
proof.
At $k$-th level, we get $a_k$ subproblems of size $n/b^k$. Also we should consider fixed cost $O(n^c)$ too. At $k$-th level, the size of problem is $n/b^k$, so fixed cost is $O\left(\left(n/b^k\right)^c\right)$.
Then, the cost at $k$-th level is
\[a^k \cdot O\left(\left(n/b^k\right)^c\right)\]We will slightly modify the given form of cost as below.
\[a^k \cdot O\left(\left(n/b^k\right)^c\right) = O(n^c) \cdot \left( a/b^c \right)^k\]There are $\log_b {n}$ levels of division process, so letβs sum up the cost at each level.
\[\sum^{\log_b {n}}_{k=0} O(n^c) \cdot \left( a/b^c \right)^k\]Then, given series is a geometric series with ratio $a/b^c$.
The simple way to understand geometric series is watching the ratio!
Case 1. if ratio $a/b^c < 1$, then the series decreases.
Case 2. if ratio $a/b^c = 1$, then the series is a just constant sum up.
Case 3. if ratio $a/b^c > 1$, then the series increases.
- In Case 1, take the 1st term, $\implies$ $O(n^c)$
- In Case 2, take sum, $\implies$ $O(n^c) \cdot \log_b {n} = O(n^c \log_b {n}) = O(n^c \log n)$
- In Case 3, take the last term, $\implies$ $O(n^c) \cdot \left(a/b^c\right)^{\log_b {n}} = O(n^c) \cdot \frac{a^{\log_b n}}{n^c} = O\left( n^{\log_b a} \right)$
Therefore,
\[T(n) = \begin{cases} O(n^c) &\mbox{if } c > \log_b {a} \\ O(n^d \log {n}) & \mbox{if } c = \log_b {a} \\ O(n^{\log_b {a}}) & \mbox{if } c < \log_b {a} \end{cases}\]$\blacksquare$
μλλ <λΆν μ 볡>μ μ΄μ©ν΄ ν΄κ²°νλ λνμ μΈ μ£Όμ λ€μ λν΄ μ 리ν ν¬μ€νΈλ€μ΄λ€.