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ː/ | شجرة العودية |
| root/ruːt/ | |
| 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 |
| square root/skweə ruːt/ |
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$ هي $\Theta(n \log n)$, like merge sort, because it also halves $n$.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
مقارنة $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.
للدفاع $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$.
ما المشكلة؟ لا يرى ذلك سوى فريق الموقع.