Recurrences and analysis tools
引入
5:37
算法设计与分析
本课程教你在把真实数据交给算法处理之前,先评估其设计的计算代价。前两个单元介绍分析工具和分治法。接下来两个单元介绍两种选择模式:贪心算法和动态规划。最后两个单元探索前沿内容:先是图优化,然后是归约和 NP 完全性。每个论断都用具体输入加以检验。
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| Algorithm design and analysis/ˈælɡərɪθəm dɪˈzaɪn ænd əˈnæləsɪs/ | 算法设计与分析 | suàn fǎ shè jì yǔ fēn xī |
| recurrence/rɪˈkʌrəns/ | 递推式 | dì tuī shì |
| recursion tree/rɪˈkɜːʃn triː/ | 递归树 | dì guī shù |
| root/ruːt/ | 根节点 | gēn jié diǎn |
| amortised/əˈmɔːtaɪzd/ | 均摊 | jūn tān |
| potential method/pəˈtenʃl ˈmeθəd/ | 势能法 | shì néng fǎ |
| square root/skweə ruːt/ | 平方根 | píng fāng gēn |
树即分析
- 算法设计与分析 algorithm design and analysis 先定价再动手。递推式 recurrence 用更小的输入表达递归算法的代价:$T(n) = 2T(n/2) + \Theta(n)$。递归树 recursion tree 把它画出来:第 $i$ 层有 $2^i$ 个规模 $n/2^i$ 的节点、每层 $\Theta(n)$、深 $\log_2 n$——所以 $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.
主定理三行版
- 对 $T(n) = aT(n/b) + f(n)$,把 $f(n)$ 与叶总量 $n^{\log_b a}$ 比较。叶胜:$\Theta(n^{\log_b a})$;平衡:$\Theta(f(n)\log n)$;根胜:$\Theta(f(n))$。
- 课程例:$2T(n/2) + n \to n\log n$;$T(n/2) + 1 \to \log n$;$4T(n/2) + n \to n^2$(叶 $n^{\log_2 4}$ 胜)。
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.
代入法证明
- 猜 $2T(n/2) + n$ 满足 $T(n) \le c\,n\log n$:代入后归纳在 $c \ge 1$ 时闭合——猜想升级为证明。
- 再测量:归并排序在固定 64 元课程数组上做 $310$ 次比较,低于最坏上界 $n\log_2 n - n + 1 = 321$。理论定界、实测确认。
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$.
出了什么问题?只有网站团队能看到这些信息。