CS With NehalCS With Nehal

Knapsack Simulator

Simulate 0/1 Knapsack using dynamic programming tabulation and Fractional Knapsack using greedy approach.

Configuration

Weights Array (w)

Item 1
Item 2
Item 3
Item 4

Profits Array (p)

Item 1
Item 2
Item 3
Item 4

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.

DP State Transition Formula
K[i][w] = max( K[i-1][w], p[i] + K[i-1][w - w[i]] )

Where:

  • K[i][w]: Max profit for first i items with capacity w (Current Cell)
  • K[i-1][w]: Value when Item i is excluded (Cell Above)
  • K[i-1][w - w[i]]: Lookback cell value when Item i is included (Cell to Left)

Tabulation Matrix

Items \ Capacity (w)01234567
0 (No Item)00000000
Item 1wt: 2 | val: 3
0———————
Item 2wt: 3 | val: 4
0———————
Item 3wt: 4 | val: 5
0———————
Item 4wt: 5 | val: 6
0———————
History: 1 / 28
Animation Speed — 1000ms

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

  1. 0/1 knapsack: build a table dp[i][w] = the best value using the first i items with capacity w.
  2. 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]]).
  3. 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.
  4. Fractional knapsack: sort items by value/weight ratio from highest to lowest.
  5. 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

Acknowledgements

The Person Behind This Simulation

This simulation would not have been possible without his valuable contributions and support. We sincerely appreciate his time, effort, and dedication to the project.

Rahib Kibria

Rahib Kibria

  • 3rd Year CS Undergraduate Student | BRAC University