SlidePub
Home
Categories
Login
Register
Home
Technology
Knapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.com
gomzee18
4,724 views
24 slides
Dec 18, 2012
Slide
1
of 24
Previous
Next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
About This Presentation
Knapshal Problem Notes By V.S. Subrahmanian, University of Maryland
Size:
215.08 KB
Language:
en
Added:
Dec 18, 2012
Slides:
24 pages
Slide Content
Slide 1
© 5/7/2002 V.S.
Subrahmanian
1
Knapsack Problem Notes
V.S. Subrahmanian
University of Maryland
Slide 2
© 5/7/2002 V.S.
Subrahmanian
2
Knapsack Problem
•You have a knapsack that has capacity (weight) C.
•You have several items I
1
,…,I
n
.
•Each item I
j
has a weight w
j
and a benefit b
j
.
•You want to place a certain number of copies of
each item I
j
in the knapsack so that:
–The knapsack weight capacity is not exceeded and
–The total benefit is maximal.
Slide 3
© 5/7/2002 V.S.
Subrahmanian
3
Example
Item Weight Benefit
A 2 60
B 3 75
C 4 90
Capacity = 5
Slide 4
© 5/7/2002 V.S.
Subrahmanian
4
Key question
•Suppose f(w) represents the maximal
possible benefit of a knapsack with weight
w.
•We want to find (in the example) f(5).
•Is there anything we can say about f(w) for
arbitrary w?
Slide 5
© 5/7/2002 V.S.
Subrahmanian
5
Key observation
•To fill a knapsack with items of weight w, we
must have added items into the knapsack in some
order.
•Suppose the last such item was I
j
with weight w
i
and benefit b
i
.
•Consider the knapsack with weight (w- w
i
).
Clearly, we chose to add I
j
to this knapsack
because of all items with weight w
i
or less, I
j
had
the max benefit b
i
.
Slide 6
© 5/7/2002 V.S.
Subrahmanian
6
Key observation
•Thus, f(w) = MAX { b
j
+ f(w-w
j
) | I
j
is an
item}.
•This gives rise to an immediate recursive
algorithm to determine how to fill a
knapsack.
Slide 7
© 5/7/2002 V.S.
Subrahmanian
7
Example
Item Weight Benefit
A 2 60
B 3 75
C 4 90
Slide 8
© 5/7/2002 V.S.
Subrahmanian
8
f(0), f(1)
•f(0) = 0. Why? The knapsack with capacity
0 can have nothing in it.
•f(1) = 0. There is no item with weight 1.
Slide 9
© 5/7/2002 V.S.
Subrahmanian
9
f(2)
•f(2) = 60. There is only one item with
weight 60.
•Choose A.
Slide 10
© 5/7/2002 V.S.
Subrahmanian
10
f(3)
•f(3) = MAX { b
j
+ f(w-w
j
) | I
j
is an item}.
= MAX { 60+f(3-2), 75 + f(3-3)}
= MAX { 60 + 0, 75 + 0 }
= 75.
Choose B.
Slide 11
© 5/7/2002 V.S.
Subrahmanian
11
f(4)
•f(4) = MAX { b
j
+ f(w-w
j
) | I
j
is an item}.
= MAX { 60 + f(4-2), 75 + f(4-3), 90+f(4-4)}
= MAX { 60 + 60, 75 + f(1), 90 + f(0)}
= MAX { 120, 75, 90}
=120.
Choose A.
Slide 12
© 5/7/2002 V.S.
Subrahmanian
12
f(5)
•f(5) = MAX { b
j
+ f(w-w
j
) | I
j
is an item}.
= MAX { 60 + f(5-2), 75 + f(5-3), 90+f(5-4)}
= MAX { 60 + f(3), 75 + f(2), 90 + f(1)}
= MAX { 60 + 75, 75 + 60, 90+0}
= 135.
Choose A or B.
Slide 13
© 5/7/2002 V.S.
Subrahmanian
13
Result
•Optimal knapsack weight is 135.
•Two possible optimal solutions:
–Choose A during computation of f(5). Choose
B in computation of f(3).
–Choose B during computation of f(5). Choose
A in computation of f(2).
•Both solutions coincide. Take A and B.
Slide 14
© 5/7/2002 V.S.
Subrahmanian
14
Another example
•Knapsack of capacity 50.
•3 items
–Item 1 has weight 10, benefit 60
–Item 2 has weight 20,benefit 100
–Item 3 has weight 30, benefit 120.
Slide 15
© 5/7/2002 V.S.
Subrahmanian
15
f(0),..,f(9)
•All have value 0.
Slide 16
© 5/7/2002 V.S.
Subrahmanian
16
f(10),..,f(19)
•All have value 10.
•Choose Item 1.
Slide 17
© 5/7/2002 V.S.
Subrahmanian
17
f(20),..,f(29)
•F(20) = MAX { 60 + f(10), 100 + f(0) }
= MAX { 60+60, 100+0}
=120.
Choose Item 1.
Slide 18
© 5/7/2002 V.S.
Subrahmanian
18
f(30),…,f(39)
•f(30) = MAX { 60 + f(20), 100 + f(10), 120
+ f(0) }
= MAX { 60 + 120, 100+60, 120+0}
= 180
Choose item 1.
Slide 19
© 5/7/2002 V.S.
Subrahmanian
19
f(40),…,f(49)
•F(40) = MAX { 60 + f(30), 100 + f(20), 120
+ f(10)}
= MAX { 60 + 180, 100+120, 120 + 60}
= 240.
Choose item 1.
Slide 20
© 5/7/2002 V.S.
Subrahmanian
20
f(50)
•f(50) = MAX { 60 + f(40), 100 + f(30), 120
+ f(20) }
= MAX { 60 + 240, 100+180, 120 + 120}
= 300.
Choose item 1.
Slide 21
© 5/7/2002 V.S.
Subrahmanian
21
Knapsack Problem Variants
•0/1 Knapsack problem: Similar to the
knapsack problem except that for each item,
only 1 copy is available (not an unlimited
number as we have been assuming so far).
•Fractional knapsack problem: You can take
a fractional number of items. Has the same
constraint as 0/1 knapsack. Can solve using
a greedy algorithm.
Slide 22
© 5/7/2002 V.S.
Subrahmanian
22
0/1 Knapsack
•f[j,w] = best solution having weight exactly
w after considering j elements
•If j >= C: f[j,w] = f[j-1,w]
•Otherwise: f[j,w] =
•MAX
remaining items
{
–f[j-1,w],
–f[j-1,w-w
j
]+b
j
}
Best solution after adding k-1 items
Replace an item with the k’th item
Slide 23
© 5/7/2002 V.S.
Subrahmanian
23
0/1 Knapsack Algorithm
For w = 0 to C do f[w]=0; (* initialize *)
For j=1 to n do
for w=C downto w
j
do
if f[w-w
j
] + b
j
> f[w] then
f[w] = f[w-w
j
] + b
j
O(n.C) algorithm
Slide 24
© 5/7/2002 V.S.
Subrahmanian
24
Fractional knapsack
•Much easier
•For item I
j, let r
j = b
j/w
j. This gives you the
benefit per measure of weight.
•Sort the items in descending order of r
j
•Pack the knapsack by putting as many of
each item as you can walking down the
sorted list.
Tags
data structures
knapshak algo
knapsack algorithm
dsa
knapshak
knapsack problem
invisisble knapsack
Categories
Technology
Download
Download Slideshow
Get the original presentation file
Quick Actions
Embed
Share
Save
Print
Full
Report
Statistics
Views
4,724
Slides
24
Favorites
2
Age
4735 days
Related Slideshows
11
8-top-ai-courses-for-customer-support-representatives-in-2025.pptx
JeroenErne2
50 views
10
7-essential-ai-courses-for-call-center-supervisors-in-2025.pptx
JeroenErne2
49 views
13
25-essential-ai-courses-for-user-support-specialists-in-2025.pptx
JeroenErne2
38 views
11
8-essential-ai-courses-for-insurance-customer-service-representatives-in-2025.pptx
JeroenErne2
38 views
21
Know for Certain
DaveSinNM
24 views
17
PPT OPD LES 3ertt4t4tqqqe23e3e3rq2qq232.pptx
novasedanayoga46
27 views
View More in This Category
Embed Slideshow
Dimensions
Width (px)
Height (px)
Start Page
Which slide to start from (1-24)
Options
Auto-play slides
Show controls
Embed Code
Copy Code
Share Slideshow
Share on Social Media
Share on Facebook
Share on Twitter
Share on LinkedIn
Share via Email
Or copy link
Copy
Report Content
Reason for reporting
*
Select a reason...
Inappropriate content
Copyright violation
Spam or misleading
Offensive or hateful
Privacy violation
Other
Slide number
Leave blank if it applies to the entire slideshow
Additional details
*
Help us understand the problem better