{
  "id": "greedy-algorithm",
  "title": "Greedy Algorithm",
  "lang": "en",
  "source_lang": "en",
  "summary": "A greedy algorithm constructs a solution through locally preferred choices, yielding an optimum when the problem’s structure makes those choices globally safe.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "Greedy method",
    "Greedy strategy"
  ],
  "generated_at": "2026-11-03",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/greedy-algorithm/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/greedy-algorithm/",
      "json": "https://aiwiki.page/zh/greedy-algorithm.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "A greedy algorithm is an algorithm that builds a solution by repeatedly making the choice that appears best according to a specified rule, without systematically reconsidering earlier choices. It is widely used in mathematical optimization, where the aim is to maximize or minimize an objective subject to constraints. Greedy methods produce optimal solutions for some problems, provably approximate solutions for others, and merely plausible solutions when no guarantee is known. Their effectiveness depends on the relationship between the local selection rule and the problem’s global structure."
    },
    {
      "heading": "Basic principle",
      "text": "A greedy method usually maintains a partial solution and repeatedly performs three operations:\n\n- Identify the available choices.\n- Select the choice ranked best by a local criterion.\n- Commit to that choice if it satisfies the relevant constraints.\n\nFor a problem involving selection from a finite set, a common template is:\n\nsolution ← empty set\norder candidates by the greedy criterion\n\nfor each candidate in that order:\n    if adding candidate preserves feasibility:\n        add candidate to solution\n\nreturn solution\n\nThis template is not universal. Some algorithms repeatedly merge objects, update priorities, or finalize provisional values rather than simply add elements to a set. The essential feature is commitment to locally preferred decisions rather than a systematic exploration of alternative decision sequences.\n\n“Best” is defined by the algorithm, not by a universal measure. A greedy rule might prefer the earliest finishing task, the cheapest eligible edge, the highest value per unit of weight, or the smallest currently known distance. Different rules for the same problem can have very different results.\n\nA feasible solution is not necessarily optimal. Nor is a maximal solution necessarily a maximum one: a maximal collection cannot be enlarged while preserving feasibility, whereas a maximum collection has the greatest possible size."
    },
    {
      "heading": "Conditions for optimality",
      "text": "Two properties commonly appear in explanations of exact greedy algorithms.\n\nGreedy-choice property\n\nA problem has the greedy-choice property for a specified rule when some optimal solution is consistent with the choice made by that rule. After making the choice, the algorithm must leave a residual problem for which the same reasoning remains valid.\n\nThis is a substantive mathematical claim. The fact that a choice looks attractive, or produces good results on examples, does not establish it.\n\nOptimal substructure\n\nA problem has optimal substructure when an optimal solution can be assembled from optimal solutions to appropriately defined subproblems.\n\nOptimal substructure alone does not justify a greedy algorithm. Many problems have this property but require dynamic programming, which compares multiple alternatives rather than committing immediately to one. The additional issue is whether the greedy choice can safely eliminate the other alternatives.\n\nProof techniques\n\nSeveral recurring arguments establish that a greedy rule is safe:\n\n- Exchange argument: Start with an optimal solution and replace one of its decisions with the greedy decision, showing that feasibility and objective value are not worsened. Repeat the argument on the remaining problem.\n- Stays-ahead argument: Compare successive greedy decisions with those of another solution and show that the greedy construction never falls behind according to a suitable measure.\n- Structural safety property: Establish a theorem identifying choices that can always belong to an optimal solution, such as a minimum-weight edge crossing an appropriate cut in a graph.\n\nThese arguments often become inductive proofs. In program verification, the corresponding safety claim may be expressed as a loop invariant, such as “the selected edges can be extended to a minimum spanning tree.”"
    },
    {
      "heading": "Representative examples",
      "text": "Interval scheduling\n\nIn the unweighted interval scheduling problem, each task occupies a time interval, and the objective is to select as many mutually non-overlapping tasks as possible on one resource.\n\nAn optimal greedy rule repeatedly selects the compatible task that finishes earliest. If tasks occupy half-open intervals \\([s_i,f_i)\\), a task can follow another whenever its start time is at least the previous task’s finish time.\n\nThe exchange argument considers the first task in an optimal schedule. Replacing it with the earliest-finishing task cannot reduce the time available for later tasks. The remaining selection problem has the same form.\n\nSorting tasks by finish time takes \\(O(n\\log n)\\) time, followed by a linear scan. If the tasks are already sorted, the scan takes \\(O(n)\\).\n\nOther plausible rules fail in general. Choosing the shortest task or the task that starts earliest need not maximize the number selected. The weighted version, where tasks have different rewards, also does not generally admit the same rule; it is commonly solved by dynamic programming.\n\nMinimum spanning trees\n\nA minimum spanning tree of a connected, undirected, weighted graph connects all vertices with minimum total edge weight and contains no cycles.\n\nTwo classic greedy methods are:\n\n- Kruskal’s algorithm: Process edges in nondecreasing weight order and add an edge if it joins two previously disconnected components.\n- Prim’s algorithm: Grow one tree by repeatedly adding a minimum-weight edge connecting the tree to a vertex outside it.\n\nTheir correctness follows from structural properties of spanning trees. In particular, an appropriately chosen minimum-weight edge crossing a cut is safe to add. Equal edge weights may permit multiple optimal trees, but do not invalidate the methods.\n\nDijkstra’s algorithm\n\nDijkstra’s algorithm solves the single-source shortest-path problem in graphs with nonnegative edge weights. It repeatedly finalizes the unprocessed vertex with the smallest tentative distance.\n\nNonnegativity makes the commitment safe: a later path through a more distant unprocessed vertex cannot decrease the distance of the vertex just finalized. Negative edge weights remove this guarantee. Algorithms such as the Bellman–Ford algorithm handle negative weights under different conditions.\n\nWith an appropriate priority queue, Dijkstra’s method efficiently selects the next vertex as distance estimates change.\n\nHuffman coding\n\nHuffman coding constructs an optimal binary prefix code for symbols with given frequencies under the standard objective of minimizing weighted codeword length. It repeatedly combines the two least-weight objects into a new object whose weight is their sum.\n\nThe local step is justified by the existence of an optimal code tree in which two least-weight symbols occupy sibling leaves at maximum depth. Combining them reduces the problem to a smaller instance.\n\nThis illustrates a greedy method in which the candidate set changes during construction, rather than being processed in one fixed order.\n\nFractional knapsack\n\nIn the fractional knapsack problem, item \\(i\\) has value \\(v_i\\), positive weight \\(w_i\\), and a selectable fraction \\(x_i\\). The objective is\n\n\\[\\max \\sum_{i=1}^{n} v_i x_i\\]\n\nsubject to\n\n\\[\\sum_{i=1}^{n} w_i x_i \\leq W, \\qquad 0 \\leq x_i \\leq 1.\\]\n\nFor nonnegative values, choosing items in nonincreasing order of value density \\(v_i/w_i\\), taking as much as possible of each, is optimal.\n\nThe corresponding 0–1 knapsack problem requires \\(x_i\\in\\{0,1\\}\\), so items cannot be divided. The same rule can then fail. With capacity \\(50\\) and items of weight–value pairs \\((10,60)\\), \\((20,100)\\), and \\((30,120)\\), density order selects the first two for value \\(160\\), while the last two give value \\(220\\)."
    },
    {
      "heading": "Structural theory: matroids",
      "text": "A matroid provides a precise setting in which a broad class of greedy selection methods is guaranteed to work.\n\nA finite matroid consists of a ground set \\(E\\) and a family \\(\\mathcal{I}\\) of independent subsets satisfying:\n\n- The empty set belongs to \\(\\mathcal{I}\\).\n- Every subset of an independent set is independent.\n- If \\(A,B\\in\\mathcal{I}\\) and \\(|A|<|B|\\), some element of \\(B\\setminus A\\) can be added to \\(A\\) while preserving independence.\n\nThe third condition is an exchange property. For nonnegative element weights, processing elements in nonincreasing weight order and accepting each one whenever independence is preserved produces a maximum-weight independent set.\n\nConversely, among finite hereditary set systems, correctness of this algorithm for every assignment of nonnegative weights characterizes matroids. This explains why superficially similar selection problems may differ sharply in their suitability for greedy methods.\n\nForests in an undirected graph form the independent sets of a graphic matroid, connecting this theory to spanning-tree algorithms. Matroids do not encompass every successful greedy algorithm; interval scheduling, for example, is justified by a different exchange structure."
    },
    {
      "heading": "Approximation and heuristic use",
      "text": "Failure to find an exact optimum does not make a greedy method useless. An approximation algorithm provides a proven bound on how far its result can be from optimal. A heuristic may be useful in practice without such a bound.\n\nFor the unweighted set cover problem, a standard greedy algorithm repeatedly selects the set covering the greatest number of uncovered elements. For a coverable universe of \\(n\\) elements, its solution uses at most \\(H_n\\) times as many sets as an optimum, where\n\n\\[H_n=\\sum_{k=1}^{n}\\frac{1}{k}\\leq 1+\\ln n.\\]\n\nGreedy heuristics also appear in scheduling, routing, feature selection, and search. A nearest-neighbor rule for the traveling salesperson problem, for example, constructs a route quickly but need not produce the shortest tour.\n\nAn approximation guarantee belongs to a particular algorithm under particular assumptions. It cannot be inferred merely from the label “greedy.”"
    },
    {
      "heading": "Efficiency and implementation",
      "text": "Greedy algorithms are often efficient because they avoid enumerating complete solutions, but greedy design does not imply any particular time complexity.\n\nCommon costs include:\n\n- Sorting candidates, often \\(O(n\\log n)\\).\n- Maintaining a heap or another priority queue.\n- Testing whether a proposed choice preserves feasibility.\n- Updating candidate scores after each selection.\n\nThe choice of data structure can therefore dominate performance. Kruskal’s algorithm, for example, uses efficient component tracking, while Huffman coding repeatedly extracts minimum-weight objects.\n\nTie-breaking may affect which solution is returned. When the correctness proof permits arbitrary ties, all such outcomes retain the guarantee. For an unproven heuristic, tie-breaking can substantially affect quality."
    },
    {
      "heading": "Relationship to other algorithmic approaches",
      "text": "Greedy algorithms differ from several neighboring methods:\n\n- Dynamic programming retains and compares solutions to multiple subproblems; a greedy method commits to a locally selected alternative.\n- Divide and conquer splits a problem into subproblems and combines their solutions, without necessarily using greedy choices.\n- Backtracking revisits earlier decisions when exploring alternatives; a basic greedy construction does not.\n- Local search usually starts with a complete solution and improves it through nearby modifications, whereas a constructive greedy method typically builds a solution incrementally.\n\nGreedy selection can be one component of a larger algorithm that also uses search, randomization, or improvement steps."
    },
    {
      "heading": "Historical development",
      "text": "Greedy reasoning predates its recognition as a unified algorithm-design paradigm. Several canonical examples emerged in the mid-twentieth century: Huffman coding was published in 1952, Kruskal’s spanning-tree algorithm in 1956, Prim’s presentation in 1957, and Dijkstra’s shortest-path algorithm in 1959. Some spanning-tree ideas had earlier antecedents.\n\nSubsequent work on combinatorial optimization, especially matroid theory, clarified when repeated local choices necessarily yield a global optimum. This distinction between a provably safe choice and an intuitively attractive one remains central to the analysis of greedy algorithms."
    }
  ],
  "links": [
    "algorithm",
    "mathematical-optimization",
    "dynamic-programming",
    "loop-invariant",
    "dijkstra-algorithm",
    "shortest-path-problem",
    "bellman-ford-algorithm",
    "priority-queue",
    "huffman-coding",
    "prefix-code",
    "knapsack-problem",
    "approximation-algorithm",
    "heuristic",
    "time-complexity",
    "data-structure",
    "divide-and-conquer"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}