Courses / Computer Science I
Algorithmics

Greedy Choices and Counterexamples

Computer Science I 187 words Free to read

A greedy algorithm builds a solution step by step, making the locally best choice at each turn without ever reconsidering. While simple and fast, a series of local optimizations does not always yield a globally optimal result.

ApproachBehaviorRisk / Benefit
GreedyGrabs immediate best stepFast, but can fail globally
OptimalMaximizes overall resultGuaranteed best, often slower

Some problems allow greedy solutions. Making change with standard coins, minimizing lateness, and building minimum spanning trees are proven optimal via exchange arguments or matroids.

A minimum spanning tree, built by always taking the cheapest edge left

When Greedy Fails

Greedy often fails, and the definitive proof is a counterexample. Consider making change for 30 using coins of sizes c{1,15,20}c \in \{1, 15, 20\}.

The greedy choice picks 20 first, leaving 10, and takes ten 1sts for a total of 11 coins. The optimal solution uses two 15s, totaling just 2 coins.

Common pitfall: Assuming a locally optimal choice guarantees a global optimum. Never assume correctness; you must mathematically prove it, or a single counterexample will demolish your algorithm.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

10practice questions
2interactive scenes

Algorithmics