Knapsack Problem Visualization

Introduction to the Knapsack Problem

The Knapsack Problem is a classic optimization problem: given a set of items, each with a weight and a value, determine which items to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.

There are two main variants of the problem:

0/1 Knapsack

Items cannot be broken down – you either take an item completely or not at all.

This variant requires dynamic programming for an optimal solution.

Fractional Knapsack

Items can be divided into smaller pieces – you can take a fraction of an item.

This variant can be solved optimally using a greedy approach.

0/1 Knapsack (DP)
Fractional Knapsack (Greedy)
Comparison

0/1 Knapsack Problem

In the 0/1 Knapsack Problem, each item must be either taken completely or left behind. This problem cannot be solved optimally using a greedy approach and requires dynamic programming.

Please enter a valid capacity (minimum 1)

Generate random items and set a knapsack capacity, then click "Solve with DP" to find the optimal solution.

Algorithm Explanation

The dynamic programming approach for the 0/1 Knapsack problem:

  1. Create a 2D array dp[i][w] where i is the number of items considered and w is the current capacity
  2. For each item and each possible capacity, decide whether to include the item or not
  3. The value in dp[n][W] will be the maximum possible value (where n is the number of items and W is the total capacity)
function knapsack01(weights, values, capacity):
    n = length of weights array
    dp = new table of size (n+1) x (capacity+1)
    for i from 0 to n:
        for w from 0 to capacity:
            if i == 0 or w == 0:
                dp[i][w] = 0
            else if weights[i-1] <= w:
                dp[i][w] = max(values[i-1] + dp[i-1][w-weights[i-1]], dp[i-1][w])
            else:
                dp[i][w] = dp[i-1][w]
    return dp[n][capacity]

Time Complexity

O(n × W) - Where n is the number of items and W is the capacity

Space Complexity

O(n × W) - For the DP table

Fractional Knapsack Problem

In the Fractional Knapsack Problem, items can be divided into smaller pieces. This allows for a simpler greedy approach to find the optimal solution.

Generate random items and set a knapsack capacity, then click "Solve with Greedy" to find the optimal solution.

Algorithm Explanation

The greedy approach for the Fractional Knapsack problem:

  1. Calculate the value/weight ratio for each item
  2. Sort items by their value/weight ratio in descending order
  3. Take items with the highest ratio first, using fractions of items when needed
function fractionalKnapsack(items, capacity):
    for each item in items:
        item.ratio = item.value / item.weight
    sort items by ratio descending
    remainingCapacity = capacity
    totalValue = 0
    for each item in items:
        if remainingCapacity <= 0:
            break
        if item.weight <= remainingCapacity:
            item.selected = 1
            remainingCapacity -= item.weight
            totalValue += item.value
        else:
            fraction = remainingCapacity / item.weight
            item.selected = fraction
            totalValue += item.value * fraction
            remainingCapacity = 0
    return totalValue

Time Complexity

O(n log n) - Due to sorting items by ratio

Space Complexity

O(n) - Only need to store the original items

Comparison

See how the same set of items is handled using the 0/1 Knapsack (DP) and Fractional Knapsack (Greedy) approaches.

Real-world Applications

0/1 Knapsack Applications

  • Capital budgeting – selecting projects to invest in
  • Cargo loading – when items cannot be divided
  • Cutting stock problems
  • Resource allocation with indivisible resources

Fractional Knapsack Applications

  • Portfolio optimization
  • Resource allocation with divisible resources
  • Cargo loading with divisible goods (e.g., liquids, grains)
  • Time management – allocating partial time to tasks