0% found this document useful (0 votes)
354 views11 pages

Knapsack Problem

The knapsack problem involves selecting items to place in a knapsack of limited weight capacity to maximize the total value of items while not exceeding the weight limit. There are two variants: the fractional knapsack problem allows items to be divisible while the 0/1 knapsack problem does not. The fractional knapsack problem is solved using a greedy approach by sorting items by their value/weight ratio and placing items in order until the knapsack is full.

Uploaded by

Vijay Trivedi
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)
354 views11 pages

Knapsack Problem

The knapsack problem involves selecting items to place in a knapsack of limited weight capacity to maximize the total value of items while not exceeding the weight limit. There are two variants: the fractional knapsack problem allows items to be divisible while the 0/1 knapsack problem does not. The fractional knapsack problem is solved using a greedy approach by sorting items by their value/weight ratio and placing items in order until the knapsack is full.

Uploaded by

Vijay Trivedi
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/ 11

KNAPSACK PROBLEM

• You are given the following-


• A knapsack (kind of shoulder bag) with limited weight capacity.
• Few items each having some weight and value.

• The problem states-


• Which items should be placed into the knapsack such that-
• The value or profit obtained by putting the items into the
knapsack is maximum.
• And the weight limit of the knapsack does not exceed.
KNAPSACK PROBLEM
KNAPSACK PROBLEM
• Knapsack Problem Variants-
• Knapsack problem has the following two
variants-

• Fractional Knapsack Problem


• 0/1 Knapsack Problem
Fractional Knapsack Problem-

• In Fractional Knapsack Problem,


• As the name suggests, items are divisible here.
• We can even put the fraction of any item into the
knapsack if taking the complete item is not possible.
• It is solved using Greedy Method.
Fractional Knapsack Problem Using Greedy Method-

• Step-01:
•  
• For each item, compute its value / weight ratio.
•  
• Step-02:
•  
• Arrange all the items in decreasing order of their value / weight ratio.
•  
• Step-03:
•  
• Start putting the items into the knapsack beginning from the item with the
highest ratio.
• Put as many items as you can into the knapsack.
Time Complexity-
• The main time taking step is the sorting of all items in
decreasing order of their value / weight ratio.
• If the items are already arranged in the required order,
then while loop takes O(n) time.
• The average time complexity of Quick Sort is O(nlogn).
• Therefore, total time taken including the sort is O(nlogn).
Problem-

• For the given set of items and knapsack capacity = 60 kg, find the
optimal solution for the fractional knapsack problem making use of
greedy approach.
Solution-

• Step-01:

• Compute the value / weight ratio for each


item-
Solution-

• Step-02:
•  
• Sort all the items in decreasing order of their
value / weight ratio- 
• I1 I2 I5 I4 I3
• (6) (4) (3.6) (3.5) (3)
Solution-

• Step-03:
• Start filling the knapsack by putting the items
into it one by one.
solution
• Now,
• Knapsack weight left to be filled is 20 kg but item-4 has
a weight of 22 kg.
• Since in fractional knapsack problem, even the fraction
of any item can be taken.
• So, knapsack will contain the following items-
• < I1 , I2 , I5 , (20/22) I4 >
• Total cost of the knapsack
• = 160 + (20/27) x 77
• = 160 + 70
• = 230 units

You might also like