You are on page 1of 154

Dynamic

Programming

M. Ashraf
Iqbal
w ww . m y U E T . n e t . t c

http://www.istockphoto.com/stock-illustration-12665058-armed-
Distributed by : www.myUET.net.tc
robbery.php
The Knapsack
Problem

Distributed by : www.myUET.net.tc
The 0/1 Knapsack
Problem
• The 0-1 knapsack problem: Either
put an item once in the knapsack
or ignore it so as to maximize total
profit while keeping the weight
inside the knapsack within its
capacity. Alternately we may like to
ignore profits and try to maximize
the weight in the knapsack such
that it does not exceed its capacity.
Distributed by : www.myUET.net.tc
You can
Cut an
Item to Fit


The Fractional knapsack problem:
Either put an item or its fraction in the
knapsack or ignore it so as to maximize total
profit while keeping the weight inside the
knapsack exactly equal to its capacity.
http://www.google.com.pk/imgres?imgurl=http://images.clipartof.com/small/5669-Male-Carpenter-Cutting-Wood-On-A-Sawhorse-Clipart-
Illustration.jpg&imgrefurl=http://www.clipartof.com/details/clipart/5669.html&h=368&w=450&sz=55&tbnid=QzoxPWsL_w7nAM:&tbnh=1
04&tbnw=127&prev=/images%3Fq%3Dimage%2Bof%2Bcutting
%2Bwood&zoom=1&q=image+of+cutting+wood&hl=en&usg=__13lywSPGfp84b9pE_jq9zFTGznw=&sa=X&ei=1KuRTLvNGoGyvgO81ICJBA&ved=0
 CBgQ9QEwAA Distributed by : www.myUET.net.tc
Un-Bounded 0/1
Knapsack

The unbounded knapsack problem assumes that we


have an unlimited supply of each weight. We intend
to fill the knapsack as far as possible while
keeping the total number of weights minimum (also
known as the Change Problem ). We can also
maximize profit provided profit for each weight is
also given. Distributed by : www.myUET.net.tc
Un-
bounded
0/1
Knapsack

http://www.fotosearch .comby/BLD007
Distributed /jp2005_0002521/
: www.myUET.net.tc
Another Knapsack Problem
 The bounded 0/1 knapsack
problem restricts the number
of each item to a fixed
maximum value.

Distributed by : www.myUET.net.tc
Objective
s
1.We may like to maximize the
number
 of weights to be placed in the
knapsack such that the total
weight remains within the
knapsack capacity in a 0/1
scenario.
1.We may like to maximize the
profit provided the profit of
each weight is the same.
2.We may like to maximize profit
Distributed by : www.myUET.net.tc
How Many Knapsack
Problems?
H o w M a n y K n a p sa ck?
• What Strategies should be used?
• Greedy?
• Dynamic Programming?
• Brute Force – look at all options?
• What is Greedy
• What is Dynamic Programming
• What is the relationship between
Greedy, Dynamic Programming, &
Brute force Strategies?
Distributed by : www.myUET.net.tc
Which Strategy should be
used?
• Brute Force is expensive but?
• Dynamic Programming is intelligent
but?
• Greedy is FAST but?

Distributed by : www.myUET.net.tc
Which Strategy should be
used?
• Brute Force can solve every thing?
• Dynamic Programming can solve
some thing?
• How about Greedy?

Distributed by : www.myUET.net.tc
Start with the Change
Problem?

Distributed by : www.myUET.net.tc
Start with the Change
Problem?

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
T h e R e cu rsio n Tre e

Distributed by : www.myUET.net.tc
The Change Problem?
Recursive Formulation?

Recursion Tree?

Distributed by : www.myUET.net.tc
The Change Problem transforms into a Graph
Problem?
Find a Change = Find a Path?

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Find a Change = Find a
Shortest Path?

Distributed by : www.myUET.net.tc
Where is Brute Force, DP, &
Greedy?

Distributed by : www.myUET.net.tc
Do it by Brute Force?

Distributed by : www.myUET.net.tc
Do it by Dynamic
Programming?

Distributed by : www.myUET.net.tc
Do it by Greedy?

Distributed by : www.myUET.net.tc
The Change Problem?

• Brute Force provides an


Optimal answer?



• Dynamic Programming
provides an Optimal
answer?


Distributed by : www.myUET.net.tc
Profits & Weights in a
Knapsack?

H o w to sta rt n o w ?
Distributed by : www.myUET.net.tc
Profits & Weights in a
Knapsack?

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Where is Brute Force, DP, &
Greedy?

Distributed by : www.myUET.net.tc
Do it by Brute Force?

Distributed by : www.myUET.net.tc
Greedy: The Graph
collapses?

Distributed by : www.myUET.net.tc
Dynamic Programming: It
Collapses?

Distributed by : www.myUET.net.tc
Do it by Dynamic
Programming?

Distributed by : www.myUET.net.tc
The Change
Problem
Do it by Dynamic
Programming?

Distributed by : www.myUET.net.tc
The Change
Problem
Do it by Dynamic
Programming?

Distributed by : www.myUET.net.tc
The Change
Problem
Do it by Dynamic
Programming?

Distributed by : www.myUET.net.tc
Maximizing
Profit
Do it by Dynamic
Programming?

Distributed by : www.myUET.net.tc
Maximizing
Profit
Do it by Dynamic
Programming?

Distributed by : www.myUET.net.tc
Time complexity
calculations?

Distributed by : www.myUET.net.tc
The Knapsack Problem?

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Knapsack Problem?
• The above diagram shows a recursive
implementation of which recursive
equation? Discuss briefly. We have not yet
gone into dynamic programming.
• How can you find (from the above diagram)
the combination of weights that exactly
fits the knap sack of given capacity equal
to 11. Discuss briefly.
• The answer to the above part requires a
cute reduction of this problem into a
graph problem? What is that graph
problem? Please discuss.
Distributed by : www.myUET.net.tc
A New Recursive
Formulation?
P(5,11) = P(4,5) +
6
=
P(4,6) + 5
=
P(4,7) + 4
=
P(4,8) + 3 max
=
P(4,9) + 2

Distributed by : www.myUET.net.tc
A “Last Year” Mistake??

 

Distributed by : www.myUET.net.tc
Last Year Class: Hisaan to
the rescue

 
Distributed by : www.myUET.net.tc
Alternate Recursive
Formulation?

Distributed by : www.myUET.net.tc
Old Recursive Formulation?

Distributed by : www.myUET.net.tc
The Knapsack Problem

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Another Knapsack Problem
• Problem: We have k lists of
integers and a knapsack of
capacity T. We need to select
one item from each list such as
to maximize the total weight in
the knapsack without
overflowing it. Here is recursive
solution unfolding itself with
some common sense.
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
• Problem: We have k lists of integers
and a knapsack of capacity T. We
need to select one item from each
list such as to maximize the total
profit in the knapsack such that the
weight in the knapsack remains
within its capacity. Here is recursive
solution unfolding itself with some
common sense.
• Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Problem : We
have k lists of
integers and a
knapsack of
capacity T. We
need to select
one item from
each list such
as to maximize
the total weight
in the knapsack
without
overflowing it.
Here is an
efficient
dynamic
programming
solution.
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Knapsack Problem
• We intend to maximize profit keeping
the total weight within the knapsack
capacity. Find a recursive equation
corresponding to this scenario. How
will you implement this recursive
equation on the diagram above?
• How can you find (from the above
recursive diagram) the combination of
weights that maximizes total profit
keeping the total weight within
knapsack capacity? Discuss briefly.
• How can you reduce this graph and
thus reduce the time complexity of
this recursive implementation
Distributed by : www.myUET.net.tc
(without going into dynamic
The Problem of Maximizing
Profit?

Distributed by : www.myUET.net.tc
The Problem of Maximizing
Number of Elements in a single
Knapsack

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Activity Selection
Problem?

Distributed by : www.myUET.net.tc
Activity Selection Problem?

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Activity Selection Problem
• Maximize Total Merit with Start time
and Finish time fixed for each
activity?
• Maximize Total Merit when only
Duration is fixed for each activity?
• Maximize the Total Utilization of the
Hall?
• Maximize the Total Number of
activities?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Activity Selection Problem?

Distributed by : www.myUET.net.tc
Maximize Merit

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Complexity of Graph
Construction
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Partitioning Problem
• Problem: Given a fixed sequence of n
numbers, we wish to divide the sequence
into at most p partitions as fairly as
possible (you know the problem already).
Assume that we are given the following
sequence (5, 1, 2, 3, 4) and assume that p
=2.
• Write down a general recursive formulation
of the linear partition problem. Find the
optimal answer by inspection for the
specific problem given above.
• Draw the recursion tree similar to the one
drawn in Fig. 7(attached herewith).
Distributed by : www.myUET.net.tc
Remember that in the present problem
The Partitioning Problem

(5 , 1 , 2 , 3 ,
4, 3)

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Pa rtitio n th e se q u e n ce in to 4
p a rtitio n s
(5 , 1 , 2 , 3 ,
4, 3)

Distributed by : www.myUET.net.tc
Pa rtitio n th e se q u e n ce
in to 4 p a rti
( 5 ti
, 1o,n2s, 3 , 4 ,
3)

Distributed by : www.myUET.net.tc
Pa rtitio n th e se q u e n ce
in to 4 p a rti
( 5 ti
, 1o,n2s, 3 , 4 ,
3)

Tim e
C o m p lexity?
N um ber of
V e rtice s Distributed by : www.myUET.net.tc
Pa rtitio n th e se q u e n ce in to 4
p( 5a,rti
1 ,ti
2o, n
3 ,s
4, 3)

Distributed by : www.myUET.net.tc
Once again the Partitioning
Problem?

Distributed by : www.myUET.net.tc
The Same Problem but?

Distributed by : www.myUET.net.tc
Pa rtitio n th e se q u e n ce in to 4
p( 5a,rti
1 ,ti
2o, n
3 ,s
4, 3)

Distributed by : www.myUET.net.tc
Add one more level – but
why?
Distributed by : www.myUET.net.tc
Add one more level – but
why?
Distributed by : www.myUET.net.tc
The Rod Partitioning
Problem

T h e size - p rice
ta b le
Distributed by : www.myUET.net.tc
The Rod Partitioning
Problems

1. N o cu ttin g
co st?
2. U n ifo rm
cu ttin g co st?
3. N o n u n ifo rm
cu ttin g co st
Distributed by : www.myUET.net.tc
The Rod Partitioning
Problem

T h e size - p rice
ta b le
Distributed by : www.myUET.net.tc
The Rod
Partitioni
ng
Problem

T h e size - p rice
ta b le
Distributed by : www.myUET.net.tc
The Rod
Partitioni
ng
Problem

T h e size - p rice
ta b le
Distributed by : www.myUET.net.tc
The Rod Partitioning
Problem

C u ttin g C o st = 0
M a xim u m Price =
13

Distributed by : www.myUET.net.tc
The Rod Partitioning
Problem

T h e size - p rice
ta b le
Distributed by : www.myUET.net.tc
The Rod
Partitioning
Problem Distributed by : www.myUET.net.tc
Length & Price is
given – put
weights?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
H o w m a n y ve rtice s,
Distributed by : www.myUET.net.tc
D yn a m ic
Distributed by : www.myUET.net.tc
Dynamic
Programming
Distributed by : www.myUET.net.tc
Dynamic
Programming
Distributed by : www.myUET.net.tc
Dynamic
Programming

Total vertices and


edges?
Distributed by : www.myUET.net.tc
Dynamic
Programming
Distributed by : www.myUET.net.tc
Directed Acyclic Graph

Distributed by : www.myUET.net.tc
How about a fixed cutting
cost?
Distributed by : www.myUET.net.tc
Maximizing profits after
subtracting total cutting costs

Distributed by : www.myUET.net.tc
Maximizing profits after
subtracting total cutting costs

Distributed by : www.myUET.net.tc
Maximizing profits after
subtracting total cutting costs

Distributed by : www.myUET.net.tc
Maximizing profits after
subtracting total cutting costs

Distributed by : www.myUET.net.tc
Profits
after
cutting
costs

Distributed by : www.myUET.net.tc
• Total
Vertices?
• Total Edges?
• Time
Complexit
y?

Distributed by : www.myUET.net.tc
An alternate way of solving
it?

Distributed by : www.myUET.net.tc
An alternate
way of solving
it?

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
H o w th e D A G w ill
ch a n g e ? In sh a p e ? In
e d g e w e ig h ts?

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Once again the partitioning
problem
• Given a fixed sequence of n positive
numbers how can we partition it in
k partitions such that the weight of
the heaviest partition is minimum.

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Once again the partitioning
problem?
• Given a fixed sequence of n positive
numbers how can we partition it in
k partitions such that the weight of
the heaviest partition is minimum.
• How about if k=n?

Distributed by : www.myUET.net.tc
Once again the partitioning
problem?
• Given a fixed sequence of n positive
numbers how can we partition it in
k partitions such that the weight of
the heaviest partition is minimum.
• How about if some numbers are
negative?

Distributed by : www.myUET.net.tc
Once again the partitioning
problem?
• How about if some numbers are
negative?
• What should be the value of k so as
to minimize the weight of the
heaviest partition?
• The value of kcould be just 1?

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Why can not be the DAG like this
or that?

Distributed by : www.myUET.net.tc
H o w th e D A G
w o u ld lo o k
like if so m e
edge
w e ig h ts a re
n e g a tive b u t
k is fixe d
fro m a b o ve ?

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
M a trix M u ltip lica tio n
Pro b le m ?

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Longest or shortest path
problem

Distributed by : www.myUET.net.tc
The
Longest
path
problem

Distributed by : www.myUET.net.tc
Longest Increasing
sequence
• What is this problem?

Distributed by : www.myUET.net.tc
Longest Increasing Sub-
Sequence
• What is this problem?

Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc

You might also like