CSAlgorithms & ComplexityDiagramBeginner
Greedy Algorithm
KO: 그리디 알고리즘 (탐욕법)·EN: Greedy Algorithm·JA: 貪欲法(Greedy Algorithm)
An algorithmic paradigm that makes the locally optimal choice at each step, achieving fast global optimality when the greedy-choice property holds
Table of Contents
Structure Diagram
Greedy Algorithm: Earliest-Finish-Time Activity Selection
Greedy Optimal (3 jobs)3 steps
Skipped Overlaps2 steps
STEP 1A (0-3h)(30 h)
Quick Answer
What is Greedy Algorithm?
An algorithmic paradigm that makes the locally optimal choice at each step, achieving fast global optimality when the greedy-choice property holds. It is a core concept in Algorithms & Complexity within CS.
Core Principles
A Greedy Algorithm builds a solution step by step by always picking the locally optimal choice without revisiting earlier decisions.
- When It Guarantees Global Optimality: Only when the problem has both the Greedy-Choice Property and Optimal Substructure.
- Typical Pattern: Sort candidates by a key metric (
O(N log N)) or pull from a Priority Queue, committing to each feasible choice once.
Practical Takeaways
- Always test for counterexamples: With coins
[1, 3, 4]making6, greedy picks4+1+1(3 coins) instead of optimal3+3(2 coins). Fall back to DP when greedy fails. - Pragmatic heuristic for NP-Hard problems: In bin packing, job scheduling, or ad bidding, greedy heuristics deliver fast, near-optimal production solutions in milliseconds.
- Prove via Exchange Argument: Show that swapping any non-greedy choice in an optimal solution with the greedy choice never worsens the result.
Notable Examples
- Dijkstra & Kruskal/Prim MST: Greedily expanding the lowest-cost edge or vertex.
- Huffman Coding: Assigning shortest bit codes to frequent symbols in ZIP, JPEG, and MP3 compression.
- Interval Scheduling: Selecting earliest-finishing tasks to maximize throughput.