Professional Documents
Culture Documents
Dharan MSC 2075 DP L8
Dharan MSC 2075 DP L8
Dynamic Programming
System Engineering
MSc
MScininLand & WaterEngineering
Hydropower Engineering
Institute of Engineering
Dharan Campus Dharan
2075
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
• Dynamic programming is applicable, when the sub‐ • Frequently, there is a polynomial number of sub‐
problems are NOT independent, that is when sub‐ problems, but they get repeated.
problems share sub sub‐problems.
• A dynamic programming algorithm solves every sub‐
• It is making a set of choices to arrive at optimal problem just once and then saves its answer in a table,
thereby avoiding the work of re‐computing the answer
solution. every time the sub‐problem is encountered
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
6/6/2018
Recursive Definition
of the Fibonacci Numbers
The Fibonacci numbers are a series of numbers as follows:
Recursive ???
fib(1) = 1
fib(2) = 1
1, n <= 2
fib(3) = 2 fib(n) = fib(n-1) + fib(n-2), n > 2
fib(4) = 3
fib(5) = 5
...
fib(3) = 1 + 1 = 2
fib(4) = 2 + 1 = 3
fib(5) = 2 + 3 = 5
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
Fibonacci numbers
In mathematics, the Fibonacci numbers or Fibonacci sequence are
the numbers in the following integer sequence:
1,2,3,5,8,13,21,34,55,89,144,…….. or including 0,
By definition, the first two numbers in the Fibonacci sequence are 1
and 1, or 0 and 1, depending on the chosen starting point of the
sequence, and each subsequent number is the sum of the previous
two.
A tiling with squares In mathematical terms, the sequence Fn of Fibonacci numbers is
whose side lengths are defined by the recurrence relation
successive Fibonacci
numbers
with seed values
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
6/6/2018
Recursive Algorithm
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
• Run an O(n) time loop, keep a temporary
One of the example of dynamic programming
variable to store the solution of sub‐problems
and then reuse them rather then recalculating
is an algorithm that solves the problem of
them.
• So by using dynamic programming we can
Matrix Chain Multiplication
solve a problem in polynomial time which
otherwise was solved in exponential time.
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
• We are given a sequence (chain) <A1, A2, A3, ...,An> , of n
matrices to be multiplied together. And we wish to
compute the product A1A2A3…An
• A product of matrices is fully parenthesized, if it is either a
single matrix or product of two fully parenthesized matrix
product, surrounded by parenthesis.
• As matrix multiplication is associative, so all
parenthesizations yield the same product/result.
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
6/6/2018
We can multiply two matrices A and B, only if the
number of columns of A is equal to the number of
rows of B.
So the time to compute C, is dominated by scalar
multiplications i.e. pqr
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
Thus computing the product according to the 1st
parenthesization is 10 times faster.
This clearly demonstrates the benefit of calculating the
optimum order before commencing the product
calculations.
(100x50)
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
• The number of alternative parenthesization for a sequence
of n matrices is denoted by P( n).
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
6/6/2018
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
The idea of dynamic programming is, rather
than computing m (min number of scalar
multiplications) recursively, computing it bottom
up: A recursive computation takes exponential
time; a bottom‐up computation O(n3) time.
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
When should we look for a dynamic
programming solution to a problem?
Two key ingredients?
– Optimal sub‐structure
– Overlapping sub‐problems
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
6/6/2018
Optimal sub‐structure
A problem exhibits optimal sub‐structure, if an
optimal solution to a problem contains within
it optimal solutions to sub‐problems.
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
Top‐down approach Vs Bottom‐Up approach
Bottom‐Up approach
More efficient because it takes advantage of the overlapping
sub‐problem property. There are only θ(n2) different sub‐
problems. Each problem solved exactly once.
Top‐down approach
Must repeatedly resolve each sub‐problem, each time it
reappears in the recursion.
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
6/6/2018
Typical formulations
2. Stage variables: define the order in which events
Occur in the system. Most commonly, time is the • Recursive Equations
Stage variable. There must be a finite number of
Possible states at each stage. • Recursive equations are used to structure a
multistage decision problem as a sequential
3. Control variables: represent the controls applied at a
particular stage and transform the state of the system. process
for a reservoir operation problem, the release of water
from the reservoir is a typical control variable. • Each recursive equation represents a stage at
which a decision is required
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
6/6/2018
• A series of equations are successively solved,
each equation depending on the output values
of the previous equations
• A multistage problem is solved by breaking into
a number of single stage problems through
recursion
• Approached can be done in a backward manner
or in a forward manner
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
6/6/2018
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
• Example:
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
Return from
Amount of water User 1 User 2 User 3
Exercise 1:
allocated R1(x) R2(x) R3(x) Solve the following 4-user water allocation problem to
x maximize the total returns, using dynamic programming:
0 0 0 0 Water available for allocation = 60 units, to be allocated
1 5 5 7 in discrete units of 0,10, 20, .., 60. Returns from the four users
for a given allocation, are given in the table below:
2 8 6 12
Allocation Returns from
3 9 3 15 User 1 User 2 User 3 User 4
4 8 ‐4 16 0 0 0 -3 1
5 5 ‐15 15 10 3 4 3 1
6 0 ‐30 12 20 5 4 5 1
30 6 4 5 7
40 3 4 4 8
50 3 6 2 10
60 3 7 0 10
[ 10 10 10 30 Max Return> 17]
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
6/6/2018
Stage 2:
n=2, and t=T-1,
Finally we have to back trace to the results
as earlier.
.
In general the recursive equation for any period t,
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
Term Definition..
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075 Dr. Bhola Ghimire M.Sc. Land and Water Engineering System Emgineering 2075
Thank You!!!