
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program for 0-1 Knapsack Problem
Knapsack is a classic problem that involves decision-making based on constraints. Suppose we have a knapsack with fixed capacity, and items with fixed values and sizes (it might be volume of the item or weight).
We need to add items into the knapsack such that we pack the maximum value items. From the available combinations, we need to provide an optimized solution. There are three types of knapsack problems
- 0-1 Knapsack: We can either pick an item or not (0 or 1).
- Fractional Knapsack: We can pick a part of an item.
- Unbounded Knapsack: We can choose items multiple times.
We can solve the 0-1 Knapsack problem using a simple recursive function, but this takes more time as the number of items increases. Using the dynamic programming technique, we can solve this problem faster.
0-1 Knapsack Problem Statement
You are given weights (w[i]) and values (v[i]) of n items. You need to fill a bag of capacity W with these items such that the total value is maximized. However, the weight of the items placed inside should not exceed the bag's capacity. Each item can be chosen only once.
Recursive (Brute-force) Solution
Here we will solve the 0-1 Knapsack problem using a recursive function that explores all possible combinations of including or excluding each item. This checks every choice to find the one that gives the maximum value.
Steps for the Recursive Approach:
- If no items are left or capacity becomes 0, return 0.
- If the weight of the current item is more than the capacity of the Knapsack, skip the item.
- Otherwise, calculate the maximum value by including the current item or excluding it.
- Return the maximum of these two values.
Example
Following is a Python program that solves the 0-1 Knapsack problem using a recursive function.
In the recursive function, we return the maximum value possible and solve sub-problems such as "no items left or, capacity is exhausted", "weight of an item is more than the capacity of the bag".
# Returns the maximum value that can be stored in the bag def knapSack(W, wt, val, n): # Base condition: no items left or no remaining capacity if n == 0 or W == 0: return 0 # If weight of nth item is more than capacity, skip it if wt[n-1] > W: return knapSack(W, wt, val, n-1) # Return the maximum of two cases, nth item included or, excluded else: return max( val[n-1] + knapSack(W - wt[n-1], wt, val, n-1), knapSack(W, wt, val, n-1) ) # Test input val = [50, 100, 150, 200] wt = [8, 16, 32, 40] W = 64 n = len(val) print(knapSack(W, wt, val, n))
Following is the output obtained -
350
Dynamic Programming (Bottom-Up)
The recursive approach solves the same sub-problems multiple times. Dynamic Programming avoids this by storing the results of sub-problems in a 2D table and building the solution step by step.
Steps for the Dynamic Programming Approach:
- Create a table K[n+1][W+1] initialized to 0.
- Iterate through each item and each capacity from 0 to W.
- If the current item's weight is less than or equal to the current capacity, set K[i][w] as the max of including or excluding the item.
- Else, carry forward the previous value without including the item.
- After filling the table, the value at K[n][W] is the result.
Example
In this example, the table K[i][w] represents the maximum value achievable using the first i items with a total weight limit w. We iteratively fill this table to find the solution -
# Dynamic Programming approach def knapSack(W, wt, val, n): # Create a 2D table to store results of subproblems K = [[0 for _ in range(W + 1)] for _ in range(n + 1)] # Build the table K[][] in bottom-up manner for i in range(n + 1): for w in range(W + 1): if i == 0 or w == 0: K[i][w] = 0 elif wt[i-1] <= w: K[i][w] = max(val[i-1] + K[i-1][w - wt[i-1]], K[i-1][w]) else: K[i][w] = K[i-1][w] return K[n][W] # Test input val = [50, 100, 150, 200] wt = [8, 16, 32, 40] W = 64 n = len(val) print(knapSack(W, wt, val, n))
We get the output as shown below -
350

All the variables are declared in the local scope, and their references are seen in the figure above.
Applications of Knapsack
This is applicable in situations where we need to allocate resources to available tasks (non-divisible) under a fixed set of constraints, such as time, budget, etc.
It is also used in selecting projects for investment to maximize returns, and in cargo loading problems where we try to carry the most valuable items without exceeding weight limits.