Dynamic Programming About Design and Analysis of Algorithm
Dynamic Programming About Design and Analysis of Algorithm
•
• With depth first search, the start state is chosen
to begin,
o then some successor of the start state,
▪ then some successor of that state,
• then some successor of that and so
on,
• trying to reach a goal state.
Disadvantages:
real-world examples:
Example
Kruskal’s Algorithm
• This is a greedy algorithm. A greedy algorithm chooses some
local optimum (i.e.picking an edge with the least weight in a
MST).
• Kruskal's algorithm works as follows:
o Take a graph with 'n' vertices, keep on adding the
shortest (least cost) edge, while avoiding the creation of
cycles, until (n - 1) edges have been added.
o Sometimes two or more edges may have the same cost.
▪ The order in which the edges are chosen, in this
case, does not matter.
o Different MSTs may result, but they will all have the
same total cost, which will always be the minimum cost.
Example
In case of parallel edges, keep the one which has the least cost
associated and remove all others.
• The least cost is 2 and edges involved are B,D and D,T.
• We add them. Adding them does not violate spanning tree
properties, so we continue to our next edge selection.
Next cost is 3, and associated edges are A,C and C,D. We add them
again −
Next cost in the table is 4, and we observe that adding it will create
a circuit in the graph. −
Now we are left with only one node to be added. Between the two
least cost edges available 7 and 8, we shall add the edge with cost
7.
By adding edge S,A we have included all the nodes of the graph
and we now have minimum cost spanning tree.
Example
• Pick the vertex with minimum key value and not already included in
MST (not in mstSET).
o We can either pick vertex 7 or vertex 2, let vertex 7 is picked. So
mstSet now becomes {0, 1, 7}.
o Update the key values of adjacent vertices of 7. The key value of
vertex 6 and 8 becomes finite (1 and 7 respectively).
• Pick the vertex with minimum key value and not already included in
MST (not in mstSET). Vertex 6 is picked.
o So mstSet now becomes {0, 1, 7, 6}. Update the key values of
adjacent vertices of 6. The key value of vertex 5 and 8 are
updated.
• We repeat the above steps until mstSet includes all vertices of given
graph. Finally, we get the following graph.
Dynamic Programming
According to the formula, we have to calculate the cost (i, j) using the following steps
Applied example
Travelling Salesman Problem
Travelling Salesman Problem
Problem Statement
• A traveler needs to visit all the cities from a list, where distances between all the
cities are known and each city should be visited just once.
• What is the shortest possible route that he visits each city exactly once and returns
to the origin city?
Solution
• Travelling salesman problem is the most notorious computational problem. We can
use brute-force approach to evaluate every possible tour and select the best one.
For n number of vertices in a graph, there are (n - 1)! number of possibilities.
.
Brute force approach
Example
Dynamic programming
• Instead of brute-force using dynamic programming approach, the solution can be
obtained in lesser time, though there is no polynomial time algorithm.
• Let us consider a graph G = (V, E), where V is a set of cities and E is a set of
weighted edges.
Example
Consider the problem above
ø
g(2, )=5
ø
g(3, )=6
ø
g(4, )=8
G(2,{3})=15
G(2,{4})=8
G(3,{2})=5
G(3,{4})=8
G(4,{2})=5
G(4,{3})=6
G(2,{3,4})=25
G(3,{2,4})=25
G(4,{2,3})=23