Recurrences and analysis tools
Introducido
5:37
Diseño y análisis de algoritmos
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…
Narración en inglés · subtítulos en inglés y 中文 que puedes cambiar
En esta lección
| English | Significado |
|---|---|
| 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/ | recurrencia |
| recursion tree/rɪˈkɜːʃn triː/ | árbol de recursión |
| 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/ |
El árbol es el análisis
- Diseño y análisis de algoritmos valora un diseño antes de escribirlo. Una recurrencia expresa el costo de un algoritmo recursivo en términos de entradas más pequeñas: $T(n) = 2T(n/2) + \Theta(n)$. El árbol de recursión lo dibuja: el nivel $i$ tiene $2^i$ nodos de $n/2^i$, cada nivel cuesta $\Theta(n)$, la profundidad es $\log_2 n$ — así $T(n) = \Theta(n \log n)$.


For T(n) = 2T(n/2) + n, one tree level costs —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Level i has 2^i nodes of n/2^i: the product is n at every level.
El método maestro, una línea cada uno
- Para $T(n) = aT(n/b) + f(n)$, compara $f(n)$ con $n^{\log_b a}$ (el total de hojas). Dominan las hojas: $\Theta(n^{\log_b a})$. Equilibrado: $\Theta(f(n)\log n)$. La raíz domina: $\Theta(f(n))$.
- Casos del curso: $2T(n/2) + n \to n\log n$; $T(n/2) + 1 \to \log n$; $4T(n/2) + n \to n^2$ (las hojas $n^{\log_2 4}$ dominan).
T(n) = 4T(n/2) + n solves to —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Leaves dominate: n^(log_2 4) = n^2.
$T(n) = 4T(n/2) + n$ es $\Theta(n \log n)$, like merge sort, because it also halves $n$.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Comparar $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.
Demuéstralo por sustitución
- Adivina $T(n) \le c\,n\log n$ para $2T(n/2) + n$: sustituye y la inducción se cierra siempre que $c \ge 1$ — la adivinanza se convierte en prueba.
- Luego mide: la ordenación por fusión sobre el array fijo de elementos 64 realiza $310$ comparaciones, bajo el techo del peor caso $n\log_2 n - n + 1 = 321$. Los límites teóricos confirman la medición.
Merge sort on the 64-element course array makes — comparisons
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Measured 310, under the worst-case ceiling 321 = 64x6 - 64 + 1.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
32 + 16 + 8 + 4 + 2 + 1 = 63, under 2 x 32.
The amortised cost of one binary-counter increment is —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
n increments flip fewer than 2n bits, so each costs O(1) on average over the sequence.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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.
A favor $T(n) = 2T(\sqrt n) + 1$ starting from $n = 2^{16}$: how many square-root steps until the size is 2?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
$2^{16} \to 2^8 \to 2^4 \to 2^2 \to 2$: $\log_2 16 = 4$.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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:
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
$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.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
$f(n)$ is smaller by only a log factor, which falls between the cases; the tree gives $\Theta(n\log\log n)$.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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$.
¿Qué salió mal? Solo el equipo del sitio ve esta información.