Problem Solving

Greedy Algorithm

Students choose a large wooden token for the next step of a puzzle.

Greedy Algorithm definition

A greedy algorithm builds a solution by making the best available local choice at each step according to a stated rule.

In Spanish
algoritmo voraz
Topic
Algorithms and problem-solving

Greedy Algorithm in a picture

Greedy Algorithm diagramWith coin values 1, 3, and 4, a largest-coin-first rule gives three coins for 6, while two 3s are enough.Target amount: 6Available values: 1, 3, 4Greedy choice starts with 4Then use 1 and 1: three coinsBetter choice: 3 and 3Two coins show the greedy rule fails here
With coin values 1, 3, and 4, a largest-coin-first rule gives three coins for 6, while two 3s are enough.

How it works

Once chosen, a step is typically kept while the algorithm moves on. A greedy rule can give an optimal result when the problem has a suitable structure, but a proof is needed. Choosing the largest value now can miss a better combination later.

Worked examples

Using coin values 1, 5, and 10, what does largest-first choose for 16?

10, then 5, then 1.

Does a locally best step always yield the best complete solution?

No. The example with 1, 3, and 4 shows a failure.

What would show a greedy method works for every input?

A correctness proof that the local rule can always produce an optimal solution.

Common mistake

State the selection rule and verify its correctness. A few successful examples do not establish general optimality.

Quick check

Which choice does a greedy step make?

Can a greedy method give a nonoptimal answer?

Show answer

Yes.

What can disprove a greedy rule?

Show answer

One valid input where it misses the optimum.

Sources

Flashcard

Greedy Algorithm

Reveal the definition

A greedy algorithm builds a solution by making the best available local choice at each step according to a stated rule.

Effortless Math. “Greedy Algorithm.” Math Dictionary.