Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 5

Knapsack Problem

What is Knapsack Problem?


• The Knapsack Problem is a classic optimization problem in computer
science and mathematics, which involves finding the most valuable
combination of items that can fit into a limited capacity "knapsack" or
backpack.
• The goal is to maximize the value of the items included, subject to
the constraint of the knapsack's limited capacity. This problem has
applications in various fields, including finance, engineering, and
logistics.
Example
for i = 1 to n Time complexity
// calculate profit/weight O (n)
store object in decreasing order of P/W ratio O (n log n)
for i =1 to n
if M>0 and Wi <=M
M = M – Wi ;
P = P + Pi ;
else break;
if (M>0) O(n)
P = P + pi (M/Wi);

Hecne the total time complexity for Knapsak problem = O (n) + O (n log n) + O(n)
As O(n) are very snaller terms Hence time complexity = O (n log n)
Advantages Disadvantages
• There are many advantages to using the • There are a few disadvantages to using the
knapsack problem when trying to solve knapsack problem to solve optimization
problems involving the allocation of problems.
resources. • One disadvantage is that it can be difficult to
• One of the main advantages is that it is very find the optimal solution.
easy to understand and apply. • In addition, the knapsack problem can be
• Additionally, the knapsack problem often time-consuming to solve, and it can be
provides an optimal solution, which means difficult to understand the results
that it is not possible to improve upon the
solution found using this method.
• Finally, the knapsack problem is very efficient,
which means that it can be solved relatively
quickly

You might also like