Lecture 18 Greedy Algo
Lecture 18 Greedy Algo
Lecture 18
DP Algorithm
DP algorithm can broken into 4
steps
1. Characterize the structure of
optimal solution
2. Recursively define the value of
optimal solution
3. Compute the value of optimal
solution in bottom up manner
4. Construct an optimal solution
from computed information
0/1 Knapsack Problem
O Given a knapsack with maximum
capacity W, and a set S consisting
of n items
O Each item i has some weight wi
and value vi (all wi,vi and W are
integers)
O Problem: How to pack knapsack
to achieve maximum total value of
packed items ?
0/1 Knapsack Problem
Item(i) Weight(wi) Value(vi)
1 2 3
2 3 4
3 4 5
4 5 8
5 9 10
maximize ∑ vi
iεT
subject to ∑ wi≤W
iεT
V[i,j]=
v[i-1,j] if wi > j
max{v[i-1,j],vi+[i-1,j-wi]} if wi≤j
Weight limit
0 1 2 3 4 5 6 7 8 9 10
(j)
w0 =0 v0=0
w1 =1 v1=1
w2 =2 v2=6
w3 =5 v3=18
w4 =6 v4=22
w5 =7 v5=28
Weight limit
0 1 2 3 4 5 6 7 8 9 10
(j)
w0 =0 v0=0 0 0 0 0 0 0 0 0 0 0 0
w1 =1 v1=1 0
w2 =2 v2=6 0
w3 =5 v3=18 0
w4 =6 v4=22 0
w5 =7 v5=28 0
Weight limit
0 1 2 3 4 5 6 7 8 9 10
(j)
w0 =0 v0=0 0 0 0 0 0 0 0 0 0 0 0
w1 =1 v1=1 0 1 1 1 1 1 1 1 1 1 1
w2 =2 v2=6 0
w3 =5 v3=18 0
w4 =6 v4=22 0
w5 =7 v5=28 0
Weight limit
0 1 2 3 4 5 6 7 8 9 10
(j)
w0 =0 v0=0 0 0 0 0 0 0 0 0 0 0 0
w1 =1 v1=1 0 1 1 1 1 1 1 1 1 1 1
w2 =2 v2=6 0 1 6 7 7 7 7 7 7 7 7
w3 =5 v3=18 0
w4 =6 v4=22 0
w5 =7 v5=28 0
Weight limit
0 1 2 3 4 5 6 7 8 9 10
(j)
w0 =0 v0=0 0 0 0 0 0 0 0 0 0 0 0
w1 =1 v1=1 0 1 1 1 1 1 1 1 1 1 1
w2 =2 v2=6 0 1 6 7 7 7 7 7 7 7 7
w3 =5 v3=18 0 1 6 7 7 18 22 24 25 25 25
w4 =6 v4=22 0
w5 =7 v5=28 0
Weight limit
0 1 2 3 4 5 6 7 8 9 10
(j)
w0 =0 v0=0 0 0 0 0 0 0 0 0 0 0 0
w1 =1 v1=1 0 1 1 1 1 1 1 1 1 1 1
w2 =2 v2=6 0 1 6 7 7 7 7 7 7 7 7
w3 =5 v3=18 0 1 6 7 7 18 22 24 25 25 25
w4 =6 v4=22 0 1 6 7 7 18 24 29 29 29 29
w5 =7 v5=28 0 1 6 7 7 18 22 26 29 34 35
0/1 Knapsack Problem
O we entered the values using the
recursion V[i,j]=
O max{v[i-1,j],vi+[i-1,j-wi]} if
wi≤j
O V[3,7]=max{v[2,7],v3+v[2,2]}
=max{7,18+6}=24
O KnapSack(n,W)
1. for w0 to W
2. do V[0,w] 0
3. for i=0 to n
4. do v[i,0]0
5. for w=0 to w
6. do if(wi≤w & vi+v[i-1,w-wi]>v[i-1,w]
7. then v[i,w]vi+v[i-1,w-wi]
8. else v[i,w]v[i-1,w]
The time complexity of this algorithm is
clearly O(n.w)
Constructing the optimal
solution
O The algorithm for computing v[i,j]
does not keep the record of which
subset of items gives the optimal
solution
O To compute the actual subset we
can add an auxiliary Boolean
matrix keep[i,j] which is 1 if we
decide to take the ith element and
0 otherwise
Weight limit
0 1 2 3 4 5 6 7 8 9 10
(j)
w0 =0 v0=0 0 0 0 0 0 0 0 0 0 0 0
w1 =1 v1=1 0 1 1 1 1 1 1 1 1 1 1
w2 =2 v2=6 0 0 1 1 1 1 1 1 1 1 1
w3 =5 v3=18 0 0 0 0 0 1 1 1 1 1 1
w4 =6 v4=22 0 0 0 0 0 0 1 1 1 1 1
w5 =7 v5=28 0 0 0 0 0 0 0 1 1 1 1
Optimal solution
O How do we use all values to
determine optimal subset T of
items
O If keep[n,w] is 1 then nεT , we
can repeat this argument for
keep[n-1,w-wn]
O If keep[n,w] is 0 then n≠T and
we repeat the argument for
keep [n-1,W]
Weight limit
0 1 2 3 4 5 6 7 8 9 10
(j)
w0 =0 v0=0 0 0 0 0 0 0 0 0 0 0 0
w1 =1 v1=1 0 1 1 1 1 1 1 1 1 1 1
w2 =2 v2=6 0 0 1 1 1 1 1 1 1 1 1
w3 =5 v3=18 0 0 0 0 0 1 1 1 1 1 1
w4 =6 v4=22 0 0 0 0 0 0 1 1 1 1 1
w5 =7 v5=28 0 0 0 0 0 0 0 1 1 1 1
a:5 5
a0 0 1
b 110
c:3 2
c 10 0 1
d 111
b:1 d:1
01100100100101110
abacacacda
Total no of 17 bits
Binary Character Code
Suppose we want to send 100000 characters
Which requires 800000 bits 11000101
300000
224000
Huffman Coding
O The tree holds the prefix code
property
O If C is the character set then
there are |C| leaves and |C|-1
internal nodes
O How we will build this tree
O We build it bottom-up using the
greedy approach
O HUFFMAN(C)
1. n |C|
2. QC // Q is a priority queue
3. for i 1 to n-1
4. do allocate a new node z
5. left[z] x Extract-Min(Q)
6. right[z]y Extract-Min(Q)
7. f[z]f[x]+f[y]
8. Insert(Q,z)
9. Return Extract-Min(Q) // return root
The codes are as
follows:
character code-word
f0
c 100
d 101
a 1100
b 1101
e 111