{"post":{"slug":"what-is-a-greedy-algorithm-2026","title":"What Is a Greedy Algorithm? A 2026 Practical Guide","excerpt":"A greedy algorithm always takes the best-looking choice right now and never looks back. Here is how that works in 2026, the classic examples, and the coin-change case where it quietly fails.","category":"code","date":"July 18, 2026","author":"ByteLedger Team","content":"<span class=\"dropcap\">A</span> greedy algorithm builds a solution one step at a time, always taking whatever choice looks best right now, and never reconsidering that choice later. No backtracking, no exploring alternatives, no cache of subproblem answers — just a sequence of locally optimal decisions. That simplicity makes greedy algorithms fast and easy to reason about, but it only produces a correct answer for a specific class of problems. Knowing which class is the entire skill.\n\n## What changed in 2026\n\n- **The core technique is unchanged**, but recognizing that a problem is greedy-shaped rather than DP-shaped continues to be one of the more commonly misjudged calls in technical interviews and real optimization code alike.\n- **Greedy heuristics remain the practical backbone of large-scale scheduling and resource allocation systems**, where a provably optimal solution is too slow to compute and a good-enough greedy approximation ships instead.\n- **Greedy-based approximation algorithms saw continued use in large routing and clustering problems**, where exact optimization does not scale and a bounded-error greedy approach is the pragmatic tradeoff.\n\n## The greedy choice property\n\nA greedy algorithm only produces a correct, optimal answer when the problem has the **greedy choice property**: a locally optimal choice at each step leads to a globally optimal solution, with no need to revisit earlier choices. Proving this formally usually uses an exchange argument, showing that any optimal solution can be rearranged to match the greedy choice without getting worse. Most textbooks skip the proof and just show the pattern; trust it for the classic problems below, and stay skeptical of a new problem until a proof or counterexample can be found.\n\n## Classic examples that work\n\n```\nCoin change (canonical denominations, e.g. US coins):\n  amount = 67\n  greedy: take the largest coin <= remaining amount, repeat\n  25 + 25 + 10 + 5 + 1 + 1 = 67   (6 coins, optimal for this coin system)\n```\n\n- **Activity selection:** given a set of activities with start and end times, pick the maximum number that do not overlap. Sorting by end time and greedily picking the next compatible activity is provably optimal.\n- **Huffman coding:** build an optimal prefix-free compression code by always merging the two least-frequent nodes first.\n- **[Dijkstra's algorithm](/blog/what-is-dijkstras-algorithm-2026):** always finalize the closest unvisited vertex next — a greedy choice that happens to be provably correct for non-negative-weight graphs.\n- **Minimum spanning tree, Kruskal or Prim:** always add the cheapest edge that does not create a cycle.\n\n## Where greedy fails\n\n```\nCoin change (non-canonical denominations):\n  coins = [1, 3, 4], amount = 6\n  greedy: take 4, then 1, then 1  = 3 coins (4+1+1)\n  optimal: take 3, then 3         = 2 coins (3+3)\n```\n\nThe greedy choice — always take the largest coin — is wrong here because taking a 4 first blocks the better 3-plus-3 combination. This is the standard demonstration that greedy is not a general-purpose technique: it needs the greedy choice property to hold, and most problems do not guarantee it without proof.\n\n## Greedy vs dynamic programming\n\n| Property | Greedy | [Dynamic programming](/blog/what-is-dynamic-programming-2026) |\n| --- | --- | --- |\n| Decisions | One pass, never revisited | Considers multiple options, keeps the best |\n| Speed | Usually O(n log n) or better | Usually O(n times states), slower |\n| Correctness | Only when greedy choice property holds | Correct whenever optimal substructure holds |\n| Memory | O(1) extra, typically | O(n) or more, for the cache or table |\n| Example | Activity selection | 0/1 knapsack |\n\nThe 0/1 knapsack problem is the sharpest illustration: greedy, always take the best value-to-weight ratio item, fails because taking an item is irreversible even when a different combination would have been better, while DP correctly considers both including and excluding each item.\n\n## Common pitfalls\n\n**Assuming greedy works without checking the greedy choice property.** This is the single biggest source of subtly wrong optimized code — it often produces a good-looking answer that is not actually optimal, and the bug only shows up on specific inputs.\n\n**Using greedy for 0/1 knapsack-style problems.** Any problem where taking an item now can block a better combination later signals that greedy will fail and DP is needed instead.\n\n**Not sorting first when the problem needs it.** Many greedy algorithms, including activity selection, Huffman coding, and interval scheduling, depend entirely on processing items in the right sorted order — skipping the sort silently breaks correctness.\n\n## FAQ\n\n**How can a problem be checked for whether it is solvable greedily?**\nLook for the greedy choice property and optimal substructure together. Without a clean proof or counterexample, test a brute-force solution against the greedy one on small random inputs before trusting it.\n\n**Is Dijkstra's algorithm a greedy algorithm?**\nYes — it is one of the most cited examples of a provably correct greedy algorithm, specifically because non-negative edge weights guarantee the locally closest vertex is genuinely part of the globally shortest path.\n\n**Can a greedy algorithm be combined with dynamic programming?**\nNot usually within the same subproblem — they represent different decision strategies. But a larger system can use greedy for one stage, such as an initial heuristic solution, and DP or exact optimization for a refinement stage.\n\n## Where to go next\n\n- [What is dynamic programming in 2026](/blog/what-is-dynamic-programming-2026)\n- [What is Dijkstra's algorithm in 2026](/blog/what-is-dijkstras-algorithm-2026)\n- [What is backtracking in 2026](/blog/what-is-backtracking-2026)","hue":320,"metaTitle":"What Is a Greedy Algorithm in 2026","metaDescription":"What is a greedy algorithm in 2026 — how locally optimal choices work, classic examples, when greedy fails, and how it compares to dynamic programming.","keywords":["greedy algorithm 2026","what is a greedy algorithm","greedy algorithm examples","greedy algorithm vs dynamic programming","when greedy algorithms fail","activity selection problem","greedy choice property","coin change greedy algorithm"],"tldr":["**A greedy algorithm always takes the locally best choice and never revisits it** — no backtracking, no cache, just one pass of decisions.","**It only produces the correct answer when the greedy choice property holds** — a locally optimal choice must guarantee a globally optimal result.","**Classic correct examples include activity selection, Huffman coding, and Dijkstras algorithm** — each has a provable greedy-choice property.","**Coin change with non-canonical denominations is the standard counterexample** — greedy picks the largest coin first and can miss the truly optimal combination.","**Skip:** trusting a greedy solution without checking the greedy choice property first — test it against brute force on small inputs before relying on it."],"readTime":"4 min read","wordCount":899},"related":[{"slug":"what-is-backtracking-2026","title":"What Is Backtracking? A 2026 Practical Guide","excerpt":"Backtracking builds a solution one choice at a time and abandons it the moment it cannot work. This 2026 guide covers the pattern, a worked N-Queens example, and when to use it instead of DP.","category":"code","date":"July 22, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":310},{"slug":"what-is-dynamic-programming-2026","title":"What Is Dynamic Programming? A 2026 Beginner Guide","excerpt":"Dynamic programming caches subproblem answers instead of recomputing them. This 2026 beginner guide walks through the two-question test and a full worked example from first principles.","category":"code","date":"July 22, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":280},{"slug":"memoization-explained-2026","title":"Memoization explained in 2026: caching results the right way","excerpt":"Memoization trades memory for speed by caching a function output keyed on its inputs. Here is the 2026 guide to how it works, when it helps, and the cache-invalidation traps that bite people.","category":"code","date":"June 29, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":290}],"sameCategory":[{"slug":"bazel-build-explained-2026","title":"Bazel Explained: What Hermetic Builds Actually Cost","excerpt":"Bazel delivers reproducible, precisely cached builds and demands that you describe everything. Here is what you get, what it costs, and who should say no.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":145},{"slug":"graphql-federation-2026","title":"GraphQL Federation: One Graph, Many Teams","excerpt":"Federation composes independently-owned subgraphs into a single API. Here is what it solves, the operational cost it adds, and when a simpler gateway is enough.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":300},{"slug":"hermetic-builds-explained-2026","title":"Hermetic Builds Explained: Same Inputs, Same Output","excerpt":"A hermetic build depends only on declared inputs. Here is what that property gives you, the common leaks that break it, and how to move toward it incrementally.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":130},{"slug":"monorepo-tooling-compared-2026","title":"Monorepo Tooling Compared: What You Actually Need","excerpt":"Monorepo tools solve task orchestration and caching, not repository layout. Here is what each tier provides and how to tell when you have outgrown the simple option.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":150}],"newer":{"slug":"what-is-a-graph-data-structure-2026","title":"What Is a Graph Data Structure? 2026 Guide","excerpt":"A graph is nodes plus edges, with no hierarchy required, unlike a tree. This 2026 guide covers directed vs undirected, adjacency lists vs matrices, and how graphs get traversed.","category":"code","date":"July 18, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":240},"older":{"slug":"what-is-a-headless-cms-2026","title":"What is a headless CMS in 2026? Content as an API","excerpt":"A headless CMS stores and serves content through an API instead of rendering pages itself. Here is how that split works in 2026, what it costs in setup, and when it is genuinely worth it.","category":"code","date":"July 18, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":200}}