{
  "id": "knapsack-problem",
  "title": "Knapsack Problem",
  "lang": "en",
  "source_lang": "en",
  "summary": "The knapsack problem asks how to select items with given weights and values to maximize total value without exceeding a capacity limit.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "Rucksack problem",
    "Knapsack optimization problem"
  ],
  "generated_at": "2026-11-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/knapsack-problem/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/knapsack-problem/",
      "json": "https://aiwiki.page/zh/knapsack-problem.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "The knapsack problem is a problem in mathematical optimization: given items with specified weights and values, select a collection whose total weight does not exceed a capacity and whose total value is as large as possible. Its name evokes packing a bag with limited carrying capacity. The standard 0–1 knapsack problem allows each item to be selected at most once; other variants allow repeated items, divisible items, multiple bags, or several resource constraints. It is a central example in computer science because closely related versions range from efficiently solvable problems to computationally difficult ones."
    },
    {
      "heading": "Mathematical formulation",
      "text": "For \\(n\\) items, let item \\(i\\) have weight \\(w_i\\) and value \\(v_i\\), and let the capacity be \\(W\\). The standard 0–1 formulation is\n\n\\[\\begin{aligned} \\text{maximize}\\quad & \\sum_{i=1}^{n} v_i x_i\\\\ \\text{subject to}\\quad & \\sum_{i=1}^{n} w_i x_i \\leq W,\\\\ & x_i\\in\\{0,1\\}\\qquad (i=1,\\ldots,n). \\end{aligned}\\]\n\nThe variable \\(x_i\\) indicates whether item \\(i\\) is selected. This is a special case of integer programming, with one capacity constraint and binary decision variables.\n\nWeights need not represent physical mass: they may represent cost, storage consumption, processing time, or another limited resource. Values may represent profit, utility, priority, or expected benefit.\n\nThe conventional algorithmic formulation assumes positive integer weights and nonnegative integer values, with numbers encoded in binary. Rational inputs can also be considered, but running times that depend on numerical magnitudes must then be interpreted carefully. An item heavier than the capacity can be discarded. In the ordinary model, a zero-weight item with positive value can be selected immediately.\n\nFor example, suppose the capacity is \\(50\\):\n\nItem | Weight | Value | Value-to-weight ratio\nA | 10 | 60 | 6\nB | 20 | 100 | 5\nC | 30 | 120 | 4\n\nChoosing items in decreasing value-to-weight order selects A and B, with total value \\(160\\). The optimal selection is B and C, with total value \\(220\\). This illustrates why a locally attractive choice need not produce the best indivisible selection."
    },
    {
      "heading": "Principal variants",
      "text": "0–1, bounded, and unbounded knapsack\n\nIn 0–1 knapsack, each item is either selected or omitted.\n\nIn bounded knapsack, item type \\(i\\) is available in a specified quantity \\(b_i\\), so\n\n\\[x_i\\in\\{0,1,\\ldots,b_i\\}.\\]\n\nIn unbounded knapsack, any nonnegative integer number of copies may be taken. Positive weights ensure that only finitely many copies fit within a finite capacity. Bounded instances can be transformed into 0–1 instances by splitting quantities into groups, commonly using a binary grouping technique.\n\nAlthough these variants resemble one another, they require different recurrence relations or update orders in memory-efficient implementations.\n\nFractional knapsack\n\nIn fractional knapsack, each item may be divided:\n\n\\[0\\leq x_i\\leq 1.\\]\n\nThe optimal solution is obtained by sorting items by decreasing \\(v_i/w_i\\), taking whole items while possible, and then taking the fraction of the next item that fills the remaining capacity. This greedy algorithm takes \\(O(n\\log n)\\) time with comparison sorting.\n\nIts correctness depends on divisibility: replacing a quantity of lower-ratio material with an equal weight of higher-ratio material cannot decrease value. This exchange argument does not apply to indivisible items.\n\nMultiple constraints and multiple bags\n\nIn multidimensional knapsack, each item consumes several resources. The constraints take the form\n\n\\[\\sum_{i=1}^{n} w_{ji}x_i\\leq W_j, \\qquad j=1,\\ldots,d.\\]\n\nFor example, a project might consume both money and staff time. A capacity-indexed dynamic program then typically has a state space depending on the product of the capacities, which can become prohibitively large.\n\nIn multiple knapsack, items are assigned to several bags, each with its own capacity, and each item can ordinarily be assigned to at most one bag. This differs from multidimensional knapsack: several containers are not the same as several simultaneous resource limits on one selection.\n\nOther extensions impose group choices, dependencies, conflicts between items, or nonlinear values. Such additions can substantially change the available algorithms and approximation guarantees."
    },
    {
      "heading": "Computational complexity",
      "text": "The optimization problem asks for a maximum-value feasible selection. Its decision version asks whether there is a selection satisfying both\n\n\\[\\sum_i w_i x_i\\leq W \\quad\\text{and}\\quad \\sum_i v_i x_i\\geq V,\\]\n\nwhere \\(V\\) is a target value.\n\nFor binary-encoded integer inputs, this decision problem is NP-complete, and the optimization problem is NP-hard. A proposed selection can be checked efficiently, but no polynomial-time exact algorithm for all instances is known. Such an algorithm would resolve the P versus NP problem in favor of \\(P=NP\\).\n\nThe standard problem is weakly NP-complete, reflecting its dependence on numerical magnitudes. It has a pseudo-polynomial-time algorithm whose running time is polynomial in \\(n\\) and \\(W\\), but not necessarily in the length of the binary input. Representing \\(W\\) requires only \\(O(\\log W)\\) bits; iterating through all capacities up to \\(W\\) can therefore require exponentially many steps relative to that representation.\n\nThe closely related subset sum problem asks whether some input numbers sum exactly to a target \\(B\\). It can be expressed as knapsack by setting each item's weight and value equal to its input number, using capacity \\(B\\), and asking for value at least \\(B\\)."
    },
    {
      "heading": "Exact algorithms",
      "text": "Exhaustive search and meet-in-the-middle\n\nEnumerating all subsets examines \\(2^n\\) possibilities. This is practical only for relatively small \\(n\\), though pruning can reduce the work on particular instances.\n\nA meet-in-the-middle algorithm splits the items into two groups and enumerates the weight–value pairs achievable in each group. After removing dominated pairs and sorting, it combines compatible choices from the two groups. Its running time is roughly \\(O(2^{n/2})\\), apart from polynomial factors, with substantial memory requirements.\n\nThis approach is useful when the number of items is moderate but the capacity is too large for a capacity-indexed table.\n\nDynamic programming by capacity\n\nFor integer weights, dynamic programming gives an exact method. Define \\(D(i,c)\\) as the maximum value attainable using the first \\(i\\) items with capacity \\(c\\). Then\n\n\\[D(i,c)= \\begin{cases} D(i-1,c), & w_i>c,\\\\[4pt] \\max\\!\\left(D(i-1,c),\\,D(i-1,c-w_i)+v_i\\right), & w_i\\leq c. \\end{cases}\\]\n\nThe boundary condition is \\(D(0,c)=0\\), and the answer is \\(D(n,W)\\).\n\nThere are \\(O(nW)\\) states, each requiring constant work under the usual arithmetic model. Thus the time complexity is \\(O(nW)\\). Storing the complete table uses \\(O(nW)\\) space; retaining only the information needed for the optimal value reduces the space complexity to \\(O(W)\\).\n\nA one-dimensional implementation updates capacities in descending order:\n\ndp[0 ... W] = 0\nfor each item i:\n    for c from W down to w[i]:\n        dp[c] = max(dp[c], dp[c - w[i]] + v[i])\n\nDescending order prevents the current item from being used more than once. Updating capacities in ascending order instead permits repeated use of that item and implements the corresponding unbounded-knapsack recurrence.\n\nRecovering the selected items requires additional bookkeeping, a retained table, or a reconstruction procedure; an optimal-value array alone does not directly record the selection.\n\nDynamic programming by value\n\nWhen values are small integers but weights are large, the roles of the two quantities can be reversed. Let \\(M(i,p)\\) be the minimum weight needed to achieve total value exactly \\(p\\) using the first \\(i\\) items. Impossible states have weight \\(+\\infty\\).\n\nThe recurrence is\n\n\\[M(i,p)= \\min\\!\\left( M(i-1,p), M(i-1,p-v_i)+w_i \\right),\\]\n\nwith the second term omitted when \\(p<v_i\\). The answer is the largest \\(p\\) such that \\(M(n,p)\\leq W\\).\n\nIf \\(P=\\sum_i v_i\\), the basic algorithm takes \\(O(nP)\\) time. This formulation also provides a foundation for approximation by scaling values.\n\nBranch-and-bound and optimization solvers\n\nBranch-and-bound explores partial selections while discarding branches that cannot improve the best known feasible solution. Solving the fractional relaxation supplies an upper bound on the value obtainable from a branch.\n\nPractical integer-programming solvers may combine such bounds with preprocessing, cutting planes, and specialized search. They can solve many substantial instances, but performance depends strongly on instance structure and does not remove the worst-case complexity."
    },
    {
      "heading": "Approximation algorithms",
      "text": "The standard 0–1 problem admits efficient approximation even though exact optimization is NP-hard.\n\nA simple approximation algorithm first discards items heavier than the capacity and sorts the remaining items by value-to-weight ratio. It considers the whole-item prefix before the first item that would exceed capacity, and compares that prefix with the highest-value feasible single item. The better choice has value at least half the optimum.\n\nThe guarantee follows from the fractional relaxation: its value is at most the prefix value plus the full value of the first item that does not fit. Both quantities are bounded by the two candidate solutions being compared.\n\nMore strongly, 0–1 knapsack has a fully polynomial-time approximation scheme, or FPTAS. For any \\(0<\\varepsilon<1\\), it returns a feasible solution of value at least\n\n\\[(1-\\varepsilon)\\operatorname{OPT},\\]\n\nin time polynomial in the input length and \\(1/\\varepsilon\\).\n\nOne standard construction discards infeasible items, lets \\(V_{\\max}\\) be the largest remaining value, and sets\n\n\\[K=\\frac{\\varepsilon V_{\\max}}{n}, \\qquad \\widehat v_i= \\left\\lfloor\\frac{v_i}{K}\\right\\rfloor.\\]\n\nIf all feasible values are zero, the instance is trivial. Otherwise, dynamic programming solves the instance with scaled values exactly. Rounding loses less than \\(K\\) per selected item, so the total loss is at most\n\n\\[nK=\\varepsilon V_{\\max}\\leq\\varepsilon\\operatorname{OPT}.\\]\n\nA straightforward implementation takes \\(O(n^3/\\varepsilon)\\) time under the standard arithmetic model. More advanced schemes improve this bound. These guarantees apply to the standard single-constraint problem and do not automatically carry over to its extensions."
    },
    {
      "heading": "Applications and modeling limits",
      "text": "Knapsack models arise in budget allocation, cargo selection, project selection, storage management, and resource-constrained scheduling. They also appear as subproblems inside larger optimization methods.\n\nThe basic model assumes that item values add independently and that resource consumption is known. It does not directly capture interactions such as two projects becoming valuable only when selected together, uncertain future costs, or priorities that cannot be represented by one numerical objective. Dependencies and multiple resources require explicit extensions.\n\nKnapsack is related to, but distinct from, bin packing. Knapsack usually selects a valuable subset for limited capacity; bin packing normally assigns all items while minimizing the number of containers.\n\nKnapsack-like problems have also been used in cryptography. Early proposals included public-key systems based on structured subset-sum instances. Several prominent early constructions were broken: the difficulty of general knapsack instances does not establish the security of a particular structured cryptographic construction."
    }
  ],
  "links": [
    "mathematical-optimization",
    "computer-science",
    "integer-programming",
    "greedy-algorithm",
    "np-completeness",
    "np-hardness",
    "p-versus-np-problem",
    "dynamic-programming",
    "time-complexity",
    "space-complexity",
    "branch-and-bound",
    "approximation-algorithm",
    "cryptography"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}