Skip to content

AKR-2803/knapsack-visualizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

0/1 Knapsack Visualizer

An interactive visualizer to understand how 0/1 Knapsack problem in Dynamic Programming by filling the DP table cell by cell.

UI Screenshot


What is the 0/1 Knapsack Problem?

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.


Dynamic Programming Approach

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.


How to Use the Visualizer

  • 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)

Practice 0/1 Knapsack Problems

Here are a few practice problems based on 0/1 Knapsack:

Furthermore, you may refer knapsack article on cp-algorithms


Contact

If you have feedback or suggestions, feel free to connect on LinkedIn

About

A visualizer for 01 knapsack dynamic programming problems

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published