The 0/1 Knapsack Problem is a fundamental problem in combinatorial optimization. The task is to determine the most valuable subset of items to pack into a knapsack of limited capacity, ensuring that the total weight does not exceed the limit. The "0/1" aspect means that each item can either be included or excluded, but not partially.
- Dynamic Programming Approach: Efficiently calculates the optimal solution for the knapsack problem.
- Traceback Mechanism: Displays which items are selected for the best solution.
- ANSI Terminal Colors: Adds colored output to improve readability in the terminal.
- Signature Graphic: A fun, personalized signature graphic is displayed at the end of the program output.
- You are given a set of
nitems, each with a weight and a value. - You also have a knapsack with a limited capacity
W(i.e., the total weight it can carry). - The objective is to maximize the total value of the items in the knapsack without exceeding the capacity.
n: Number of items.W: Maximum capacity of the knapsack.w[]: Array of weights for the items.v[]: Array of values for the items.
- The optimal value that can be carried in the knapsack.
- The list of selected items that provide the optimal value.
Dynamic Programming (DP) provides an efficient solution to the 0/1 Knapsack Problem by building up a solution incrementally. We solve the problem by breaking it down into smaller sub-problems and storing the results to avoid recalculating them.
-
DP Table Creation:
- Create a 2D DP table
dp[n + 1][W + 1]where each entrydp[i][j]represents the maximum value that can be obtained using the firstiitems and a knapsack capacity ofj.
- Create a 2D DP table
-
Filling the DP Table:
- For each item
i(from 1 ton), and for each capacityj(from 0 toW):- If the current item's weight
w[i - 1]is less than or equal to the current capacityj, you have two choices:- Exclude the item: The value remains the same as the previous row,
dp[i-1][j]. - Include the item: Add the item's value
v[i - 1]to the value of the remaining capacityj - w[i - 1], i.e.,dp[i-1][j - w[i - 1]] + v[i - 1].
- Exclude the item: The value remains the same as the previous row,
- Take the maximum of the two options and store it in
dp[i][j].
- If the current item's weight
- For each item
-
Backtracking to Find Selected Items:
- After filling the DP table, the maximum value can be found in
dp[n][W]. - To determine which items were selected in the optimal solution:
- Start from
dp[n][W]and backtrack. - If
dp[i][W] != dp[i - 1][W], then itemiwas included in the solution. - Decrease the remaining capacity by the weight of the included item and continue until you reach the first item.
- Start from
- After filling the DP table, the maximum value can be found in
-
Result:
- Print the selected items and the total value that can be carried in the knapsack.
- Efficient Computation: The dynamic programming solution efficiently computes the maximum value that can be carried in the knapsack, with a time complexity of
O(n * W), wherenis the number of items andWis the capacity. - Optimal Substructure: The solution is built by solving smaller sub-problems, which is characteristic of dynamic programming approaches.
- Traceable Decisions: Using the DP table, it is easy to backtrack and find the exact items included in the optimal solution.
Here’s a preview of the algorithm's output in action:
To better understand the 0/1 Knapsack Problem and Dynamic Programming, you can refer to the following resources:
- GeeksforGeeks: 0/1 Knapsack Problem
- MIT OpenCourseWare: Dynamic Programming
- YouTube: Dynamic Programming - Knapsack Problem
If you discover a security vulnerability in the project or the implementation of the Knapsack Algorithm, please email Abdullah Al Raimi at abdullah@syalux.com. All security vulnerabilities will be promptly addressed.
This project is licensed under the MIT license.
