04 Knapsack 0-1 - Dynamic Programming
04 Knapsack 0-1 - Dynamic Programming
Knapsack Problem
• Input: a Knapsack (or a Container) of a
given capacity (in wt.)
• Input: number of items with given Profit
and Weight
• Outcome: Select items to be put in the
Container (within its capacity), ensuring
maximum profit
2
Knapsack Problem
• Two variations possible:
• Part of an item can be included
3
Knapsack Problem
• Two variations possible:
• Part of an item can be included
Fractional Knapsack
4
Fractional Knapsack
• If a whole item cannot be put in the
Knapsack (not enough capacity), a part of
it can be included, to fill in the remaining
capacity of Knapsack
• Other items included in Knapsack are
whole items
• In proportion to actual weight included of
the last item, partial profit is calculated
5
0-1 Knapsack
• Only whole items to be included, part of an
item not allowed
• Not possible to use the Fractional
Knapsack technique
• Need to look at all combinations of items
to ensure that total profit is maximised
6
Fibonacci Series
• Very familiar series in Mathematics
• Defined as : F(n) = F(n-1) + F(n-2)
• Recursive Definition, easy to implement as
recursive function
7
Fibonacci Series
• Very familiar series in Mathematics
• Defined as : F(n) = F(n-1) + F(n-2)
• Recursive Definition, easy to implement as
recursive function
Proc Fib (n)
Begin
End Proc
8
Fibonacci Series
Fib(7)
Fib(6) Fib(5)
0
Fib(6) Fib(5)
1
2
Fib(5) Fib(4) Fib(4) Fib(3)
• Introducing a Table F
• Stores intermediate values of series
• Every value need not be recomputed multiple times
10
Fibonacci Series
• New Algorithm for Fib
Proc N_Fib (n)
Begin
If n=0
Set F[0] := 0
Return F[0]
End If
If n=1
Set F[1] := 1
Return F[1]
End If
If F[n]=0
End If
Return F[n]
End Proc
11
Fibonacci Series
• New table
reduces
computation
time
• Time required
for F(n) = ?
12
Fibonacci Series
• Simpler Algorithm for Fib, using Table F[n]
• Iterative instead of Recursive
Begin
Set F[0] := 0
Set F[1] := 1
End If
End IF
End Proc
13
0-1 Knapsack
• Given an input of n items, calculations of
all combinations will require 2n steps
• To reduce computational complexity, we
will use a temporary table for storing
intermediate values
• Reduces exponential complexity to
Polynomial
14
0-1 Knapsack
Item List
Items Weight Profit
I1 2 3
I2 3 4
I3 4 5
I4 5 7
Knapsack Capacity: 7
15
0-1 Knapsack
Table for storing item combinations and profits
0 1 2 3 4 5 6 7
0
I1
I2
I3
I4
16
0-1 Knapsack
0 1 2 3 4 5 6 7
0
I1
I2
I3
I4
• Initialization of Table
• First Row and First Column initialized to 0
• First Row indicates zero profit when no items are added
• First Column indicates zero profit when Knapsack capacity
is 0
18
0-1 Knapsack
0 1 2 3 4 5 6 7 I1 2 3
I2 3 4
0 0 0 0 0 0 0 0 0
I3 4 5
I1 0 0 3 3 3 3 3 3 I4 5 7
I2 0
I3 0
I4 0
23
0-1 Knapsack
0 1 2 3 4 5 6 7 I1 2 3
I2 3 4
0 0 0 0 0 0 0 0 0
I3 4 5
I1 0 0 3 3 3 3 3 3 I4 5 7
I2 0 0 3 4 4 7 7 7
I3 0 0 3 4 5 7
I4 0
27
0-1 Knapsack
0 1 2 3 4 5 6 7 I1 2 3
I2 3 4
0 0 0 0 0 0 0 0 0
I3 4 5
I1 0 0 3 3 3 3 3 3 I4 5 7
I2 0 0 3 4 4 7 7 7
I3 0 0 3 4 5 7 8 9
I4 0
I3 0 0 3 4 5 7 8 9
I4 0
I3 0 0 3 4 5 7 8 9
I4 0 0
I3 0 0 3 4 5 7 8 9
I4 0 0 3
I3 0 0 3 4 5 7 8 9
I4 0 0 3 4
I3 0 0 3 4 5 7 8 9
I4 0 0 3 4 5
I3 0 0 3 4 5 7 8 9
I4 0 0 3 4 5 7
End Repeat
0-1 Knapsack
I\J 0 1 2 3 4 5 6 7 I1 2 3
0 0 0 0 0 0 0 0 0 I2 3 4
I1 0 0 3 3 3 3 3 3 I3 4 5
I2 0 0 3 4 4 7 7 7 I4 5 7
I3 0 0 3 4 5 7 8 9
I4 0 0 3 4 5 7 8
I3 0 0 3 4 5 7 8 9
I4 0 0 3 4 5 7 8 10
I3 0 0 3 4 5 7 8 9
I4 0 0 3 4 5 7 8 10