0/1 Knapsack Solver

Choose the best set of items to pack within a weight limit using dynamic programming.

Runs in your browser. Your files never leave your device. See how to check

…

How to do it

  1. 1
    Step 1: Open 0/1 Knapsack Solver and add your input

    Type your numbers or text, add your file, or pick the options you need. Nothing is uploaded: everything stays in your browser.

  2. 2
    Step 2: What 0/1 Knapsack Solver does

    Choose the best set of items to pack within a weight limit using dynamic programming.

  3. 3
    Step 3: Read the result and compare

    The result appears at once. Change one value to see how the answer moves, then keep the figure that fits your case.

  4. 4
    Step 4: Copy or save it

    Copy the result or download it. For an important decision, confirm it with the official source or an adviser.

About this tool

Enter one item per line as name, weight, value, and the capacity. The solver finds the combination with the largest total value that fits, using each item at most once, and lists the items chosen with their total weight.

Method

Dynamic programming fills a table best[i][w] = the largest value using the first i items within weight w. The answer is the last cell, and the chosen items are found by tracing back.

Notes

Weights must be whole numbers. The result is exactly optimal, unlike a greedy value-per-weight choice, which can miss it.

Worked example

With a capacity of 12 and the default five items the best choice is Stove, Food and Water: weight 12 and value 145.

Tips for best results

Check every box before you rely on a result, because one wrong digit changes the whole answer. Use the exact figures from your payslip, bill, certificate or contract, and keep a note of the date you calculated. Rates, thresholds and rules change from time to time, so for tax, fees and legal matters treat the output as an estimate and confirm with the official source named on this page. You can run as many scenarios as you like; nothing is saved or sent, and refreshing the page clears the boxes.

Quick facts

Embed this calculator on your site

Copy this code to show the calculator on your own page. A link back to shobfree.com is included.

Frequently asked questions

What does 0/1 mean?

Each item is either taken once or left.

Is greedy good enough?

Not always; DP finds the true optimum.

Can weights be decimals?

No, whole numbers only.

How large can the capacity be?

Up to 5000, with up to 60 items.

Guides for this tool

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages