Lecture Slides on Knapsack Problem - Algorithms | CS 4102, Study notes of Algorithms and Programming

Material Type: Notes; Professor: Cohoon; Class: Algorithms; Subject: Computer Science; University: University of Virginia; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 07/29/2009

koofers-user-28f-1
koofers-user-28f-1 🇺🇸

9 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
0/1 Knapsack Problem
Definition
Input:
Vector p of n profits and a vector w of n weights and a
total weight M
Output:
An optimal 0/1 vector of length x, where xi= 1 indicates
object i is in knapsack and xi= 0 indicates object i is not
in the knapsack
Maximizes the profit subject to the constraint that
the total weight of the included objects is at most M
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lecture Slides on Knapsack Problem - Algorithms | CS 4102 and more Study notes Algorithms and Programming in PDF only on Docsity!

0/1 Knapsack Problem

  • Definition

 Input:  Vector p of n profits and a vector w of n weights and a total weight M

 Output:  An optimal 0/1 vector of length x, where xi = 1 indicates object i is in knapsack and xi = 0 indicates object i is not in the knapsack  Maximizes the profit subject to the constraint that the total weight of the included objects is at most M

Notation

  • Knap(i, j, y)

 Value of optimal solution with respect to objects i through j using at most capacity y

 Knap(1, n, M)  Value of the solution to our problem

  • gj(y) = Knap(j+1, n, y)
  • fj(y) = Knap(1, j, y)

Backward Approach

  • Observe

 fi(y) = max {fi-1(y) , pi + fi-1(y - wi)

  • Also

 f 0 (y) = 0 for y >= 0

 f 0 (y) = - for y < 0

 Can compute f 1 from f 0 , f 2 from f 1 , and so on ...

  • fi() is an ascending step function
  • fi() is specified by the jump pairs (p, w) where fi(y) changes
  • fi() is buildable from fi-1()
  • Let Si^ be the jump pair set for fi ()  Si^ = ( (fi(yj), yj) | yj is a point of jumping (discontinuity) }

Backward Approach

Algorithm

S[0] = {(0,0)}

for (i = 1; i < n; ++i) { Si1 = {(P, W) | (P - p[i],W - w[i]) | S[i-1] && W M} S[i] = MergePurge(S[i-1], Si1) }

for (i = n; n >= 1; --i) { (P1, W1) = maxp { (P,W) | (P,W) in S[i-1]} (P2, W2) = maxw { (P + p[i], W + w[i]) | (P,W) in S[i-1], W + w[i] M} if (P1 > P2) x[i] = 0 else x[i] = 1 }

Implementation

  • Running time

 |Si1| |S[i-1]|  |S[i]| 2|S[i-1]|

  • If S[i]’s are in sorted order then merging can be done in

linear time

1 1

[ ] 2 2 1

n n i n

i i

S i