Recurrences and analysis tools
도입됨
5:37
알고리즘 설계 및 분석
This course teaches you to price an algorithm's design before you trust it with real data. The first two units cover analysis tools and divide and conquer. The…
영어 내레이션 · 영어와 중국어 자막 전환 가능
이 수업의 내용
| English | 의미 |
|---|---|
| Algorithm design and analysis/ˈælɡərɪθəm dɪˈzaɪn ænd əˈnæləsɪs/ | the study of how to build algorithms and prove their correctness and cost |
| recurrence/rɪˈkʌrəns/ | 재귀식 |
| recursion tree/rɪˈkɜːʃn triː/ | 재귀 트리 |
| amortised/əˈmɔːtaɪzd/ | averaged over a long sequence of operations, so rare expensive steps are shared out among many cheap ones |
| potential method/pəˈtenʃl ˈmeθəd/ | amortised analysis that stores saved-up work as "potential" to pay for later expensive steps |
The tree is the analysis
- Algorithm design and analysis 算法设计与分析 prices a design before you write it. A recurrence 递推式 gives the cost of a recursive algorithm in terms of smaller inputs: $T(n) = 2T(n/2) + \Theta(n)$.
- The recursion tree 递归树 draws it. Level $i$ has $2^i$ nodes of size $n/2^i$, every level costs $\Theta(n)$, and there are $\log_2 n$ levels, so $T(n) = \Theta(n \log n)$.

For T(n) = 2T(n/2) + n, one tree level costs —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Level i has 2^i nodes of n/2^i: the product is n at every level.
The master method, one line each
- For $T(n) = aT(n/b) + f(n)$, compare $f(n)$, the work at the root, with $n^{\log_b a}$, the total cost of the leaves.
- If one is larger by a power of $n$, it wins. If they are the same size, every level costs the same, so multiply by the $\log n$ levels: $T(n/2) + 1 \to \log n$.
| Bigger | $T(n)$ | Example |
|---|---|---|
| leaves | $\Theta(n^{\log_b a})$ | $4T(n/2) + n \to n^2$ |
| neither | $\Theta(f(n)\log n)$ | $2T(n/2) + n \to n\log n$ |
| root | $\Theta(f(n))$ | $2T(n/2) + n^2 \to n^2$ |
T(n) = 4T(n/2) + n solves to —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Leaves dominate: n^(log_2 4) = n^2.
$T(n) = 4T(n/2) + n$ is $\Theta(n \log n)$, like merge sort, because it also halves $n$.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Compare (비교) $n^{\log_b a} = n^{\log_2 4} = n^2$ with $f(n) = n$: the leaves dominate, so $T(n) = \Theta(n^2)$. Four subproblems instead of two changes the answer.
Prove it by substitution
- Guess $T(n) \le c\,n\log n$ for $2T(n/2) + n$. Put the guess into the recurrence: the induction works whenever $c \ge 1$, so the guess becomes a proof.
- Then measure. Merge sort on the fixed 64-element course array makes $310$ comparisons, under the worst-case limit $n\log_2 n - n + 1 = 321$. Theory gives the bound; measurement confirms it.
Merge sort on the 64-element course array makes — comparisons
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Measured 310, under the worst-case ceiling 321 = 64x6 - 64 + 1.
Amortised analysis
- Some operations are expensive only now and then. One increment of a binary counter can flip all $k$ bits, but bit $0$ flips every time, bit $1$ every second time, bit $2$ every fourth time. Sixteen increments from $0$ flip $16 + 8 + 4 + 2 + 1 = 31$ bits: fewer than $2n$.
- So the amortised 均摊 cost is at most $2$ flips per increment. The potential method 势能法 proves it by saving credit: each $0 \to 1$ flip pays one extra unit, which later pays for that bit's $1 \to 0$ flip.
- The worst case of one operation and the average over a sequence are different claims.
A binary counter starts at 0 and is incremented 32 times. How many bit flips happen in total?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
32 + 16 + 8 + 4 + 2 + 1 = 63, under 2 x 32.
The amortised cost of one binary-counter increment is —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
n increments flip fewer than 2n bits, so each costs O(1) on average over the sequence.
When the master method does not fit
- $T(n) = 2T(\sqrt n) + \log n$ has no $n/b$. Substitute $m = \log_2 n$ and $S(m) = T(2^m)$: then $S(m) = 2S(m/2) + m$, which is $\Theta(m\log m)$. Back in $n$: $T(n) = \Theta(\log n \log\log n)$.
- The square root shrinks the size quickly. From $n = 2^{16}$ the sizes go $2^8, 2^4, 2^2, 2$, so there are only $\log_2\log_2 n = \mathbf{4}$ levels.
지지(For) $T(n) = 2T(\sqrt n) + 1$ starting from $n = 2^{16}$: how many square-root steps until the size is 2?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
$2^{16} \to 2^8 \to 2^4 \to 2^2 \to 2$: $\log_2 16 = 4$.
Worked example: the gap between the cases
- $T(n) = 2T(n/2) + n/\log n$: here $f(n)$ is smaller than $n^{\log_2 2} = n$, but only by a log, not by a power $n^\varepsilon$. None of the three cases applies.
- The recursion tree still works. Level $i$ costs $n/\log(n/2^i) = n/(\log n - i)$, so the levels add up to $n(1 + 1/2 + \dots + 1/\log n) = \Theta(n\log\log n)$. When the master method says nothing, draw the tree.
$T(n) = 3T(n/4) + n\log n$ solves to:
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
$n^{\log_4 3} \approx n^{0.79}$ is polynomially smaller than $f(n)$, so the root dominates.
The master method gives $T(n) = 2T(n/2) + n/\log n$ directly.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
$f(n)$ is smaller by only a log factor, which falls between the cases; the tree gives $\Theta(n\log\log n)$.
Marks that slip away
- Judging the master theorem by $b$ alone. $4T(n/2) + n$ also halves but is $\Theta(n^2)$: compare $f(n)$ with $n^{\log_b a}$.
- A substitution proof whose constant grows. The induction must finish with the same $c$ it assumed; ending at $c\,n\log n + n$ proves nothing.
- Treating amortised cost as an average over inputs. It is a bound on every sequence of operations, including the worst one.
Recap
- Recursion tree: $2T(n/2) + n$ has $\log_2 n$ levels of cost $n$, so $\Theta(n \log n)$.
- Master method: compare $f(n)$ with $n^{\log_b a}$; the leaves dominate, they are equal, or the root dominates.
- Prove by substitution, then measure: $310$ comparisons against the limit of $321$.
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.