Skip to content
DSA Visualizerpreview

0/1 knapsack

Add the items one row at a time; each cell chooses between leaving the item and taking it with less room.

O(items × capacity) time · O(items × capacity) space

Items (weight:value)1:1, 3:4, 4:5, 5:7capacity 7

Items (weight, value): 1kg $1, 3kg $4, 4kg $5, 5kg $7. Capacity 7. With no items, every capacity is worth 0.

View
Table of best values: rows are items added so far, columns are capacities 0 to 7; 0 cells filled.

Table of best values: rows are items added so far, columns are capacities 0 to 7; 0 cells filled.

  • compare
  • swap or write
  • settled

1 / 34

Speed

Space play · ← → step · Home End jump · drag the strip to scrub