Why greedy works

RoadmapsDSA

Overview

A greedy algorithm builds a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. It works when this local, short-term optimization inevitably leads to a global, long-term optimum.

When applicable, greedy algorithms are exceptionally fast and simple to implement, usually avoiding complex state management or recursion.

Where used: Minimum Spanning Trees, Shortest path finding, Data compression, Job sequencing

Why learn this

Common mistakes

Recall questions

Understanding checks

How does the exchange argument justify the greedy choice in interval scheduling?

If an optimal schedule doesn't pick the interval ending earliest, you can swap that early-ending interval in place of the first interval in the optimal schedule. Because the greedy choice ends earlier, the swap frees up more room and cannot conflict with the rest of the schedule.

This proves that picking the earliest ending interval never forecloses a better outcome.

If a problem has optimal substructure but lacks the greedy-choice property, can you use a greedy algorithm?

No, you usually need dynamic programming instead.

Without the greedy-choice property, a locally optimal choice might lead you down a suboptimal path, requiring you to explore multiple branches.

Practice tasks

Mental exchange argument

Think of Kruskal's algorithm. If an optimal spanning tree does not include the cheapest edge in the graph, how could you use an exchange argument to prove it should?

Continue learning

Previous: Minimum Spanning Tree (Kruskal)

Next: Why greedy fails

Next: Interval scheduling

Return to DSA Roadmap