Unit 4- Dynamic Programming.pdf

542 views 11 slides May 22, 2023
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Dynamic Programming-Multistage graph,Floyd's algorithm


Slide Content

UNIT 4
Dynamic Programming
Ms.Mary Jacob
Assistant Professor , Kristu Jayanti College (Autonomous),
Bangalore

Dynamic Programming
ØAn algorithm design method which can be used when the solution to a
problem may be viewed as the result of a sequence of decisions.
ØDynamic Programming algorithm stores the solutions for small
subproblems and looks them up to the stored results rather than
recomputing them again when it needs later to solve larger subproblems
ØTypically applied to optimiation problems

Dynamic Programming
4-Steps of Developing Dynamic Programming Algorithm
üCharacterize the structure of an optimal solution
üRecursively define the value of an optimal solution
üCompute the value of an optimal solution in a bottom-up fashion
üConstruct an optimal solution from computed nformation

Principle of Optimalilty
ØPrinciple of Optimalilty states that a an optimal sequence of decisions has the
property that whatever the initial state of decision is, the remaining decisions must
constitute an optimal decision sequence with regard to that resulting from the first
decision.
ØIn other words this principles states that the optimal solution for a larger
subproblem contains an optimal solution for a smaller subproblem.

Dynamic Programming VS. Greedy Method Vs Divide-and-Conquer
ØIn Greedy Method only one decision sequence is ever genertated
ØInDynamic Programming has many decision sequences that may be genertated.
ØIn Divide-and-Conquer we partition the problem into independent subproblems,
solve the subproblems recursively, and then combine their solutions to solve the
original problem
ØDynamic Programming is applicable when the subproblems are not
independent, that is, when subproblems share subsubproblems. Solves every
subsubproblem just once and then saves its answer in a table, thereby avoiding
the work of recomputing the answer every time the subsubproblem is
encountered

Dynamic Programming-Multistage Graph
Ø A multistage graph is a graph
G= (V, E) with V partitioned into K >= 2 disjoint subsets such that if an
edge (u, v) is in E, then u is in V
i , and v is in V
i+1 where 1<=i<=k, for
some subsets in the partition.
ØThe number of vertices in V
1 and V
k is one. i.e) | V
1 | = | V
K | = 1. The
node in the first set V
1 is source and the node in the last set V
k is
destination. Each set V
i is called a stage in the graph.
ØThe "multistage graph problem" is to find the minimum cost path from
the source to the destination. There are two approaches.
1. Multistage graph-Forward approach
2. Multistage graph –Backward approach.

Dynamic Programming-Multistage Graph
Multistage graph-Forward approach
A dynamic programming solution for a K-stage graph is obtained by first
noticing that every path from source to destination is a result of a sequence of
K-2 decisions. The i
th
decision involves in determining which vertex in stage
V
i+1 where 1<=i<=K-2 is to be selected to be on the path. Let P (i, j) be the
minimum cost path from vertex j in Vi to vertex T (destination vertex)
Cost of this path cost (i , j) = min{c(j, L)+cost(i+1,L)}
where L ϵ V
i+1 and (j, L) ϵ E.

Here j denotes the vertex in stage I and L denotes the vertex in stage i+1.

Dynamic Programming-Multistage Graph
Multistage graph-Backward approach
Multistage graph problem can also be solved using backward approach. Let cost
(i, j ) be the minimum cost path from vertex S(source) to vertex j in Vi. The cost
(i, j) using backward approach is

cost (i , j) = min{ cost (i-1, L) + c(L, j) }
Where L ϵ V
i -1 and (L, j) ϵ E.

Here j denotes the vertex in stage i and L denotes the vertex in stage i - 1.

All Pairs shortest Path – FLOYD Warshall Algorithm
ØUsed for solving the All Pairs Shortest Path problem.
ØThe problem is to find shortest distances between every pair of vertices in a given
edge.
ØThe solution matrix is initialized using the given graph . Then the solution matrix
is updated by considering all the vertices as an intermediate vertex.
ØWe have to pick one by one all the vertices and updates all shortest paths which
include the picked vertex as an intermediate vertex in the shortest path. When we
pick vertex number k as an intermediate vertex, we already have considered
vertices {0, 1, 2, .. k-1} as intermediate vertices. For every pair (i, j) of the source
and destination vertices, there are two possible cases1) k is not an intermediate
vertex in shortest path from i to j. We keep the value of dist[i][j] as it is2) k is an
intermediate vertex in shortest path from i to j. We update the value of dist[i][j] as
dist[i][k] + dist[k][j] if dist[i][j] > dist[i][k] + dist[k][j]

All Pairs shortest Path – FLOYD Warshall Algorithm
Formula:
D
k [i, j] =min {D
k-1[i, j], D
k-1[i, k] +D
k-1[k, j]}

Where D
k represents the matrix D after k
th iteration. Initially D
0=C (cost matrix). i
is the source vertex , j is the destination vertex and k represents the intermediate
vertex used.

THANK YOU
Tags