0% found this document useful (0 votes)
56 views

Knapsack Lect

The document discusses the knapsack problem from computing. It describes the knapsack problem as selecting items to fit in a bag without exceeding weight capacity while maximizing total value. It outlines different types of knapsack problems and provides examples of solving fractional knapsack problems using a greedy algorithm and dynamic programming.

Uploaded by

azeemahmad570
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Knapsack Lect

The document discusses the knapsack problem from computing. It describes the knapsack problem as selecting items to fit in a bag without exceeding weight capacity while maximizing total value. It outlines different types of knapsack problems and provides examples of solving fractional knapsack problems using a greedy algorithm and dynamic programming.

Uploaded by

azeemahmad570
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

knapsack

Knapsack

 Intro
 What is knapsack problem?
 Solution to knapsack problem
 0-1 knapsack
 Fractional knapsack + Example
 Dynamic programming + Example
 Time complexity
Introduction

 A knapsack is a bag with two straps that you wear over your shoulders, leaving
your arms free.
 Don't forget your knapsack when you head out on that hiking trip!
 Knapsack can also be name as "backpack" or a "rucksack."
 The word knapsack is thought to be the oldest of these terms, and while
"backpack" is more often used today in the U.S, knapsack is more common in
Canada.
What is knapsack problem?

 In the supermarket there are n packages (n ≤ 100) the package i has weight W[i]
≤ 100 and value V[i] ≤ 100.
 A thief breaks into the supermarket, the thief cannot carry weight exceeding M
(M ≤ 100).
 The problem to be solved here is: which packages the thief will take away to get
the highest value?
Input:
 Maximum weight M and the number of packages n.
 Array of weight W[i] and corresponding value V[i].
Output:
 Maximize value and corresponding weight in capacity.
 Which packages the thief will take away.
Solution to knapsack problem

 A simple solution is to consider all subsets of items and calculate the total
weight and value of all subsets.
 Consider the only subsets whose total weight is smaller than W. From all such
subsets, pick the maximum value subset.
 Knapsack problem can be further divided into two types:
 The 0/1 knapsack problem. in this type, each package can be taken or not
taken. besides, the thief cannot take a fractional amount of a taken package or
take a package more than once.
 Fractional knapsack problem: This type can be solved by greedy strategy.
 This type can be solved by dynamic programming approach.
0/1 Knapsack

 In 0-1 knapsack, items cannot be broken which means the thief should take the
item as a whole or should leave it. this is reason behind calling it as 0-1
knapsack.
 Hence, in case of 0-1 knapsack, the value can be either 0 or 1, where other
constraints remain the same.
 0-1 knapsack cannot be solved by greedy approach. greedy approach does not
ensure an optimal solution. in many instances, greedy approach may give an
optimal solution.
Fractional knapsack

 In Fractional Knapsack, we can break items for maximizing the total value
of knapsack.
 This problem in which we can break an item is also called the fractional
knapsack problem.
Algorithm of Fractional Knapsack

 Fractional Knapsack(Array w, Array v, int M)

 for i 1 to size(v)

 calculate cost[i] v[i]/w[i];

 sort Descending (cost);

 i 1;

 while (i <= size(v))

 {

 if w[i] <= M

 M M - w[i];

 total total + v[i];

 if

 {

 w[i]> M;

 i i+1

 }
Example of fractional knapsack

M=15

Objects 1 2 3 4 5 6 7

Profit 10 5 18 7 6 18 3

2 3 5 7 1 4 1
Weight

P/W 5 1.3 3 1 6 4.5 3


We put the objects in the bag on the basis of descending order.

• 5th object with P/W=6 and weight 1kg so 15-1=14 left


• 1st object with P/W=5 and weight 2kg so 14-2=12 left
• 6th object with P/W=4.5 and weight 4kg so 12-4=8 left
• 3rd object with P/W=3 and weight 5kg so 8-5=3 left
15 kg • 7th object with P/W=3 and weight 1kg so 3-1=2 left
• 2nd object with P/W=1.3 and weight 3 kg but according
to the capacity there is only 2kg left so we pick only 2 kg
instead of 3kg (2/3) therefore
2-2=0
x1 x2 x3 x4 x5 x6 x7 1 denote that the object
Objects 1 2 3 4 5 6 7 was picked up and 0
denoted that the object
1 2/3 1 0 1 1 1
was not picked.
 Constraint: Constraint is based on the object that was picked up and also
on the basis of weight of the objects.
x1 x2 x3 x4 x5 x6 x7
1 2/3 1 0 1 1 1
Weight 2 3 5 7 1 4 1

=1*2 + 2/3*3 + 5*1 + 0*7 +1*1 + 4*1 +1*1


=2+2+5+0+1+4+1
= 15
 Objective: objective is based on the item that was picked up and also on
the basis of profit of the objects.

x1 x2 x3 x4 x5 x6 x7
1 2/3 1 0 1 1 1
The objective is that the
Profit 10 5 15 7 6 18 3
profit is greater than weight

= 10*1 + 2/3*5 + 15*1 + 7*0 + 6*1 + 18*1 + 3*1


= 10+3.33+15+0+6+18+3
= 55.33
Dynamic programming

 Recursive backtracking starts with max capacity and makes choice for items.
Choices are:
▪ take the item if it fits
▪ don't take the item
 Dynamic programming start with the simpler problems.
 Reduce number of items available
 AND Reduce weight limit on knapsack

You might also like