← Course home

Problem-solving methods · Lesson 44 of 52

Greedy methods

Evaluate a locally best choice and justify it with a structural proof.

Full lessonAbout 10 min

You will learn to state a greedy decision rule and distinguish examples from a correctness proof.

Recommended first: Comparison sorts.

On this page 6 sections

The idea

Intervals (1, 3), (2, 4), and (3, 5) yield 2 compatible intervals.

A greedy algorithm makes a local choice and does not take it back. The choice needs an argument that some optimal solution agrees with it. interval_scheduling_max_count on (1, 3, a), (2, 4, b), and (3, 5, c) returns 2. The rule sorts by finish time and takes the next interval that starts at or after the previous finish. Interval a finishes first and is taken. b overlaps a and is skipped. c starts when a finishes and is taken. An example with three intervals does not prove the rule. The exchange argument does. Huffman coding is another greedy choice. huffman_codes on frequencies a 3, b 1, and c 1 gives a the code 1, the short code, because a is the frequent symbol. The two rare symbols share the longer branch. The library builds that tree by repeatedly combining the two lightest weights with a min-heap.

A worked trace

StepStateWhat changed
takecount = 1Take interval a. The count is 1.
skipcount = 1Skip interval b. The count is 1.
takecount = 2Take interval c. The count is 2.

Why it is correct

Suppose an optimal schedule's first interval is not the one that finishes first. Swap it for the earliest-finishing interval. The swap stays feasible because that interval finishes even sooner, so everything that fit after the old first interval still fits. The count does not drop. Repeating the swap produces the greedy schedule without losing optimality. That is the proof. A single worked example only shows the rule on one input. The Huffman exchange argument says the two lightest symbols can be siblings in some optimal tree. Combining them and repeating builds the codes.

A common mistake

One worked example is not a proof. The exchange argument is the proof.

What it costs

Sorting the intervals is n log n, and the scan that follows is linear. Huffman does n heap operations, also n log n, and the output codes are linear in the number of symbols. Extra memory is the sorted copy or the heap. Do not quote the bound until you can say why the local choice is safe. A different sort key, such as shortest interval, needs its own argument or a counterexample. Sorting the intervals is Θ(n log n) time and uses Θ(n) extra memory.

Run it

python
from bitsandbytes.greedy.classic import interval_scheduling_max_countprint(interval_scheduling_max_count([(1, 3, 'a'), (2, 4, 'b'), (3, 5, 'c')]))# prints: 2 

Practice and next step

Open Dynamic programming next. Dynamic programming stores a state so the same subproblem is solved once.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.greedy.classic

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Take the interval that finishes first

Intervals (1, 3), (2, 4), and (3, 5) yield two compatible intervals. The frequent symbol gets the short Huffman code.

count
1

State

  1. 0achosen
  2. 11chosen

Take interval a. The count is 1.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1Interval scheduling sorts by finish time and takes the next compatible interval. What justifies that first choice?
2Two lightest symbols become siblings in a Huffman tree. Why is that safe?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.