The Knapsack Problem (背包问题) is a classic optimization problem in computer science and mathematics. It involves a set of items, each with a weight and a value, and a knapsack with a maximum weight capacity. The goal is to determine the most valuable combination of items to include in the knapsack without exceeding its weight capacity.
Variants of the Knapsack Problem
0/1 Knapsack Problem
In this version, each item can either be included in the knapsack or excluded; you can’t take a fraction of an item. Formally, given n items with weights w1, w2, ..., wn and values v1, v2, ..., vn, and a knapsack with capacity W, the problem is to maximize the total value V while ensuring that the total weight W′ dose not exceed W.
Mathematical formulation: Maximize: $$ V=\sum_{i=1}^n v_i x_i $$ Subject to: $$ \sum_{i=1}^n w_i x_i \le W $$ Where xi is 0 or 1, indicating whether item i is included.
Fractional Knapsack Problem
In this version, you can take fractions of items. This allow for a greedy algorithm to achieve the optimal solution by taking items in decreasing order of their value-to-weight ratio until the knapsack is full. Mathematical formulation: Maximize: $$ V=\sum_{i=1}^{n}v_i x_i $$ Subject to: $$ \sum_{i=1}^n w_i x_i \le W $$ Where $0 x_i $, indicating the fraction of item i included.
Solution Approaches
Dynamic Programming
Used primarily for the 0/1 knapsack problem. It builds up a table of solutions to subproblems to find the optimal solution.
In the context of the 0/1 kanpsack problem, the transition equation helps build the dynamic programming solution. This equation is used to determine the maximum value achieveable by either including or excluding an item based on previous solutions. Let’s delve into the specifics.
Dynamic Programming Transition Equation for 0/1 Knapsack Problem
For the 0/1 knapsack problem, the transition equation determines the maximum value dpi, w achievable using the first i items with a maximum weight capacity w. The equation can be expressed as: dpi, w = max(dpi − 1, w, dpi − 1, w − wi + vi) Where: - dpi, w is the maximum value achievable with the first i items and capacity w. - vi and wi are the value and weight of the i-th item. - dpi − 1, w represents the maximum value without including the i-th item. - dpi − 1, w − wi + vi represents the maximum value including the i-th item, given that w − wi must be non-negative.
Explanation: 1. Initialzation: - dp0, w = 0 for all w, meaning if no items are considered, the maximum value is zero regradless of the weight capacity. 2. Recurrence: - For each item i and each weight w - If the item’s weight wi is greater than w, the item cannot be included, so the maximum value remains dpi − 1, w. - If the item’s weight wi is less than or equal to w, consider two cases: - Exclude the item: The maximum value is dpi − 1, w. - Include the item: The maximum value is dpi − 1, w − wi + vi. - Take the maximum of these two cases.
Implementation
1 | int knapsack_DP(const vector<int>& values, const vector<int>& weights, int W) { |
Greedy Algorithm
Effective for the fractional knapsack problem. It involves selecting items based on their value-to-weight ratio until knapsack is full.
Implementation
1 | struct Item { |