The Knapsack Problem - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

The Knapsack Problem

Description:

Output a set of items S such that. the sum of weights of items in S is at most K ... wD = 5 vD = $95. wE = 3 vE = $30. Weight. Items. Define Subproblems Based on Value ... – PowerPoint PPT presentation

Number of Views:222
Avg rating:3.0/5.0
Slides: 8
Provided by: charle106
Learn more at: http://www.cse.msu.edu
Category:

less

Transcript and Presenter's Notes

Title: The Knapsack Problem


1
The Knapsack Problem
  • Input
  • Capacity K
  • n items with weights wi and values vi
  • Goal
  • Output a set of items S such that
  • the sum of weights of items in S is at most K
  • the sum of values of items in S is maximized

2
Variations
  • Fractional
  • You may take a fraction of the entire item
  • All or nothing
  • You must take all of an item or none of it
  • Special cases of All or nothing
  • All items have same weight (or same value)
  • All items have same density (value/weight)
  • For which of the above variations does a greedy
    strategy work?
  • For which of the above variations does a greedy
    strategy NOT work?

3
Dynamic Programming
  • Consider the all or nothing version
  • Assume that all weights/values are reasonably
    sized integers
  • We can use dynamic programming to solve this
    problem (even though there is no explicit
    ordering among the items)

4
Definining subproblems
  • Define P(i,w) to be the problem of choosing a set
    of objects from the first i objects that
    maximizes value subject to weight constraint of
    w.
  • Impose an arbitrary ordering on the items
  • V(i,w) is the value of this set of items
  • Original problem corresponds to V(n, K)

5
Recurrence Relation/Running Time
  • V(i,w) max (V(i-1,w-wi) vi, V(i-1, w))
  • A maximal solution for P(i,w) either
  • uses item i (first term in max)
  • or does NOT use item i (second term in max)
  • V(0,w) 0 (no items to choose from)
  • V(i,0) 0 (no weight allowed)
  • What is the running time of this solution?
  • Number of table entries
  • Time to fill each entry

6
Example
Items
wA 2 vA 40 wB 3 vB 50 wC 1 vC
100 wD 5 vD 95 wE 3 vE 30
Weight
7
Define Subproblems Based on Value
  • Define P(i,w) to be the problem of choosing a set
    of objects from the first i objects that
    maximizes value subject to weight constraint of
    w.
  • V(i,w) is the value of this set of items
  • Original problem corresponds to V(n, W)
  • Define P(i,v) to be
Write a Comment
User Comments (0)
About PowerShow.com