4 Dynamic Programming-Lec

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

Dynamic programming

Divide & Conquer vs. Dynamic


Programming
•Both techniques split their input into parts, find sub-
solutions to the parts, and combine solutions to sub-
problems.
•In divide and conquer, solution to one sub-problem may
not affect the solutions to other sub-problems of the same
problem.
–In dynamic programming, sub-problems are dependent.
Sub-problems may share sub-sub-problems
Greedy vs. Dynamic Programming
• Both techniques are an algorithm design technique for optimization
problems (minimizing or maximizing), and both build solutions from
a collection of choices of individual elements.
–The greedy method computes its solution by making its choices in a
serial forward fashion, never looking back or revising previous
choices.
–Dynamic programming computes its solution forward/backward by
synthesizing them from smaller sub-solutions, and by trying many
possibilities and choices before it arrives at the optimal set of
choices.
• There is no a priori test by which one can tell if the Greedy method
will lead to an optimal solution.
–By contrast, there is a test for Dynamic Programming, called The
Principle of Optimality
The Principle of Optimality
•In DP an optimal sequence of decisions is obtained by making explicit
appeal to the principle of optimality.
•Definition: A problem is said to satisfy the Principle of Optimality if
the sub-solutions of an optimal solution of the problem are themselves
optimal solutions for their sub-problems.
– In solving a problem, we make a sequence of decisions D 1, D2,...,
Dn. If this sequence is optimal, then the k decisions also be optimal
•Examples: The shortest path problem satisfies the principle of
optimality.
– This is because if a, x1, x2,..., xn, b is a shortest path from node a to
node b in a graph, then the portion of xi to xj on that path is a
shortest path from xi to xj.
•DP reduces computation by
– Storing solution to a sub-problem the first time it is solved.
– Looking up the solution when sub-problem is encountered again.
– Solving sub-problems in a bottom-up or top-down fashion.
Dynamic programming (DP)
• DP is an algorithm design method that can be used when the solution
to a problem can be viewed as the result of a sequence of decisions.
–Example: The solution to knapsack problem can be viewed as the
result of a sequence of decisions. We have to decide the values of
xi, 0 or 1. First we make a decision on x1, then x2 and so on.
• For some problems, an optimal sequence of decisions can be found
by making the decisions one at a time using greedy method.
• For other problems, it is not possible to make step-wise decisions
based on only local information.
–One way to solve such problems is to try all possible decision
sequences. However time and space requirement is expensive.
–DP reduces those possible sequences not leading to optimal
decision.
Dynamic programming approaches
• To solve a problem by using dynamic programming:
– Find out the recurrence relations.
• Dynamic programming is a technique for efficiently computing
recurrences by storing partial results.
– Represent the problem by a multistage graph.
– A Multistage graph is a directed graph in which the nodes can be
divided into a set of stages such that all edges are from a stage to
next stage.
– In summary, if a problem can be described by a multistage graph,
then it can be solved by dynamic programming
Cont..
• Forward approach and backward approach:
– If the recurrence relations are formulated using the forward
approach, then the relations are solved beginning with the last
decision.
– If the recurrence relations are formulated using the backward
approach, then the relations are solved starting from the
beginning until we each to the final decision
Example: 0-1 knapsack problem
The shortest path in multistage graphs
• Find the shortest path in multistage graphs for the
following example?

• Source S, Destination T
• By greedy method: shortest path is ???
• By DP: shortest path is: ???
The shortest path
• Given a multi-stage graph, how can we find a shortest
path?
–Forward approach: Let p(i,j) denote the minimum cost
path from vertex j to the terminal vertex T. Let COST(i,j)
denote the cost of p(i,j) path. Then using the forward
approach, we obtain:
COST(i,j) = min {COST(i,j), c(i,k) + COST(k,j)}
–Backward approach: Let p(i,j) be a minimum cost path
from vertex S to a vertex j in Vi . Let COST(i,j) be the
cost of p(i,j).
COST(i,j) = min {COST(i,j), COST(i,k) + c(k,j)}
NB. If (i, j) is not element of E then COST(i, j) = + inf.
Cont..
Example
•Consider the following example to understand the concept of
multistage graph.
Cont..
• According to the formula, we have to calculate the cost (i,
j) using the following steps.
Step-1: Cost (K-2, j)
• In this step, three nodes (node 4, 5. 6) are selected as j. Hence,
we have three options to choose the minimum cost at this step.
• Cost(3, 4) = min {c(4, 7) + Cost(7, 9),c(4, 8) + Cost(8, 9)} = 7
• Cost(3, 5) = min {c(5, 7) + Cost(7, 9),c(5, 8) + Cost(8, 9)} = 5
• Cost(3, 6) = min {c(6, 7) + Cost(7, 9),c(6, 8) + Cost(8, 9)} = 5
Step-2: Cost (K-3, j)
• Two nodes are selected as j because at stage k - 3 = 2 there are
two nodes, 2 and 3. So, the value i = 2 and j = 2 and 3.
• Cost(2, 2) = min {c(2, 4) + Cost(4, 8) + Cost(8, 9),c(2, 6) +
• Cost(6, 8) + Cost(8, 9)} = 8
• Cost(2, 3) = {c(3, 4) + Cost(4, 8) + Cost(8, 9), c(3, 5) +
Cost(5, 8)+ Cost(8, 9), c(3, 6) + Cost(6, 8) + Cost(8, 9)} = 10
Cont..
Step-3: Cost (K-4, j)
•Cost (1, 1) = {c(1, 2) + Cost(2, 6) + Cost(6, 8) + Cost(8, 9), c(1,
3) + Cost(3, 5) + Cost(5, 8) + Cost(8, 9))} = 12
•Hence, the path having the minimum cost is 1→ 3→ 5→ 8→ 9.
Algorithm
procedure shortest_path (COST[], A[], n)
//cost[i,j] is the cost of edges[i,j] and A[i,j] is the shortest path from
i to j
//cost[i,i] is 0.0
for i = 1 to n do
for j = 1 to n do
A(i, j) := COST(i, j) //copy cost into A
for k = 1 to n do
for i = 1 to n do
for j = 1 to n do
A(i, j ) = min(A(i, j), A(i,k) + A(k,j));
end for
end for
end for
return A(1..n,1..n)
end shortest_path
This algorithm runs in time O( n3 )

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy