Knapsack algorithm
optimization
dynamic programming
computer science
computational complexity

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.

Practice algorithms

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:

  • nn be the number of items.
  • wiw_i be the weight of item ii.
  • viv_i be the value of item ii.
  • WW 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 WW. This can be represented mathematically as:

Maximize i=1nvixi\text{Maximize } \sum_{i=1}^n v_i \cdot x_i Subject to:

i=1nwixiW\sum_{i=1}^n w_i \cdot x_i \leq W Where xi0,1x_i \in {0, 1} represents whether item ii 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:

ItemWeight (kg)Value ($)
Tent1060
Water310
Food220
Jacket530

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:

  • did_i be the durability of item ii.

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:

Total Value: i=1nvixi\text{Total Value: } \sum_{i=1}^n v_i \cdot x_i Total Durability: i=1ndixi\text{Total Durability: } \sum_{i=1}^n d_i \cdot x_i

Example with Additional Property

Using the modified problem where each item also has a durability score, depicted in the table:

ItemWeight (kg)
Tent10607
Water3102
Food2205
Jacket5308

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:

  1. Initialization: Use a 3D DP array dp[i][j][k] where i = items considered, j = weight, and k = durability.
  2. Transitions:
    • If item i is not taken, dp[i][j][k] = dp[i-1][j][k].
    • If item i is taken, dp[i][j][k] = \max(dp[i-1][j - w_i][k - d_i] + v_i, dp[i][j][k]).
  3. Final State: Check for maximum value and corresponding durability at dp[n][W][K] (where K is 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 ComponentDescription/Explanation
ObjectiveMaximize both total value and total durability
ConstraintsWeight must not exceed the knapsack capacity
Variablesxix_i (binary decision), viv_i, wiw_i, did_i, WW
AlgorithmDynamic Programming with multi-objective optimization
Application ExamplesSupply 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.