An interactive visualizer to understand how 0/1 Knapsack problem in Dynamic Programming by filling the DP table cell by cell.
The 0/1 Knapsack problem is an optimization problem in Dynamic Programming, where we have:
- weight array
wt[] - value array
val[] - and a knapsack with capacity
W
Each item can either be taken (1) OR not taken (0)
The goal is to maximize the total value without exceeding the capacity of the knapsack.
We use a DP table where:
dp[i][j] = maximum value using first `i` items with capacity `j`
At each cell, we can either:
- Take the item (if capacity allows)
- Dont take the item
This visualizer highlights these decisions interactively.
- Enter the weight array, value array, and capacity
- Generate the DP table, and use these options:
- Fill Next Cell step-by-step execution
- Play / Pause – automatic filling
- Speed Slider – control filling animation speed
- At each cell, observe:
- 🟦 Blue cell -> current cell whose value is being computed
- 🟩 Green cell -> considered✅ (MAX of take and donttake)
- 🟥 Red cell -> rejected❌ (not MAX of take and donttake)
Here are a few practice problems based on 0/1 Knapsack:
Furthermore, you may refer knapsack article on cp-algorithms
If you have feedback or suggestions, feel free to connect on LinkedIn
