Knapsack Problem
The Knapsack Problem

• The famous knapsack problem:

– A thief breaks into a museum. Fabulous paintings,
sculptures, and jewels are everywhere. The thief
has a good eye for the value of these objects, and
knows that each will fetch hundreds or thousands
of dollars on the clandestine art collector’s market.
But, the thief has only brought a single knapsack to
the scene of the robbery, and can take away only
what he can carry. What items should the thief take
to maximize the haul?
Knapsack Problem – in Short
• A thief considers taking W pounds of loot. The
loot is in the form of n items, each with weight
wi and value vi. Any amount of an item can be
put in the knapsack as long as the weight limit
W is not exceeding.
Knapsack Problem – 2 types
1. 0-1 Knapsack Problem
(Dynamic Programming
Solution)
2. Fractional Knapsack Problem
(Greedy Approach Solution)
0-1 Knapsack problem
• Given a knapsack with maximum capacity W,
and a set S consisting of n items
• Each item i has some weight wi and benefit
value bi (all wi , bi and W are integer values)
• Problem: How to pack the knapsack to achieve
maximum total value of packed items?

• Solution: Dynamic Programming.
Fractional Knapsack Problem
• Fractional Knapsack Problem: we are given n
objects and a knapsack. Object i has a weight
wi and the knapsack has a capacity m. If a
fraction xi, 0<=xi <=1, of object i is placed into
the knapsack, then a profit pi xi is earned. The
objective is to obtain a filling of the knapsack
that maximizes the total profit earned.
Fractional Knapsack
max imize

pixi
1 i n

subject to

wixi m and 0 xi 1, 1 i n

1 i n

Greedy Algorithm gives the optimal solution
for the Fractional Knapsack Problem.
Fractional Knapsack-Greedy Solution
• Greedy-fractional-knapsack (w, v, W)

1. for i =1 to n
2. do x[i] =0
3. weight = 0
4. while weight < W
5. do i = best remaining item
6. if weight + w[i] ≤ W
7.
then x[i] = 1
8.
weight = weight + w[i]
9. else
10. x[i] = (w - weight) / w[i]
11. weight = W
12. return x
Analysis
• If the items are already sorted into decreasing
order of vi / wi, then the while-loop takes a time in
O(n);
• Therefore, the total time including the sort is in
O(n log n).
• If we keep the items in heap with largest vi/wi at the
root. Then creating the heap takes O(n) time
• while-loop now takes O(log n) time (since heap property
must be restored after the removal of root)

Fractional knapsack class 13

  • 1.
  • 2.
    The Knapsack Problem •The famous knapsack problem: – A thief breaks into a museum. Fabulous paintings, sculptures, and jewels are everywhere. The thief has a good eye for the value of these objects, and knows that each will fetch hundreds or thousands of dollars on the clandestine art collector’s market. But, the thief has only brought a single knapsack to the scene of the robbery, and can take away only what he can carry. What items should the thief take to maximize the haul?
  • 3.
    Knapsack Problem –in Short • A thief considers taking W pounds of loot. The loot is in the form of n items, each with weight wi and value vi. Any amount of an item can be put in the knapsack as long as the weight limit W is not exceeding.
  • 4.
    Knapsack Problem –2 types 1. 0-1 Knapsack Problem (Dynamic Programming Solution) 2. Fractional Knapsack Problem (Greedy Approach Solution)
  • 5.
    0-1 Knapsack problem •Given a knapsack with maximum capacity W, and a set S consisting of n items • Each item i has some weight wi and benefit value bi (all wi , bi and W are integer values) • Problem: How to pack the knapsack to achieve maximum total value of packed items? • Solution: Dynamic Programming.
  • 6.
    Fractional Knapsack Problem •Fractional Knapsack Problem: we are given n objects and a knapsack. Object i has a weight wi and the knapsack has a capacity m. If a fraction xi, 0<=xi <=1, of object i is placed into the knapsack, then a profit pi xi is earned. The objective is to obtain a filling of the knapsack that maximizes the total profit earned.
  • 7.
    Fractional Knapsack max imize pixi 1i n subject to wixi m and 0 xi 1, 1 i n 1 i n Greedy Algorithm gives the optimal solution for the Fractional Knapsack Problem.
  • 8.
    Fractional Knapsack-Greedy Solution •Greedy-fractional-knapsack (w, v, W) 1. for i =1 to n 2. do x[i] =0 3. weight = 0 4. while weight < W 5. do i = best remaining item 6. if weight + w[i] ≤ W 7. then x[i] = 1 8. weight = weight + w[i] 9. else 10. x[i] = (w - weight) / w[i] 11. weight = W 12. return x
  • 9.
    Analysis • If theitems are already sorted into decreasing order of vi / wi, then the while-loop takes a time in O(n); • Therefore, the total time including the sort is in O(n log n). • If we keep the items in heap with largest vi/wi at the root. Then creating the heap takes O(n) time • while-loop now takes O(log n) time (since heap property must be restored after the removal of root)