0-1 Knapsack



John Rankin @ May 10, 2023

The 0/1 Knapsack problem is a classic dynamic programming problem. It involves selecting items from a set, each with its own weight and value, to maximize the total value while keeping the total weight within a given limit.

Here's an example question:

Question: Given a list of items, each with a weight and value, and a maximum weight capacity, determine the maximum value that can be obtained by selecting a subset of items, where the sum of the weights is less than or equal to the capacity.

Now let's go through the solution using the 0/1 Knapsack pattern:

Solution:

Step 1: Understand the problem and identify the problem type.

  • The problem is about selecting items from a list, given their weights and values, to maximize the total value while respecting a weight capacity.
  • This is a classic 0/1 Knapsack problem.

Step 2: Define the problem variables.

  • weights: An array representing the weights of the items.
  • values: An array representing the values of the items.
  • capacity: The maximum weight capacity.
  • n: The total number of items.

Step 3: Formulate the recurrence relation.

  • Let's define dp as a 2D array of size (n+1) x (capacity+1), where dp[i][j] represents the maximum value that can be obtained by considering items up to index i and with a maximum capacity of j.
  • The recurrence relation is:
    dp[i][j] = max(dp[i-1][j], dp[i-1][j-weights[i]] + values[i])
    

    Here, dp[i-1][j] represents the maximum value obtained by excluding the current item, and dp[i-1][j-weights[i]] + values[i] represents the maximum value obtained by including the current item.

Step 4: Initialize the base cases.

  • When there are no items to consider (i = 0), the maximum value is always 0, regardless of the capacity (dp[0][j] = 0).
  • When the capacity is 0 (j = 0), the maximum value is always 0, regardless of the number of items (dp[i][0] = 0).

Step 5: Determine the iteration order.

  • We can iterate over the items from i = 1 to n and the capacities from j = 1 to capacity.

Step 6: Implement the solution and handle edge cases. Here's a Python implementation of the 0/1 Knapsack problem:

def knapsack(weights, values, capacity):
    n = len(weights)
    dp = [[0] * (capacity + 1) for _ in range(n + 1)]

    for i in range(1, n + 1):
        for j in range(1, capacity + 1):
            if weights[i - 1] <= j:
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weights[i - 1]] + values[i - 1])
            else:
                dp[i][j] = dp[i - 1][j]

    return dp[n][capacity]

Step 7: Test the solution with sample inputs. Now, let's test the knapsack function with a sample input:

weights = [2, 3, 4, 5]
values = [3, 4, 5, 6]
capacity = 5

print(knapsack(weights, values, capacity))

When you run this code, it will output the maximum value that can be obtained by selecting items from the given list, considering their weights and values, while respecting the weight capacity.

In this case, the output will be 8, which represents the maximum value achievable by selecting items with weights [2, 3] and values [3, 4].

Note that this implementation assumes that the weights and values lists are 0-based indexed, meaning the weight and value of the first item are weights[0] and values[0] respectively. If your lists are 1-based indexed, you'll need to adjust the indices accordingly in the implementation.




 

 

 

Most Recent Posts


Bitwise XOR-exclusive OR

Read This
May 10, 2023

0-1 Knapsack

Read This
May 10, 2023

K-Way Merge

Read This
May 10, 2023

Subsets

Read This
May 10, 2023

Backtracking

Read This
May 10, 2023

Greedy Algorithm

Read This
May 10, 2023

Trie

Read This
May 10, 2023

Depth-First Search-DFS

Read This
May 10, 2023

Breadth-First Search-BFS

Read This
May 10, 2023

Binary Search Tree-BST

Read This
May 10, 2023

Top K Elements

Read This
May 10, 2023

Binary Search Pattern

Read This
May 10, 2023

Cyclic Sort pattern

Read This
May 10, 2023

Merge Intervals

Read This
May 10, 2023

Day 7 - DynamoDB - and Working with 2 Services - Lambda

Read This
August 25, 2018

Day 6 - Lambda - Creating an API

Read This
August 23, 2018

AWS - Day 5 - S3 - Simple Storage Service

Read This
August 22, 2018

AWS - Day 4 - AWS CLI Useful Scripts

Read This
August 21, 2018

AWS - Day 3 - Create Container from another container

Read This
August 20, 2018

Day 2 - Docker Intro

Read This
August 17, 2018

AWS - Day1 - Tutorial - Launching my first Docker Container

Read This
August 16, 2018

AWS - Day 1 - Signing up and testing out their tutorials

Read This
August 16, 2018

Dynamic Programming - Edit Distance

Read This
December 19, 2016

Dynamic Programming - Fibonacci

Read This
December 19, 2016