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/ |
木を使った解析
- アルゴリズムの設計と解析では、コードを書く前に設計の計算量を見積もります。漸化式は、再帰アルゴリズムの計算量を、より小さな入力に対する計算量を使って表します:$T(n) = 2T(n/2) + \Theta(n)$。
- 再帰木は、それを図に表したものです。レベル$i$にはサイズ$n/2^i$のノードが$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}$を比較します。
- 一方がもう一方より$n$の累乗の分だけ大きければ、大きい方が支配的になります。同じ大きさなら、各レベルの計算量は等しいので、レベル数$\log n$を掛けます:$T(n/2) + 1 \to \log n$。
| 大きい方 | $T(n)$ | 例 |
|---|---|---|
| 葉 | $\Theta(n^{\log_b a})$ | $4T(n/2) + n \to n^2$ |
| どちらでもない | $\Theta(f(n)\log n)$ | $2T(n/2) + n \to n\log n$ |
| 根 | $\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 コイン + 今日初回達成時の小ボーナス
比较 $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.
償却解析
- 処理量が大きくなるのは、ときどきだけという操作があります。二進カウンターを1増やすと、$k$ビットすべてが反転することもありますが、ビット$0$は毎回、ビット$1$は2回に1回、ビット$2$は4回に1回反転します。$0$から16回増やすと、反転するのは$16 + 8 + 4 + 2 + 1 = 31$ビットで、$2n$より少なくなります。
- したがって、1回増やす操作の償却計算量は、最大で$2$回のビット反転です。ポテンシャル法では、余分な費用を貯めることでこれを証明します。$0 \to 1$への反転ごとに1単位を余分に支払い、後でそのビットが$1 \to 0$へ反転するときの費用に充てます。
- 1回の操作の最悪の場合と、一連の操作の平均についての主張は異なります。
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.
マスター法が使えない場合
- $T(n) = 2T(\sqrt n) + \log n$には$n/b$がありません。$m = \log_2 n$と$S(m) = T(2^m)$を代入すると、$S(m) = 2S(m/2) + m$となり、これは$\Theta(m\log m)$です。$n$に戻すと、$T(n) = \Theta(\log n \log\log n)$となります。
- 平方根を取ると、サイズは急速に小さくなります。$n = 2^{16}$から始めるとサイズは$2^8, 2^4, 2^2, 2$と変化するので、レベルは$\log_2\log_2 n = \mathbf{4}$個しかありません。
賛成 $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$.
例題:各場合の間のすき間
- $T(n) = 2T(n/2) + n/\log n$:ここでは$f(n)$は$n^{\log_2 2} = n$より小さいものの、その差は対数の分だけで、$n^\varepsilon$の累乗の分ではありません。3つのどの場合にも当てはまりません。
- それでも再帰木は使えます。レベル$i$の計算量は$n/\log(n/2^i) = n/(\log n - i)$なので、全レベルの合計は$n(1 + 1/2 + \dots + 1/\log n) = \Theta(n\log\log n)$です。マスター法で答えが出ないときは、木を描きましょう。
$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)$.
点を落としやすいところ
- $b$だけでマスター定理を判断する。 $4T(n/2) + n$も半分になりますが、$\Theta(n^2)$です。$f(n)$と$n^{\log_b a}$を比較しましょう。
- 代入法の証明で定数が大きくなる。 帰納法では、仮定したものと同じ$c$で終わる必要があります。$c\,n\log n + n$で終わっても何も証明できません。
- 償却計算量を入力に対する平均と考える。 償却計算量は、最悪の操作列を含め、あらゆる操作列に対する上限です。
まとめ
- 再帰木:$2T(n/2) + n$には計算量が$n$のレベルが$\log_2 n$個あるので、$\Theta(n \log n)$となります。
- マスター法:$f(n)$と$n^{\log_b a}$を比較します。葉が支配的か、両者が等しいか、根が支配的かのいずれかです。
- 代入法で証明し、次に測定します。比較回数は$310$回で、上限は$321$です。
何が問題でしたか?この内容はサイトの運営チームだけが確認します。