To calculate the ratio in a knapsack problem, divide each item’s value by its weight: value-to-weight ratio = item value ÷ item weight. The result is often called value density or profit-to-weight ratio and measures how much value one unit of the constrained resource provides.

in knapsack problem how to calculate the ratio
How to Calculate the Ratio in a Knapsack Problem

What the knapsack ratio measures

In the usual maximization setup, an item has a value, such as its benefit, profit, score, or payoff, and a weight, meaning the capacity it consumes. The value and weight must belong to the same candidate item, and the weight must be positive. Dividing value by weight places the items on a common “value per unit of weight” basis, making it easier to compare candidates before checking whether they fit together.

The ratio is a per-item measure, not a measure of the final solution. It does not directly show the total value of a subset, the capacity that will remain, or whether two choices can coexist. A high-ratio item may leave a gap that no remaining item can fill, while a lower-ratio item may complete a higher-value subset. This distinction is central to 0/1 knapsack.

Calculate and rank each item’s ratio

  1. List every candidate once. Record its label, value, and weight. Do not exclude an item simply because its ratio looks poor; the combination of items matters.
  2. Divide value by weight. For an item worth 50 with weight 5, the calculation is 50 ÷ 5 = 10 value units per weight unit. Keep the value and weight units consistent so the resulting ratios are comparable.
  3. Retain the unrounded value for comparison. Ratios with decimals are valid. Displaying rounded ratios can make the list easier to read, but ordering should ideally use the full values so near-equal ratios are not separated incorrectly.
  4. Arrange the items from highest ratio to lowest. This creates a value-density ranking and identifies candidates that appear efficient before capacity and combination constraints are applied.
  5. Check feasible combinations. Starting with the ranking can help organize the analysis, but each proposed subset must still fit the capacity and contain each item at most once.

Ratio ranking is often a useful first pass in a 0/1 exercise, but it is not itself a selection algorithm. Two attractive items may not fit together, and one moderate-ratio item may fit the leftover capacity after the other choices are considered. Treat the order as a heuristic or an explanatory comparison, then evaluate feasible subsets or use an exact method. If the problem allows an item to be split, the procedure is different; the guide to solving the fractional knapsack problem step by step explains that case.

Why the same ratio rule behaves differently by model

The phrase “take the highest ratios first” has different consequences depending on what the model permits. The decisive issue is whether an item is indivisible and whether the objective concerns one capacity, several containers, or another constraint.

Model Item treatment Role of the value-to-weight ratio Selection method
0/1 knapsack Each item is selected once or excluded. Useful for comparison, but it does not guarantee the maximum-value subset. Compare feasible subsets or use an exact 0/1 dynamic program.
Fractional knapsack A portion of an item may be taken. Descending ratio provides the standard greedy order. Take items by descending ratio, allowing a partial final item.
Multiple knapsack or bin packing The model uses several containers or a placement objective. A single ratio does not define the complete solution. Use a model designed for those containers and constraints.

Google’s OR-Tools knapsack documentation defines the 0/1 objective as maximizing the value of a selected subset without exceeding capacity. The Stanford CS161 dynamic-programming notes describe the item-by-capacity table used to evaluate the exact 0/1 model.

Run the exact 0/1 knapsack calculation

For indivisible items, the Knapsack Problem Calculator can solve the bounded 0/1 model directly instead of treating ratio order as a final decision rule. It finds the highest-value subset for one capacity and reports the selected items, total value, total weight, and unused capacity.

  1. Enter the capacity. Use a whole-number capacity from 1 through 10,000.
  2. Add the items. Enter each one in label, weight, value format. Labels must be unique and cannot contain commas. Weights must be positive whole numbers no greater than the capacity. Values must be nonnegative, and decimal values are allowed. The list can contain up to 100 items.
  3. Run the exact 0/1 solve. Every listed item is available at most once, so it can be selected once or excluded. It cannot be split or repeated.
  4. Inspect the result. Review the selected labels, total value, total weight, unused capacity, and stated assumptions. Confirm that the selected weight does not exceed the entered capacity.
  5. Copy the selection if needed. The result can be copied as plain text for a classroom solution, bounded portfolio example, or small allocation draft.

The solver uses exact dynamic programming: for each item and compatible capacity state, it compares the best value obtained without the item with the best compatible value obtained by including it. It then backtracks through the decisions to reconstruct the selected labels. Equal-value ties retain the earlier solution rather than switching arbitrarily, so repeated input produces a deterministic result. The calculation stays in the browser.

Because the method creates one state per capacity unit, capacity and weights must be whole numbers. Measurements with fixed decimals should be converted to consistent smaller units before entry, and a finer scale creates a larger table. This product supports the exact single-capacity 0/1 model rather than fractional packing, multiple knapsacks, or bin packing.

Interpret the result within its model

An exact result means globally optimal for the entered bounded 0/1 model: one capacity, whole-number weights, nonnegative values, and each item used at most once. It does not mean that the selection is physically or operationatively ideal. The calculator returns the chosen subset and totals rather than a ratio ranking, so use separately calculated ratios to explain or challenge the result, not as a replacement for the exact selection.

  • Physical constraints are outside the model. It does not account for dimensions, volume, balance, fragility, or other packing conditions.
  • Relationships are outside the model. Mandatory groups, incompatible pairs, deadlines, uncertainty, and item-specific risks are not evaluated.
  • Some inputs are rejected. Blank fields, duplicate labels, nonpositive weights, weights above capacity, non-finite values, and malformed comma-separated rows are not accepted.
  • The claim has a defined scope. For financial portfolios, safety loading, medical resource allocation, or other high-stakes decisions, validate the objective, dependencies, constraints, and real-world consequences with an appropriate domain model and accountable reviewer.

These boundaries do not make the result wrong; they identify what “optimal” means. The solver is exact for its stated scalar 0/1 model, while the ratio calculation is exact arithmetic for comparing individual items.

Use the ratio for the job it can do

Ratio is best treated as a transparent comparison metric. It helps show that one candidate provides more value per unit of weight than another, and it supplies the ordering rule for the fractional knapsack model. For a 0/1 problem, use the ratios to explain your reasoning, identify potentially strong candidates, and highlight combinations worth inspecting; then let an exact dynamic program determine which indivisible items maximize total value.

A sound final check asks four questions: Are value and weight defined consistently? Were all ratios compared without unnecessary rounding? Does the proposed subset actually fit? Have any real-world constraints been excluded from the claim? In short, calculate value ÷ weight, but do not confuse the best individual ratio with the best feasible subset. The ratio explains efficiency; the exact 0/1 solver answers the separate optimization question of which items should be selected.

If you're weighing options, How to Calculate Ratio of 4 Numbers covers this in detail.