Knapsack Simulator
Simulate 0/1 Knapsack using dynamic programming tabulation and Fractional Knapsack using greedy approach.
Configuration
Weights Array (w)
Profits Array (p)
Narrative & Setup
In 0/1 Knapsack, you cannot break items; you either take the whole item or leave it. This is typically solved using dynamic programming tabulation. The grid below is initialized with zeros in the first row (no items) and first column (0 capacity). Click 'Play' or 'Step Forward' below to start the step-by-step tabulation simulation.
Where:
- K[i][w]: Max profit for first
iitems with capacityw(Current Cell) - K[i-1][w]: Value when Item
iis excluded (Cell Above) - K[i-1][w - w[i]]: Lookback cell value when Item
iis included (Cell to Left)
Tabulation Matrix
| Items \ Capacity (w) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| 0 (No Item) | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Item 1wt: 2 | val: 3 | 0 | — | — | — | — | — | — | — |
Item 2wt: 3 | val: 4 | 0 | — | — | — | — | — | — | — |
Item 3wt: 4 | val: 5 | 0 | — | — | — | — | — | — | — |
Item 4wt: 5 | val: 6 | 0 | — | — | — | — | — | — | — |
Note: Row 0 and Column 0 are filled with 0. The highlighted cell (Cyan) compares the value directly above it (Amber, representing exclusion) and the value at remaining capacity in the previous row (Emerald, representing inclusion of the item).
About This Simulation
This knapsack simulator solves both versions of the knapsack problem side by side: 0/1 knapsack with a dynamic programming table, and fractional knapsack with the greedy value-per-weight method.
Features
- Enter your own items (weight and value) and knapsack capacity
- 0/1 knapsack DP table filled cell by cell
- Backtracking through the table to find the chosen items
- Fractional knapsack with items sorted by value/weight ratio
- Final maximum value and selected items for both methods
Explanation
The knapsack problem asks which items to put in a bag of limited capacity to get the highest total value. In 0/1 knapsack each item is taken whole or not at all, which needs dynamic programming. In fractional knapsack you may take part of an item, so a greedy choice is optimal.
How it works
- 0/1 knapsack: build a table dp[i][w] = the best value using the first i items with capacity w.
- If item i weighs more than w, dp[i][w] = dp[i−1][w]. Otherwise dp[i][w] = max(dp[i−1][w], value[i] + dp[i−1][w − weight[i]]).
- The answer is dp[n][W]. Trace back from that cell: if dp[i][w] differs from dp[i−1][w], item i was taken.
- Fractional knapsack: sort items by value/weight ratio from highest to lowest.
- Take whole items in that order while they fit, then take the fraction of the next item that fills the remaining capacity.
Why it matters
Knapsack is the classic example for comparing dynamic programming with greedy algorithms, and it models real problems such as budget allocation, cargo loading and resource selection. It is a standard CSE221 exam topic.
Frequently Asked Questions
What is the time complexity of 0/1 knapsack using DP?
O(n × W), where n is the number of items and W the capacity. The table has n + 1 rows and W + 1 columns, and each cell takes constant time. This is pseudo-polynomial because it depends on the value of W.
Why doesn't the greedy method work for 0/1 knapsack?
Taking the item with the best value/weight ratio first can leave unused capacity that a different combination would fill better. Because items cannot be split, only dynamic programming (or brute force) guarantees the best answer.
What is the time complexity of fractional knapsack?
O(n log n) for sorting the items by value/weight ratio, then O(n) to fill the knapsack.
What is the difference between 0/1 knapsack and fractional knapsack?
In 0/1 knapsack each item is either taken completely or left out, and it is solved with dynamic programming. In fractional knapsack items can be split, and the greedy ratio method gives the optimal answer.
Topics covered
- 0/1 Knapsack
- Fractional Knapsack
- Dynamic Programming
- Tabulation (Bottom-Up DP)
- Greedy Algorithms
- Optimal Substructure
Tags
- knapsack
- dp table
Related Simulations
Algorithms
Searching Algorithms
Run linear, binary, ternary and quaternary search on your own array and step through the code one line at a time.
Algorithms
Sorting Algorithm Visualiser
Visualise sorting algorithms
Algorithms
A* Algorithm
Show pathfinding using cost, heuristic, and total estimated distance.
Algorithms
BFS (Breadth First Search) Simulator
Build interactive graphs and watch BFS explore networks level-by-level using a visual queue.

