ALGO Dynamic Programmin Algorithm(Slide 1)
ALGO Dynamic Programmin Algorithm(Slide 1)
Rifat Ahommed
Lecturer
CSE, SEU
1
Dynamic Programming (DP)
◼ Dynamic programming is typically
applied to optimization problems.
P.2
Principle of optimality
◼ Suppose that in solving a problem, we have to
make a sequence of decisions D1, D2, …, Dn-1,
Dn
3
Dynamic method v.s.
Greedy method
◼ Comparison: In the greedy
method, any decision is locally
optimal.
◼ These locally optimal solutions
will finally add up to be a globally
optimal solution.
4
The Greedy Method
◼ E.g. Find a shortest path from v0 to v3.
◼ The greedy method can solve this problem.
◼ The shortest path: 1 + 2 + 4 = 7.
5
The Greedy Method
◼ E.g. Find a shortest path from v0 to v3 in the multi-stage
graph.
E.g.
A D
◼ 1 18
11 9
2 5 13
S B E T
16 2
5
C 2
F
2 d(B, T)
S B T
d(C, T)
5
C
11
Dynamic Programming
◼ d(B, T) = min{9+d(D, T), 5+d(E, T), 16+d(F, T)}
= min{9+18, 5+13, 16+2} = 18.
◼ d(C, T) = min{ 2+d(F, T) } = 2+2 = 4
◼ d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)}
= min{1+22, 2+18, 5+4} = 9.
4
A D
1 18 9 D
11 9 d(D, T)
2 5 13 5 d(E, T)
S B E T B E T
16 2
5 d(F, T)
16
C F F 12
2
The advantages of dynamic
programming approach
◼ To avoid exhaustively searching the entire solution
space (to eliminate some impossible solutions and
save computation).
◼ To solve the problem stage by stage
systematically.
◼ To store intermediate solutions in a table (array)
so that they can be retrieved from the table in
later stages of computation.
13
Comment
◼ If a problem can be described by a
multistage graph then it can be solved
by dynamic programming.
14
THANK YOU
15