Greedy algorithms
הוצג
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 | משמעות |
|---|---|
| greedy algorithm/ˈɡriːdi ˈælɡərɪθəm/ | אלגוריתם חרדני |
| exchange argument/eksˈtʃeɪndʒ ˈɑːɡjuːmənt/ | ארגומנט החלפה |
| activity selection | |
| heuristic/hjuːˈrɪstɪk/ | A practical shortcut or rule of thumb used to solve a problem or make a decision without guaranteeing an optimal or correct result. |
| Huffman coding/ˈhʌfmən ˈkəʊdɪŋ/ | a greedy method that gives frequent symbols short codes and rare symbols long ones |
| fractional knapsack/ˈfrækʃənl ˈnæpsæk/ | a knapsack problem in which items may be split; greedy by value per weight is optimal |
| lateness/ˈleɪtnəs/ | how far a job finishes after its deadline, zero if it finishes in time |
| depth/depθ/ | The vertical distance below a surface. |
Take the best now
- A greedy algorithm 贪心算法 builds the solution piece by piece, always taking the piece that looks best now.
- Correctness needs a proof, not hope. An exchange argument 交换论证 shows that any optimal solution can be changed into the greedy one without getting worse.

The exchange argument shows —
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Swap optimal picks for greedy ones losslessly: same size, still feasible.
Activity selection, measured
- Course activities: (1,4), (3,5), (0,6), (5,7), (3,9), (5,9), (6,10), (8,11), (8,12), (2,14), (12,16). Taking the earliest finish each time picks (1,4), (5,7), (8,11), (12,16): four activities, and no choice can do better.
- The exchange argument 交换论证 once: finish($G_1$) $\le$ finish($O_1$), so replacing the optimal solution's first pick by the greedy one keeps it valid. Repeat, and the optimal solution becomes the greedy one without changing size.
Earliest-finish on the course activities picks —
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
(1,4), (5,7), (8,11), (12,16) - four, provably optimal.
Where greedy fails, and where Huffman wins
- Coins 1, 3, 4 and a target of 6: greedy pays 4 + 1 + 1 (three coins), but the dynamic programming table pays 3 + 3 (two). The exchange argument fails, so here greedy is only a rule of thumb (a heuristic 启发式方法).
- Huffman coding 哈夫曼编码 on frequencies 5, 9, 12, 13, 16, 45 keeps merging the two smallest. It needs 224 bits against 300 for a fixed-length code, and a leaf-swap argument proves that 224 is the minimum.
Coins 1, 3, 4 for target 6: greedy pays — coins
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Greedy takes 4+1+1 = three coins; the DP optimum is 3+3 = two.
With coins $1$, $3$ וגם $4$, always taking the largest coin that fits gives the fewest coins.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
למען $6$, greedy pays $4 + 1 + 1$ (three coins), but $3 + 3$ uses two. No exchange argument holds for this coin set, so you need dynamic programming.
When greedy is provably right
- In the fractional knapsack 分数背包 you may take part of an item, and greedy by value per unit of weight is optimal.
- Capacity $7$, items (weight, value) $(1,1), (3,4), (4,5), (5,7)$ have ratios $1, 1.33, 1.25, 1.4$. Take all of $(5,7)$, then $2/3$ of $(3,4)$: value $7 + 2.67 = 9.67$.
- In the 0/1 version, where items cannot be split, the optimum is $9$, and greedy by ratio gets only $8$. Greedy works when an exchange argument holds; when it does not, use dynamic programming.
Fractional knapsack, capacity 6, items (weight, value) (2, 6), (3, 6), (4, 4). What is the best total value?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Ratios 3, 2, 1: take all of (2,6) and (3,6), then 1/4 of (4,4) for 1 more: 6 + 6 + 1 = 13.
For which problem is greedy by value per weight always optimal?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Only when items can be split does the best ratio always belong in the answer.
Earliest deadline first
- Jobs with lengths and deadlines run one at a time. The lateness 延迟 of a job is how far past its deadline it finishes. Sorting by deadline, whatever the lengths, minimises the largest lateness.
- Jobs (length, deadline), already in deadline order:
| Job | Length | Deadline | Finish | Late by |
|---|---|---|---|---|
| 1 | 3 | 6 | 3 | 0 |
| 2 | 2 | 8 | 5 | 0 |
| 3 | 1 | 9 | 6 | 0 |
| 4 | 4 | 9 | 10 | 1 |
| 5 | 3 | 14 | 13 | 0 |
| 6 | 2 | 15 | 15 | 0 |
- Only job 4 is late, by 1, so the maximum lateness is $\mathbf{1}$. An exchange argument shows that swapping two neighbouring jobs that are out of order never makes it larger.
Jobs (length, deadline) (3,6), (2,8), (1,9), (4,9), (3,14), (2,15) in deadline order: maximum lateness?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Finish times 3, 5, 6, 10, 13, 15; only the (4,9) job is late, by 1.
To minimise the largest lateness, schedule the shortest jobs first.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Earliest deadline first is optimal; job length does not decide the order.
Worked example: how many rooms?
- Lectures (0,3), (1,4), (2,5), (3,6), (4,7) each need a room. At time 2.5 three lectures run at once, so at least 3 rooms are needed: this number is the depth 深度.
- Greedy by start time, reusing any room that has become free, uses exactly 3. A lower bound that greedy reaches is a proof that greedy is optimal.
Lectures (0,3), (1,4), (2,5), (3,6), (4,7): fewest rooms?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Three overlap at time 2.5; greedy by start time uses 3.
Marks that slip away
- Greedy without a proof. With coins 1, 3 and 4, greedy pays $6$ with three coins; the best uses two.
- The wrong greedy key. For activity selection 活动选择 take the earliest finish, not the earliest start or the shortest activity.
- Merging the largest in Huffman. Always merge the two smallest weights; that puts rare symbols deepest.
Recap
- Greedy takes the piece that looks best now; an exchange argument 交换论证 proves it optimal.
- Earliest finish selects $4$ activities on the course set, provably the most.
- Huffman merges the two smallest frequencies: $224$ bits against $300$.
מה השתבש? רק צוות האתר רואה זאת.