Knapsack algorithm with an additional property
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
The Knapsack problem is a classic optimization problem that has been studied extensively in computer science and operations research. It is named after the problem faced by someone who has a fixed-size knapsack (or backpack) and must decide how to fill it with the most valuable combination of items. This article will delve into a modified version of the Knapsack problem by introducing an additional property, elucidating its applications, exploring technical explanations, and providing examples.
Understanding the Basic Knapsack Problem
The basic Knapsack problem can be stated as follows:
You are given a set of items, each with a weight and a value, and a knapsack with a maximum capacity. The goal is to determine the maximum value of items that can be placed into the knapsack without exceeding its capacity.
Formally, let:
- be the number of items.
- be the weight of item .
- be the value of item .
- be the maximum weight capacity of the knapsack.
The objective is to maximize the total value while ensuring that the total weight does not exceed . This can be represented mathematically as:
Subject to:
Where represents whether item is included in the knapsack.
A Real-World Example
Consider a scenario where a hiker has a knapsack with a maximum capacity of 15 kg. The hiker has to choose which items to bring along, given the list below:
| Item | Weight (kg) | Value ($) |
| Tent | 10 | 60 |
| Water | 3 | 10 |
| Food | 2 | 20 |
| Jacket | 5 | 30 |
Using the basic knapsack algorithm, it can be computed whether the hiker should take the tent and food for a value of 80 dollars, or perhaps other combinations, to maximize the total value.
Introducing Additional Property: Durability
Let’s introduce an additional property - durability. Each item now also has a durability score, which should be maximized alongside the value. Durability reflects how long an item can be used before it becomes unusable.
Let:
- be the durability of item .
Now, the goal is to enhance the original problem by also considering the durability of items in the selection process.
Modified Objective Function
The new objective function becomes multi-objective:
Maximize:
Example with Additional Property
Using the modified problem where each item also has a durability score, depicted in the table:
| Item | Weight (kg) | ||
| Tent | 10 | 60 | 7 |
| Water | 3 | 10 | 2 |
| Food | 2 | 20 | 5 |
| Jacket | 5 | 30 | 8 |
One solution could be to select Jacket and Food which maximizes the value to 50 dollars and durability to 13 units without exceeding the capacity.
Algorithmic Approach: Dynamic Programming with Multi-Objective
To solve this modified problem, one could use a dynamic programming approach that maintains a table to reflect various states, accounting for both knapsack capacity and durability.
Steps:
- Initialization: Use a 3D DP array dp[i][j][k] where
i= items considered,j= weight, andk= durability. - Transitions:
- If item
iis not taken,dp[i][j][k] = dp[i-1][j][k]. - If item
iis taken,dp[i][j][k] = \max(dp[i-1][j - w_i][k - d_i] + v_i, dp[i][j][k]).
- Final State: Check for maximum value and corresponding durability at
dp[n][W][K](whereKis some flexibility allowed for durability).
Applications
The enhanced knapsack with durability can be applied in various real-life scenarios beyond just hiking:
- Supply Chain Management: Selecting goods not only based on value but also on shelf life.
- Survival Kits: Preparing kits that contain durable items during emergency conditions.
- Military Logistics: Selecting equipment for missions ensuring value and durability are maximized.
Summary Table
Here's a summary of the key points related to the knapsack problem with an additional durability property:
| Key Component | Description/Explanation |
| Objective | Maximize both total value and total durability |
| Constraints | Weight must not exceed the knapsack capacity |
| Variables | (binary decision), , , , |
| Algorithm | Dynamic Programming with multi-objective optimization |
| Application Examples | Supply Chain, Survival Kits, Military Logistics |
This exploration into a modified knapsack problem introduces a new layer of complexity and practicality, offering solutions that align closer with the multifaceted nature of real-world decision-making.
Related reading
- Knapsack Equation with item groups
- Knapsack how to add item type to existing solution
- Knapsack with continuous non distinct constraint
- knapsack with weight only
- Known algorithm for efficiently distributing items and satisfying minima?
- Kubernetes's http liveness probe failed when pod under heavy load
- KNN in Tensorflow - Using Graph to predict unseen data
- kNN state-of-the-art implementation

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.