Knapsack DP Visualizer

Visualize 0/1 knapsack dynamic programming, inspect each table cell, and backtrack the selected items that create the best value.

Items
4
Capacity
5
Best value
2800
Weight
5

Item input

Step controls

Step 1 of 26: Start with an empty DP table: 4 items and capacity 5.

DP table

Start with an empty DP table: 4 items and capacity 5.

Current Take item
Item / cap012345
0000000
Laptop000000
Headphones000000
Camera000000
Jacket000000

Selected items

Laptop
Weight 3, value 2000
Camera
Weight 2, value 800

Item list

ItemWeightValue
Laptop32000
Headphones1250
Camera2800
Jacket2500

What is a Knapsack DP Visualizer?

A knapsack DP visualizer shows how the 0/1 knapsack dynamic programming table is filled. Each cell answers the best value possible using the first i items and a capacity limit.

This tool lets you paste item names, weights, and values, then inspect the DP table and selected items after backtracking.

For the full cluster of related tools, browse the Data Structure Visualizers hub.

How to use this knapsack visualizer

  • Paste one item per line with name, weight, and value.
  • Set the maximum capacity.
  • Click Build table.
  • Step through DP cells or run to the end.
  • Inspect the selected items and total value.

The visualizer uses 0/1 knapsack rules, so each item can be taken at most once.

Knapsack and dynamic programming

Dynamic programming stores answers to overlapping subproblems. In 0/1 knapsack, the table compares skipping an item against taking it when capacity allows.

If you are studying recursion before DP, try the Recursion Tree Visualizer. If you are comparing table-style algorithms with graph algorithms, use the Dijkstra Algorithm Visualizer.

Frequently Asked Questions

What input format does the knapsack visualizer use?
Use one item per line: item name, weight, and value. For example: `Laptop 3 2000`.
What is 0/1 knapsack?
0/1 knapsack means each item is either selected once or not selected at all.
Does this tool show the selected items?
Yes. After filling the table, it backtracks from the final cell to list the selected items.
What does capacity mean?
Capacity is the maximum total weight allowed in the knapsack.

Related tools