0% found this document useful (0 votes)
119 views14 pages

Design & Analysis of Algorithms: Bits, Pilani - K. K. Birla Goa Campus

The document discusses the 0-1 knapsack problem and algorithms to solve it. It begins by defining the problem - given a set of items each with a weight and value, select items to maximize total value without exceeding a weight limit. Greedy approaches are shown to not work through counterexamples. Dynamic programming is then introduced as an optimal approach, using recursive formulations and a 2D table to store optimal solutions to subproblems. Pseudocode is provided to implement the dynamic programming algorithm.

Uploaded by

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

Design & Analysis of Algorithms: Bits, Pilani - K. K. Birla Goa Campus

The document discusses the 0-1 knapsack problem and algorithms to solve it. It begins by defining the problem - given a set of items each with a weight and value, select items to maximize total value without exceeding a weight limit. Greedy approaches are shown to not work through counterexamples. Dynamic programming is then introduced as an optimal approach, using recursive formulations and a 2D table to store optimal solutions to subproblems. Pseudocode is provided to implement the dynamic programming algorithm.

Uploaded by

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

BITS, PILANI – K. K.

BIRLA GOA CAMPUS

Design & Analysis of Algorithms


(CS F364)

Lecture No. 12

1
The 0-1 Knapsack Problem
Given: A set S of n items, with each item i having
wi - a positive “weight”
vi - a positive “benefit”
Goal:
Choose items with maximum total benefit but with weight
at most W.
And we are not allowed to take fractional amounts
In this case, we let T denote the set of items we take

Objective : maximize ෍ 𝒗𝒊
𝒊∈𝑻

Constraint : ෍ 𝒘𝒊 ≤ 𝑾
𝒊∈𝑻
Greedy approach
Possible greedy approach:
Approach1: Pick item with largest value first
Approach2: Pick item with least weight first
Approach3: Pick item with largest value per
weight first
We know that none of the above approaches work
Exercise:
Prove by giving counterexamples.
Brute Force Approach
• Brute Force
The naive way to solve this problem is to go
through all 2n subsets of the n items and pick
the subset with a legal weight that maximizes
the value of the knapsack.
Optimal Substructure
As we did before we are going to solve the problem in
terms of sub-problems.
• Our first attempt might be to characterize a sub-
problem as follows:
Let Ok be the optimal subset of elements from {I0, I1, …,
Ik}.
Observe
Optimal subset from the elements {I0, I1, …, Ik+1} may
not correspond to the optimal subset of elements from
{I0, I1, …, Ik}
That means, the solution to the optimization problem for
Sk+1 might NOT contain the optimal solution from
problem Sk.
where Sk: Set of items numbered 1 to k.
Optimal Substructure
Example: Let W=20
Item Weight Value
I0 3 10
I1 8 4
I2 9 9
I3 8 11
• The best set of items from {I0, I1, I2} is {I0, I1, I2}
• BUT the best set of items from {I0, I1, I2, I3} is {I0, I2, I3}.
Note
1. Optimal solution, {I0, I2, I3} of S3 for does NOT contain the
optimal solution, {I0, I1, I2} of S2
2. {I0, I2, I3} build's upon the solution, {I0, I2}, which is really the
optimal subset of {I0, I1, I2} with weight 12 or less.
(We incorporates this idea in our 2nd attempt)
Recursive Formulation
Sk: Set of items numbered 1 to k.
Define B[k,w] to be the best selection from Sk with weight
at most w
 B[k  1, w] if wk  w
B[k , w]  
max{B[k  1, w], B[k  1, w  wk ]  bk } else

Our goal is to find B[n, W], where n is the total number of


items and W is the maximal weight the knapsack can carry.
This does have Optimal Substructure property.
Exercise – Prove Optimal Substructure property.
Optimal Substructure
Consider a most valuable load L where WL ≤ W
Suppose we remove item j from this optimal load L
The remaining load 𝑳′𝒋 = 𝑳 − {𝑰𝒋 }
must be a most valuable load weighing at most
𝑾′𝒋 = 𝑾 − {𝒘𝒋 }
pounds that the thief can take from
𝑺′𝒋 = 𝑺 − {𝑰𝒋 }
That is 𝑳′𝒋 should be an optimal solution to the
0-1 Knapsack Problem( 𝑺′𝒋 , 𝑾′𝒋 )
The 0-1 Knapsack Problem
Exercise
Overlapping Subproblems
Analysis
Pseudo- code
for w = 0 to W
B[0,w] = 0
for i = 1 to n
B[i,0] = 0
for i = 1 to n
for w = 0 to W
if 𝑤𝑖 <= w // item i can be part of the solution
if 𝑏𝑖 + B[i-1,w-𝑤𝑖 ] > B[i-1,w]
B[i,w] = 𝑏𝑖 + B[i-1,w- 𝑤𝑖 ]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // 𝑤𝑖 > w
The 0-1 Knapsack Problem
How to find actual Knapsack Items?
if B[i,k] ≠ B[i−1,k] then
mark the ith item as in the knapsack
i = i−1, k= k- 𝒘𝒊
else
i = i−1 // Assume the ith item is not in the
knapsack
All-Pairs Shortest Path
G = (V, E) be a graph
G has no negative weight cycles
Vertices are labeled 1 to n
If (i, j) is an edge its weight is denoted by wij
Optimal Substructure Property
Easy to Prove
Recursive formulation - 1
(𝒎)
𝐋𝐞𝐭 𝒍𝒊𝒋 be the minimum weight of any path from
vertex i to vertex j that contains atmost m edges.
Then,
(𝟎) 𝟎, 𝒊𝒇 𝒊 = 𝒋
𝒍𝒊𝒋 = ቊ
∞, 𝒊𝒇 𝒊 ≠ 𝒋

(𝒎) 𝒎−𝟏 𝒎−𝟏


𝒍𝒊𝒋 = 𝒎𝒊𝒏(𝒍𝒊𝒋 , 𝒎𝒊𝒏 {𝒍𝒊𝒌 + 𝒘𝒌𝒋 })
𝟏≤𝒌≤𝒏
minimum over all predecessors k of j
Recursive formulation - 2
(𝒌)
𝐋𝐞𝐭 𝒅𝒊𝒋be the weight of the shortest path from
vertex i to vertex j,
for which all the intermediate vertices are in the
set {1,2,….,k}
Then
(𝒌)
𝒅𝒊𝒋 = wij if k = 0
(𝒌) (𝒌−𝟏) (𝒌−𝟏) (𝒌−𝟏)
𝒅𝒊𝒋 = min (𝒅𝒊𝒌 , 𝒅𝒊𝒌 + 𝒅𝒌𝒋 ) if k ≥ 1
We have tacitly used the fact that an optimal
path through k does not visit k twice.

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