0% found this document useful (0 votes)
31 views10 pages

Knapsack Problem Lecture No.11

Uploaded by

reemtariq529
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

Knapsack Problem Lecture No.11

Uploaded by

reemtariq529
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Artificial Intelligence

The Knapsack problem


The Knapsack Problem
• In the knapsack problem, need to pack a set of
items, with given values and sizes (such as
weights or volumes), into a container with a
maximum capacity .
• If the total size of the items exceeds the
capacity, can't pack them all.
• In that case, the problem is to choose a subset
of the items of maximum total value that will
fit in the container.
The Knapsack Problem
• knapsack is like a container or a bag.
• given some items which have some weights or
profits.
• have to put some items in the knapsack in such a
way total value produces a maximum profit.
• For example, the weight of the container is 20 kg.
We have to select the items in such a way that the
sum of the weight of items should be either smaller
than or equal to the weight of the container, and the
profit should be maximum.
The Knapsack Problem
Types of knapsack Problems
There are two types of knapsack problems:
• 0/1 knapsack problem
• Fractional knapsack problem
What is the 0/1 knapsack problem?
• The 0/1 knapsack problem means that the items are
either completely or no items are filled in a knapsack.
0/1 knapsack problem
• For example, two items having weights 2kg
and 3kg, respectively. If we pick the 2kg item
then we cannot pick 1kg item from the 2kg
item (item is not divisible); we have to pick the
2kg item completely.
• 0/1 knapsack problem in which either we pick
the item completely or we will pick that item.
The 0/1 knapsack problem is solved by the
dynamic programming.
Example of 0/1 knapsack problem
• Consider the problem having weights and profits are:
• Weights: {3, 4, 6, 5}
• Profits: {2, 3, 1, 4}
• The weight of the knapsack is 8 kg
• The number of items is 4
• The above problem can be solved by using the following
method:
• xi = {1, 0, 0, 1}
• = {0, 0, 0, 1}
• = {0, 1, 0, 1}
Example of 0/1 knapsack problem
• The above are the possible combinations. 1
denotes that the item is completely picked and
0 means that no item is picked. Since there are
4 items so possible combinations will be:
• 24 = 16; So. There are 16 possible
combinations that can be made by using the
above problem. Once all the combinations are
made, we have to select the combination that
provides the maximum profit.
What is the fractional knapsack problem?

• The fractional knapsack problem means that


we can divide the item.
• For example, we have an item of 3 kg then we
can pick the item of 2 kg and leave the item of
1 kg.
• The fractional knapsack problem is solved by
the Greedy approach.

You might also like