Python implementation of Multiple-Choice Knapsack
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Multiple-Choice Knapsack Problem (MCKP) is a variation of the classic Knapsack Problem where items are grouped into classes, and from each class, only one item can be selected to be part of the solution. This problem can be found in various real-world scenarios, including resource allocation, project selection, and decision-making processes in business logistics. In this article, we will delve into the Python implementation of the MCKP, discussing the algorithmic approach, and providing code examples and explanations.
Problem Definition
In the MCKP, we are given several classes of items, each with different weights and values. The goal is to choose exactly one item from each class such that the total weight does not exceed the given knapsack capacity, and the total value is maximized.
Mathematical Formulation
Let there be n
classes, and each class i
contains m_i
items. Each item j
in class i
has a weight w_\{i,j\}
and a value v_\{i,j\}
. The problem can be formulated as:
Maximize:
Subject to:
Where:
• Z
is the total value.
• C
is the maximum capacity of the knapsack.
• x_\{i,j\}
is a binary variable representing whether item j
from class i
is chosen.
Dynamic Programming Approach
The MCKP can be efficiently solved using dynamic programming (DP). The underlying idea is to build a DP table with dimensions equal to the number of classes and knapsack capacity. Each cell of the DP table represents the maximum value attainable with a certain number of classes and a specific knapsack capacity.
Dynamic Programming Table
Let dp[i][c]
represent the maximum value that can be achieved using the first i
classes with a capacity c
.
State Transition
The state transition can be defined as:
For each item j
in class i
:
If w_\{i,j\} <= c
:
The solution is found in dp[n][C]
, which provides the maximum value achievable using all n
classes and total capacity C
.
Python Implementation
Here's a Python implementation of the MCKP using dynamic programming:
• Data Structure: A list dp
of size (capacity + 1)
is maintained to store the maximum values for each possible capacity.
• Outer Loop: Iterates over each class.
• Inner Loop: Traverses each item in the class and updates the DP table from right to left to ensure each item is only considered once per class.
Related reading
- Python Implementation of OPTICS Clustering Algorithm
- Python Implementations of Packing Algorithm
- Python Integer Partitioning with given k partitions
- Python linked list O1 insert/remove
- Python librdkafka producer perform against the native Apache Kafka Producer
- Python memory usage of numpy arrays
- Python implementation of the Wilson `Score` Interval?
- Python import csv to list

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.