0% found this document useful (0 votes)
64 views20 pages

Session 9 - Knapsack Problem

The document discusses the knapsack problem and greedy algorithms for solving it. The knapsack problem involves selecting items to place in a knapsack of limited weight capacity to maximize the total value or profit of the items without exceeding the weight limit. There are variants like allowing fractional items. Greedy algorithms solve this by sorting items by profit/weight ratio and selecting items in that order until the knapsack is full. The time complexity of the greedy algorithm is O(nlogn).
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)
64 views20 pages

Session 9 - Knapsack Problem

The document discusses the knapsack problem and greedy algorithms for solving it. The knapsack problem involves selecting items to place in a knapsack of limited weight capacity to maximize the total value or profit of the items without exceeding the weight limit. There are variants like allowing fractional items. Greedy algorithms solve this by sorting items by profit/weight ratio and selecting items in that order until the knapsack is full. The time complexity of the greedy algorithm is O(nlogn).
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/ 20

Analysis and Design of

Algorithms
Session -09

Knapsack Problem

1
Insights
• Knapsack Problem – An Introduction
• Knapsack Problem – Variants & Its difference.
• Knapsack Problem
• Example:
• Solution 1 – Random Selection
• Solution 2 - Decreasing order of Profits
• Solution 3 – Increasing order of Weights
• Solution 4 – Decreasing order of Profit per Weight
• Greedy Algorithm for Knapsack Problem
• Algorithm
• Time Complexity
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.

3
Knapsack Problem Variants & its differences
Variants:
• Fractional Knapsack
• 0/1 Knapsack. •  can break items for maximizing the total
• not allowed to break items. We value of knapsack
either take the whole item or don’t
take it.

10kg 4kg 8kg


$10 $5 $20

7kg 2kg 6kg


$10 $3 $11

4kg 8kg
$2 $17

4
Knapsack Problem
N -number of objects
M -knapsack or bag Capacity
pi - a positive profit
wi - a positive weight
– Selection Vector [ 0 - 1](allows fractional values)
Goal: Choose items which will maximums the profit and Total weight must be less than M.
𝑵
𝒎𝒂𝒙𝒊𝒎𝒊𝒛𝒆 𝑷𝒓𝒐𝒇𝒊𝒕   𝒐𝒇   𝒕𝒉𝒆   𝒔𝒆𝒍𝒆𝒄𝒕𝒆𝒅   𝑶𝒃𝒋𝒆𝒄𝒕 ( ∑ 𝝆 𝒊 𝒙 𝒊 ) →𝟏
𝒊=𝟏
𝑵
𝒘𝒊𝒕𝒉 𝒔𝒖𝒃𝒋𝒆𝒄𝒕 𝒕𝒐   Weight of Selected Objects  ( ∑ 𝒘 𝒊 𝒙𝒊 ¿<¿𝒎𝒂𝒙𝒊𝒎𝒖𝒎 𝒘𝒆𝒊𝒈𝒉𝒕 𝒐𝒇 𝒕𝒉𝒆 𝑲𝒏𝒂𝒑𝒔𝒂𝒄𝒌( 𝑴)→𝟐
𝒊=𝟏

𝒘𝒉𝒆𝒓𝒆 𝟎 < ¿ 𝒙𝒊 ≤ 𝟏 𝒂𝒏𝒅 𝟏 ≤ 𝒊 ≤ 𝑵 𝟑


Knapsack Problem – Solution: Random Selection
Example:
Consider the following instance of the knapsack problem
n= 3, m=20,(p1,p2,p3) = (25,24,15) and (w1,w2,w3) = (18,15,10)
Objects = {A, B, C} Objects Profit (pi) Weight (wi)
Maximum Weight that Knapsack can hold (m) = 20 A 25 18
B 24 15
Solution:1 Randomly selected C 15 10
Maximum Weight that Knapsack can hold (m) = 20
Selection Vector (x1,x2,x3) : (1/2, 1/3, 1/4)

Weight of Selected Objects) = ( 1/2 *18 + 1/3 *15 + 1/4*10) = (9+5+2.5) = 16.5

= (1/2 *25 + 1/3 *24 + 1/4*15) = (12.5+8+3.75) = 24.25


Knapsack Problem – Solution: Decreasing Order of Profits
decreasing order of profits : A, B, C
Objects Profit (pi) Weight (wi)
A 25 18
B 24 15
C 15 10
Maximum Weight that Knapsack can hold (m) = 20
Selection Vector (x1,x2,x3) : (1,2/15,0)

Knapsack Weight = ∑ wixi = ( 1*18 + 2/15*15 + 0*10) = (18+2+0) = 20

Profit = ∑ pixi = (1 *25+ 2/15 *24+0*15) = (25+3.2+0) = 28.2


Knapsack Problem – Solution: Increasing Order of Weights
Objects are arranged in increasing order of weights
: C, B, A
Objects Profit (pi) Weight (wi)
C 15 10
B 24 15
A 25 18
Maximum Weight that Knapsack can hold (m) = 20

Selection Vector (x1,x2,x3) : (0,2/3,1)

Knapsack Weight = ∑ wixi = ( 0*18+ 2/3*15+1*10) = (0+10+10) = 20

Profit = ∑ pixi = (0 *25+ 2/3 *24+1*15) = (0+16+15) = 31


Knapsack Problem – Solution: Decreasing Order of Profits per Weight

Objects are arranged in Decreasing order of pi/wi : B, C, A


Profit
Objects Weight (wi) Pi / w i
(pi)
B 24 15 1.6
C 15 10 1.5
Selection Vector (x1,x2,x3) : (0,1,1/2) A 25 18 1.4
Maximum Weight that Knapsack can hold (m) = 20

Knapsack Weight = ∑ wixi = ( 0*18+ 1*15+1/2*10) = (0+15+5) = 20

Profit = ∑ pixi =(0 *25+ 1 *24+1/2*15) = (0+24+7.5)= 31.5


GREEDY ALGORITHM FOR THE FRACTIONAL KNAPSACK PROBLEM
Algorithm GreedyKnapsack(m, n)
//P[1:n] and w[1:n] contain the profits and weights respectively of the n objects
// ordered such that p[i]/w[i] >= p[i+1]/w[i+1].
//m is the knapsack size and x[1:n] is the solution vector.
{
for i :=1 to n do
x[i] := 0.0; // Initialize x.
U := m; //Knapsack Size
for i := 1 to n do {
if ( w[i] > U ) then
break;
x[i] := 1
U := U - w[i];
}
if ( i <= n) then
x[i] := U/w[i];
}
GREEDY ALGORITHM FOR THE FRACTIONAL KNAPSACK PROBLEM

P=30 P=15 P=20


W=20 W=5 W=10

Knapsack
GREEDY ALGORITHM FOR THE FRACTIONAL KNAPSACK PROBLEM

P=15 P=20 P=30


W=5 W=10 W=20

Knapsack
GREEDY ALGORITHM FOR THE FRACTIONAL KNAPSACK PROBLEM

P=15 P=20 P=30


W=5 W=10 W=20

Knapsack
M=15
GREEDY ALGORITHM FOR THE FRACTIONAL KNAPSACK PROBLEM

P=15 P=20 P=30


W=5 W=10 W=20

Knapsack
N=3 M=15
X=[ 0.0 0.0 0.0]
U=15
i=N/A
GREEDY ALGORITHM FOR THE FRACTIONAL KNAPSACK PROBLEM

P=15 P=20 P=30


W=5 W=10 W=20

N=3
X=[ 1.0 0.0 0.0] Knapsack
M=15
U=10
i=1
GREEDY ALGORITHM FOR THE FRACTIONAL KNAPSACK PROBLEM

P=20 P=30
W=10 W=20

N=3
X=[ 1.0 1.0 0.0] Knapsack
M=15
U=0
i=2

P=15
W=5
GREEDY ALGORITHM FOR THE FRACTIONAL KNAPSACK PROBLEM

P=30
W=20

N=3
X=[ 1.0 1.0 0.0] Knapsack
M=15
U=0
i=3

P=20
W=10
P=15
W=5
GREEDY ALGORITHM FOR THE FRACTIONAL KNAPSACK PROBLEM

P=30
W=20

N=3
X=[ 1.0 1.0 0.0] Knapsack
M=15
U=0
i=3

P=20
W=10
P=15
W=5
GREEDY ALGORITHM FOR THE FRACTIONAL KNAPSACK PROBLEM
Algorithm GreedyKnapsack(m, n)
{
for i :=1 to n do
x[i] := 0.0; // Initialize x.
U := m; //Knapsack Size Time Complexity = O(n)
for i := 1 to n do {
if ( w[i] > U ) then
break;
x[i] := 1
U := U - w[i];
}
if ( i <= n) then
x[i] := U/w[i];
}
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy