The complete steps of dynamic programming to solve the backpack problem

  • Share this:
post-title
Dynamic programming is an effective method to solve the 0-1 backpack problem. This problem requires that, given a set of items and the weight of each item, find a subset so that the total weight does not exceed the capacity of the backpack, while containing as many items as possible. Recursive implementation: 1. Initialize an array dp, where dp [i] represents the total value of the previous i items. 2. For each item i, check that the conditions are met (the total weight does not exceed the backpack capacity). 3. If the condition is satisfied, add dp [i] to the result; if not, skip this item. 4. Returns the maximum value in the result array. Iterative implementation: 1. Initialize an array dp, the length is the knapsack capacity + 1. 2. Traverse all items and, for each item i, calculate the maximum value of the space remaining when item i is not included. 3. Update dp [i] to the maximum value when item i is not included plus the value when item i is included. 4. Return dp [backpack capacity] as the result.
In computer science, the knapsack problem is a classic optimization problem.

It involves putting a group of items into a backpack with a limited capacity to maximize the total value of the items in the backpack.

The 0-1 backpack problem is a special case of the backpack problem, in which each item can only be selected once (that is, either put in the backpack or not).

Dynamic programming is an efficient way to solve such problems.

By decomposing the problem into smaller sub-problems and storing the solutions of these sub-problems to avoid double counting, dynamic programming can significantly improve the efficiency of the algorithm.

Problem description.

Suppose you have a backpack with a capacity of\ (W\) and\ (n\) items, each item\ (i\) has a weight\ (w _ i\) and a value\ (v _ i\).

The goal is to determine which items should fit into the backpack so that the total value in the backpack is the largest, while not exceeding the capacity limit of the backpack.

Recursive implementation.

First, let's look at recursive implementation.

The recursive approach is based on the following idea: For each item, we have two choices: put it in the backpack or not.

If in the backpack, we need to consider the next item from the remaining capacity; if not in the backpack, we directly consider the next item.


def knapsack_recursive(weights, values, capacity, n):
    # Base case: no items left or capacity is 0
    if n == 0 or capacity == 0:
        return 0
    
    # If the weight of the nth item is more than the capacity, it cannot be included
    if weights[n-1] > capacity:
        return knapsack_recursive(weights, values, capacity, n-1)
    
    # Return the maximum of two cases:
    # (1) nth item included
    # (2) not included
    else:
        return max(values[n-1] + knapsack_recursive(weights, values, capacity - weights[n-1], n-1),
                   knapsack_recursive(weights, values, capacity, n-1))

Iterative implementation.

Next, we use the iterative approach of dynamic programming to solve this problem.

Unlike recursion, the iterative method uses a two-dimensional array dpTo store the solution of the sub-problem.

dp[i][w]Indicates the maximum value of the previous\ (i\) item when the capacity is\ (w\).


def knapsack_iterative(weights, values, capacity):
    n = len(values)
    dp = [[0 for x in range(capacity + 1)] for x in range(n + 1)]
    
    # Build table dp[][] in bottom up manner
    for i in range(n + 1):
        for w in range(capacity + 1):
            if i == 0 or w == 0:
                dp[i][w] = 0
            elif 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]

Application scenarios.

The method of dynamic programming to solve the 0-1 knapsack problem is not only suitable for theoretical algorithm research, but also widely used in practical scenarios.

For example, in the fields of resource allocation, project selection, portfolio optimization, etc., similar problem models can be used to solve the optimal solution.

In addition, this method can also be extended to more complex scenarios, such as multi-dimensional knapsack problem, constrained knapsack problem, etc.

Summarize.

Through both recursive and iterative methods, we show how to use dynamic programming to solve the 0-1 backpack problem.

The recursive method is intuitive and easy to understand, but the efficiency is low; while the iterative method, although the code is slightly more complicated, is more efficient and more suitable for processing large-scale data.

Hope this article can help you better understand and apply dynamic programming to solve practical problems.