{
    "slug": "greedy_algorithms",
    "term": "Greedy Algorithms",
    "category": "algorithms",
    "difficulty": "intermediate",
    "short": "Algorithms that make the locally optimal choice at each step — fast and simple, but only produce the globally optimal solution for problems with the greedy choice property.",
    "long": "A greedy algorithm selects the best available option at each step without reconsidering previous choices. It works when: the greedy choice is safe (local optimum leads to global optimum) and the problem has optimal substructure. Classic examples: coin change (if denominations allow), interval scheduling (sort by end time), Huffman coding, Kruskal/Prim for minimum spanning trees. Fails for: general coin change (e.g. coins {1,3,4} — greedy for 6 gives {4,1,1} not optimal {3,3}).",
    "aliases": [
        "greedy choice",
        "local optimum"
    ],
    "tags": [
        "algorithms",
        "optimisation"
    ],
    "misconception": "Greedy algorithms always find the optimal solution — they only work when the greedy choice property holds; many problems (knapsack, TSP) require dynamic programming or backtracking.",
    "why_it_matters": "Greedy algorithms are O(n log n) or faster — recognising when a greedy approach is correct avoids the complexity of dynamic programming for problems that don't need it.",
    "common_mistakes": [
        "Applying greedy to problems where it doesn't hold — coin change with arbitrary denominations requires DP.",
        "Not sorting before applying greedy — most greedy algorithms require the input in a specific order.",
        "Confusing greedy with DP — if a problem has overlapping subproblems, DP is required; if choices are independent, greedy may work.",
        "Not proving the greedy choice property — assuming greedy works without verifying leads to subtly wrong solutions."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "dynamic_programming",
        "backtracking",
        "big_o_notation",
        "sorting_algorithms"
    ],
    "prerequisites": [
        "dynamic_programming",
        "big_o_notation",
        "sorting_algorithms"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Greedy_algorithm"
    ],
    "bad_code": "// Greedy coin change fails for non-standard denominations:\nfunction greedyChange(int $amount, array $coins): array {\n    rsort($coins); // Sort descending\n    $result = [];\n    foreach ($coins as $coin) {\n        while ($amount >= $coin) { $result[] = $coin; $amount -= $coin; }\n    }\n    return $result;\n}\n// greedyChange(6, [1,3,4]) returns [4,1,1] — 3 coins\n// Optimal: [3,3] — 2 coins. Greedy fails here.",
    "good_code": "// DP coin change — always optimal:\nfunction dpChange(int $amount, array $coins): int {\n    $dp = array_fill(0, $amount + 1, PHP_INT_MAX);\n    $dp[0] = 0;\n    for ($i = 1; $i <= $amount; $i++) {\n        foreach ($coins as $coin) {\n            if ($coin <= $i && $dp[$i - $coin] !== PHP_INT_MAX) {\n                $dp[$i] = min($dp[$i], $dp[$i - $coin] + 1);\n            }\n        }\n    }\n    return $dp[$amount] === PHP_INT_MAX ? -1 : $dp[$amount];\n}",
    "quick_fix": "Greedy works when a locally optimal choice always leads to a global optimum — verify this property before using greedy; use dynamic programming when subproblems overlap",
    "severity": "low",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/greedy_algorithms",
        "html_url": "https://codeclaritylab.com/glossary/greedy_algorithms",
        "json_url": "https://codeclaritylab.com/glossary/greedy_algorithms.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Greedy Algorithms](https://codeclaritylab.com/glossary/greedy_algorithms) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/greedy_algorithms"
            }
        }
    }
}