Asymptotic analysis
แนะนำใหม่
7:45
โครงสร้างข้อมูลและอัลกอริทึม
This course is about choosing a container by what it costs. The first unit is the yardstick: how cost grows with the size of the input. The next four are the…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| data structures and algorithms/ˈdeɪtə ˈstrʌktʃəz ænd ˈælɡərɪθəmz/ | โครงสร้างข้อมูลและอัลกอริทึม |
| Big-O/bɪɡ əʊ/ | โอใหญ่ |
| Big-Ω/bɪɡ əʊˈmeɪɡə/ | an asymptotic lower bound: the cost grows at least as fast as this, up to a constant factor |
| Big-Θ | เทตต้าใหญ่ |
| binary search/ˈbaɪnəri sɜːtʃ/ | A search of ordered data that repeatedly compares with the middle item and discards half of the remaining search range. |
| nested loop/ˈnestɪd luːp/ | A loop located inside another loop, so its iterations occur as part of each relevant outer iteration. |
| recurrence/rɪˈkʌrəns/ | An equation expressing a quantity, such as running time, using values for smaller inputs. |
| master theorem/ˈmæstə ˈθɪərəm/ | a formula that gives the running time of divide-and-conquer recurrences of the form T(n) = aT(n/b) + f(n) |
| witness/ˈwɪtnəs/ | The constants c and n0 that prove a Big-O claim: the bound holds with factor c for every n from n0 on. |
The yardstick
- Data structures and algorithms 数据结构与算法 means choosing containers and methods by how their cost grows with the input. Big-O 大O记号 is an upper bound on growth, up to a constant. Big-Ω 大Ω记号 is the lower bound, and Big-Θ 大Θ记号 gives the exact rate.
- Drop constants and lower-order terms: $3n^2 + 5n + 7$ is $\Theta(n^2)$. The class matters more than the constant: at $n = 1000$, $n \log_2 n \approx 9\,966$ but $n^2 = 10^6$, about $100$ times more.
The class of 3n^2 + 5n + 7 is —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Drop constants and lower-order terms; the n squared term grows fastest.
The growth table
- How each class grows (logs rounded):
| $n$ | $\log_2 n$ | $n \log_2 n$ | $n^2$ |
|---|---|---|---|
| 10 | 3 | 33 | 100 |
| 100 | 7 | 664 | $10^4$ |
| 1000 | 10 | 9,966 | $10^6$ |
- And $2^{n}$ is off the scale: $2^{100} \approx 10^{30}$.
- Logs come from halving. An algorithm that halves $n$ at each step has $O(\log n)$ levels: standard binary search 二分查找 on $16$ items can need $5$ probes, including the final single-item interval.

Growth classes pull apart
Move n and compare log n, n, n log n and n² operation counts. Even at n = 16 the quadratic is sixteen times the linear, and the gap keeps widening.
Halving the input each step touches —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Each halving reduces the remaining search range, giving logarithmic growth; standard binary search on 16 items can need 5 probes.
Order these costs from slowest-growing to fastest-growing.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ที่ $n = 1000$ they read about $10$, $1000$, $9\,966$, $10^6$ และ $10^{301}$: each class ends up far bigger than the one before it, whatever the constants.
Measure by doubling
- Two loops with $n = 100$: a single loop runs its body $100$ times, and a nested loop 嵌套循环 runs it $100 \times 100 = 10\,000$ times. Double $n$: the single loop doubles to $200$, the nested loop goes up four times to $40\,000$.
- The ratio on doubling shows the exponent: $2$ means linear, $4$ means quadratic. Count the cost before you believe a claim about it.
A loop that halves i on each pass is $\Theta(\log n)$. Two loops over $n$, one after the other, add up to $\Theta(n)$. A loop inside a halving loop multiplies to $\Theta(n \log n)$, the shape of merge sort.
Doubling n quadruples the running count. The exponent is —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The measured ratio on doubling is the exponent: 4 means quadratic.
Recurrences: the cost of recursion
- The cost of a recursive algorithm is a recurrence 递推式. Merge sort splits the input in two and merges in linear time: $T(n) = 2T(n/2) + n$.
- There are $\log_2 n$ levels, each costing $n$, so the total is $\Theta(n \log n)$. From $T(1) = 0$: $T(2) = 2$, $T(4) = 8$, $T(8) = 24 = 8 \log_2 8$.
- The master theorem 主定理 compares the work in the leaves, $n^{\log_b a}$, with the combine work. $T(n) = T(n/2) + 1$ is $\Theta(\log n)$ (binary search). $T(n) = 4T(n/2) + n$ is $\Theta(n^2)$: four half-size calls already cost $n^2$, so the linear combine step does not matter.
With $T(n) = 2T(n/2) + n$ และ $T(1) = 0$, what is $T(16)$?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
T(2) = 2, T(4) = 8, T(8) = 24, T(16) = 2(24) + 16 = 64 = 16 log2 16.
The recurrence $T(n) = 4T(n/2) + n$ solves to —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Four calls on half the input: n^(log2 4) = n squared, which outgrows the linear combine step.
Big-O with witnesses
- $f(n) = O(g(n))$ means there are constants $c$ and $n_0$ with $f(n) \le c\,g(n)$ for every $n \ge n_0$. The pair $(c, n_0)$ is called the witness 证据常数.
- For $3n^2 + 5n + 7 \le 4n^2$ we need $n^2 \ge 5n + 7$. This is false at $n = 6$ ($36 < 37$) and true from $n = 7$ ($49 \ge 42$). So $c = 4$, $n_0 = \mathbf{7}$ proves the upper half of $\Theta$.
Smallest whole $n_0$ with $3n^2 + 5n + 7 \le 4n^2$ for all $n \ge n_0$?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$n^2 - 5n - 7 \ge 0$ first holds at $n = 7$: $49 - 35 - 7 = 7$.
Worked example: the master theorem's three cases
- $T(n) = aT(n/b) + f(n)$: compare $f$ with $n^{\log_b a}$. For $3T(n/2) + n$ the leaves cost $n^{\log_2 3}$, approximately $n^{1.585}$, which grows faster than $n$. This is case 1, $\Theta(n^{\log_2 3})$: Karatsuba multiplication.
- For $2T(n/2) + n^2$ the top level costs $n^2$, more than the leaves' $n$. This is case 3, $\Theta(n^2)$. When the two are equal, as in merge sort, case 2 adds a factor $\log n$.
Exponent of $n$ in the solution of $T(n) = 3T(n/2) + n$ (three decimals)?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$\log_2 3 = 1.585$; the leaves dominate.
$T(n) = 2T(n/2) + n^2$ เป็น $\Theta(n^2 \log n)$.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The combine work $n^2$ dominates $n^{\log_2 2} = n$: case 3 gives $\Theta(n^2)$.
Marks that slip away
- Keeping the constants. $3n^2 + 5n + 7$ is $\Theta(n^2)$: the $3$ and the lower terms stop mattering as $n$ grows.
- Reading Big-O as the exact cost. $O(n^2)$ is only an upper bound, so a linear algorithm is $O(n^2)$ too. Say $\Theta$ when you mean both bounds.
- Timing one size. One run says nothing about growth. Double $n$ and read the ratio.
Recap
- Big-O 大O记号 bounds above, Big-Ω below, Big-Θ both; drop constants and lower-order terms.
- Halving gives $\log_2 n$; doubling $n$ multiplies the count by $2^k$ for a $\Theta(n^k)$ algorithm.
- $T(n) = 2T(n/2) + n$ is $\Theta(n \log n)$; $T(n) = T(n/2) + 1$ is $\Theta(\log n)$.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้