Help
What is the 0/1 Knapsack Problem?
The 0/1 Knapsack Problem is a classic optimization problem where you have a knapsack with a fixed weight capacity and a set of items, each with a weight and value. The goal is to select items to maximize the total value without exceeding the capacity. Each item can only be included once (0 or 1 times).
How Does the Algorithm Work?
This solver uses dynamic programming to find the optimal solution:
- Table Creation: Creates a 2D table where rows represent items and columns represent capacities from 0 to the knapsack capacity
- Filling the Table: For each cell (i, w), determines if including item i gives more value than excluding it
- Formula: dp[i][w] = max(dp[i-1][w], dp[i-1][w-weight[i]] + value[i])
- Backtracking: Traces back through the table to find which items were selected
- Time Complexity: O(n × W) where n is the number of items and W is the capacity
Understanding the DP Table
The DP table shows how the algorithm builds up the solution:
- Rows: Each row represents considering items 0 through i
- Columns: Each column represents a possible knapsack capacity
- Cell Value: Maximum value achievable with given items and capacity
- Decision: Each cell decides whether to include or exclude the current item
- Bottom-Right Cell: Contains the maximum possible value
Step-by-Step Mode
Use step-by-step mode to understand how the algorithm fills the table:
- Next Step: Advance to the next cell calculation
- Previous Step: Go back to review previous calculations
- Auto Play: Automatically step through the algorithm
- Finish: Complete the remaining steps instantly
Example Applications
- Resource Allocation: Selecting projects with budget constraints
- Investment: Choosing investments to maximize returns
- Cargo Loading: Maximizing value of shipped goods
- Task Selection: Choosing tasks to maximize productivity