Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

CSE408

Knapsack problem
Dynamic Programming
Dynamic Programming is a general algorithm design technique
for solving problems defined by or formulated as recurrences with
overlapping sub instances

• Invented by American mathematician Richard Bellman in the


1950s to solve optimization problems and later assimilated by CS

• “Programming” here means “planning”

• Main idea:
- set up a recurrence relating a solution to a larger instance to
solutions of some smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
Knapsack Problem by DP
Given n items of
integer weights: w1 w2 … wn
values: v1 v2 … vn
a knapsack of integer capacity W
find most valuable subset of the items that fit into the
knapsack
Consider instance defined by first i items and capacity
j (j  W).
Let V[i,j] be optimal value of such an instance. Then
max {V[i-1,j], vi + V[i-1,j- wi]} if j- wi  0
{
V[i,j] =
V[i-1,j] if j- wi < 0
Initial conditions: V[0,j] = 0 and V[i,0] = 0
Knapsack Problem by DP (example)
Example: Knapsack of capacity W = 5
item weight value
1 2 $12
2 1 $10
3 3 $20
4 2 $15 capacity j
0 1 2 3 4 5
00 0 0
w1 = 2, v1= 12 1 0 0 12 Backtracing
w2 = 1, v2= 10 2 0 10 12 22 22 22 finds the actual
optimal subset,
w3 = 3, v3= 20 3 0 10 12 22 30 32 i.e. solution.
w4 = 2, v4= 15 4 0 10 15 25 30 37
Knapsack Problem by DP (pseudocode)

Algorithm DPKnapsack(w[1..n], v[1..n], W)


var V[0..n,0..W], P[1..n,1..W]: int
for j := 0 to W do
V[0,j] := 0
for i := 0 to n do Running time and space:
O(nW).
V[i,0] := 0
for i := 1 to n do
for j := 1 to W do
if w[i]  j and v[i] + V[i-1,j-w[i]] > V[i-1,j] then
V[i,j] := v[i] + V[i-1,j-w[i]]; P[i,j] := j-w[i]
else
V[i,j] := V[i-1,j]; P[i,j] := j
return V[n,W] and the optimal subset by backtracing
CSE408
TSP
Traveling Salesman Problem

• For each city i, 1≤ i ≤ n, find the sum si of the


distances from city i to the two nearest cities;
compute the sums of these n numbers, divide
the result by 2, and, if all the distances are
integers,round up the result to the nearest
integer:
Traveling Salesman Problem
Edge(a,d) and (d,a)
Example
CSE408
Vertex Cover and Bin
Packing
The vertex-cover problem
Example
Set Cover Problem
Bin Packing
• In the bin packing problem, objects of different volumes must
be packed into a finite number of bins or containers each of
volume V in a way that minimizes the number of bins used. In
computational complexity theory, it is a combinatorial NP-
hard problem.
• There are many variations of this problem, such as 2D
packing, linear packing, packing by weight, packing by cost,
and so on.
• They have many applications, such as filling up containers,
loading trucks with weight capacity constraints, creating file
backups in removable media and technology mapping in
Field-programmable gate array semiconductor chip design.
 The bin packing problem can also be seen as a special case of the cutting
stock problem.
 When the number of bins is restricted to 1 and each item is characterised
by both a volume and a value, the problem of maximising the value of
items that can fit in the bin is known as the knapsack problem.
 Despite the fact that the bin packing problem has an NP-hard
computational complexity, optimal solutions to very large instances of the
problem can be produced with sophisticated algorithms.
 In addition, many heuristics have been developed: for example, the first fit
algorithm provides a fast but often non-optimal solution, involving placing
each item into the first bin in which it will fit.
 It requires Θ(n log n) time, where n is the number of elements to be
packed.
Bin packing

• The algorithm can be made much more effective by first


sorting the list of elements into decreasing order
(sometimes known as the first-fit decreasing algorithm),
although this still does not guarantee an optimal solution,
and for longer lists may increase the running time of the
algorithm.
• It is known, however, that there always exists at least one
ordering of items that allows first-fit to produce an optimal
solution.[1]
• An interesting variant of bin packing that occurs in practice
is when items can share space when packed into a bin.
• Specifically, a set of items could occupy less space when
packed together than the sum of their individual sizes.
• This variant is known as VM packing[2] since when
virtual machines (VMs) are packed in a server,
their total memory requirement could decrease
due to pages shared by the VMs that need only
be stored once.
• If items can share space in arbitrary ways, the bin
packing problem is hard to even approximate.
• However, if the space sharing fits into a hierarchy,
as is the case with memory sharing in virtual
machines, the bin packing problem can be
efficiently approximated.
Bin packing Problem
First Fit Algorithm
First Fit Decreasing Algorithm
Full Bin Packing
Best Fit Algorithm

You might also like