Knapsack Problem Solver Calculator

Plan optimal packs using weights, values, and limits. See chosen items, totals, and utilization instantly. Download reports, explore formulas, and solve tougher cases today.

Knapsack Solver

Tip: weights are rounded to the nearest integer for exact methods.
Represents maximum allowed weight (or cost).
Choose the rule that matches your constraint.
Auto selects a suitable approach for your size.
Used only in fractional mode.
If provided, bulk list overrides the rows below.

Items

For bounded mode, set quantity limits per item.
Name Weight Value Qty limit Remove
Exact methods round weights and capacity to integer units for stability. If you need higher resolution, scale weights (e.g., grams) before solving.

Example Data Table

You can load this example into the solver using the “Load Example” button.
Item Weight Value Qty limit
Laptop320001
Camera115001
Jacket212001
Water Bottle13002
Book27001

Formula Used

0/1 knapsack (each item once)
Dynamic programming recurrence over capacity:
dp[w] = max(dp[w], dp[w - wi] + vi) for w ≥ wi
Update w from high to low to avoid reusing the same item.
Unbounded knapsack (unlimited items)
dp[w] = max(dp[w], dp[w - wi] + vi) for w ≥ wi
Update w from low to high to allow repeats.
Bounded knapsack (limited quantities)
Convert quantity qi into powers of two bundles, then solve 0/1 exactly.
qi = 13 → 1 + 4 + 8 (bundles)
Fractional knapsack (allow splitting)
Greedy by value density:
sort by (vi / wi) descending, take until capacity is full

How to Use This Calculator

  1. Enter a positive capacity value for your constraint.
  2. Select a mode: 0/1, bounded, unbounded, or fractional.
  3. Add items with weight and value, then set quantity limits if needed.
  4. Optionally paste bulk items using the provided format.
  5. Click Solve to view results above the form.
  6. Use the download buttons to export CSV or PDF reports.

Why Knapsack Modeling Matters

The knapsack model formalizes “best selection under a limit.” You maximize total value while keeping total weight within capacity. In symbols, choose xi to maximize Σ(vi·xi) subject to Σ(wi·xi) ≤ C. This calculator turns that math into a workflow for packing, budgeting, time allocation, and portfolio trade‑offs. When inputs are consistent, the output helps justify why one mix beats another, not just what the mix is.

Interpreting Capacity, Weights, and Units

Exact solvers work on integer capacities. If your weights are decimals, scale them to an integer unit (for example, kilograms → grams, or 2.75 → 275 with a ×100 scale). The same scale must be applied to capacity so comparisons stay valid. Rounding too aggressively can change borderline decisions, so choose a unit that preserves meaningful differences. A quick check is to recompute with a finer scale and confirm the chosen set remains stable.

Choosing the Right Mode for Your Scenario

Use 0/1 mode when each item is either taken or skipped. Use bounded mode when an item has a maximum quantity, such as “up to 3 spare parts.” Use unbounded mode for repeatable choices like identical boxes or standard modules. Use fractional mode when partial amounts are allowed, such as blending materials or allocating time blocks. Fractional solutions follow value density (vi/wi), so the top ratios are consumed first until capacity fills.

Understanding Solver Performance and Limits

Dynamic programming typically runs in O(n·C) time and O(C) memory, where C is the scaled capacity. With 60 items and C = 10,000, the core loop evaluates about 600,000 states, which is fast on servers. If you scale weights by ×100, C becomes 1,000,000 and the state count grows 100×, so runtime and memory rise sharply. For very large capacities, consider coarser units or the fractional mode for a quick benchmark.

Reading the Output for Better Decisions

The results section reports the best value, total weight used, and utilization percentage, plus a detailed pick list with counts or fractions. Low utilization can indicate leftover slack that could be filled by small, high-density items. Compare “value per weight” across chosen items to spot candidates for replacement. Exporting to CSV or PDF makes it easy to share assumptions, rerun scenarios, and track improvements across revisions.

FAQs

1) What is the difference between 0/1 and bounded mode?

0/1 allows each item at most once. Bounded lets you cap quantities per item, such as up to 5 units. Internally, the bounded case is converted into bundled 0/1 items to preserve an exact solution.

2) Why does the exact solver round weights?

Dynamic programming indexes states by capacity, which must be an integer. If you use decimals, scale weights and capacity (×10, ×100, ×1000) to keep precision. Rounding is only a fallback when units are already coarse.

3) When should I use fractional mode?

Choose fractional mode when you can take partial amounts, such as cutting material or allocating divisible time. It sorts items by value density (value/weight) and fills the remaining capacity greedily, producing an optimal fractional solution.

4) How many items can I solve reliably?

Item count matters less than scaled capacity. With moderate capacities (for example 1,000–50,000) and tens of items, exact modes are usually fast. If capacity becomes huge after scaling, reduce the unit size or try fractional for a baseline.

5) What does utilization mean?

Utilization is (total weight used ÷ capacity) × 100. A low number means slack remains, suggesting you may add smaller items. A high number means the plan is close to the limit and changes become more sensitive.

6) How do the CSV and PDF exports differ?

CSV is best for spreadsheets and further analysis, including sorting and charting. PDF is best for sharing a fixed report with the same tables and summaries you see on screen, ready to print or attach.

Related Calculators

Important Note: All the Calculators listed in this site are for educational purpose only and we do not guarentee the accuracy of results. Please do consult with other sources as well.