You are on page 1of 12

ANALYSIS OF ALGORITHM

CHAPTER FOUR: DYNAMIC PROGRAMMING AND TRAVERSAL TECHNIQUES

DYNAMIC PROGRAMMING
 Dynamic programming is a technique for solving problems with overlapping subproblems. Typically, these subproblems
arise from a recurrence relating a solution to a given problem with solutions to its smaller subproblems of the same type.
 Rather than solving overlapping subproblems again and again, dynamic programming suggests solving each of the smaller
subproblems only once and recording the results in a table from which we can then obtain a solution to the original
problem.

ALL PAIRS SHORTEST PATHS


 Let G = (V, E) be a directed graph with n vertices. Let C be a cost adjacency matrix for G such that C(i, i) = 0, 1 <= i <=
n,C(i, j) is the length (or cost) of edge (i,j) if (i,j) €E(G) and C(i,j) =∞ if i ≠ j and (i,j) € E(G).
 The all pairs shortest path problem is to determine a matrix A such that A (i, j) is the length of a shortest path from i to j. The
matrix A may be obtained by solving n single source problems using the procedure SHORTEST_PATHS.
 Let us examine a shortest i to j path in G, i ≠ j. This path originates at vertex i and goes through some intermediate vertices
(possibly none) and terminates at vertex j. We may assume that this path contains no cycles for if there is a cycle then this
may be deleted without increasing the path length (no cycle has negative length).
 If k is an intermediate vertex on this shortest path then the subpaths from i to k and from k to j must be shortest paths from i
to k and k to j respectively. Otherwise, the i to j path is not of minimum length. So, the principle of optimality holds.
 This alerts us to the prospect of using dynamic programming. If k is the intermediate vertex with highest index then the i to k
path is a shortest i to k path in G going through no vertex with index greater than k - 1. Similarly the k to j path is a shortest k
to j path in G going through no vertex of index greater than k - 1. We may regard the construction of a shortest i to j path as
first requiring a decision as to which is the highest indexed intermediate vertex k. Once this decision has been made, we need
to find two shortest paths. One from i to k and the other from k toj. Neither of these may go through a vertex with index

Chapter 4 – Dynamic Prog. and Traversal Tech Page 1


greater thank - 1. Using Ak(i, J) to represent the length of a shortest path from i toj going through no vertex of index greater
than k, we obtain

Chapter 4 – Dynamic Prog. and Traversal Tech Page 2


Chapter 4 – Dynamic Prog. and Traversal Tech Page 3
THE TRAVELING SALESPERSON PROBLEM

Chapter 4 – Dynamic Prog. and Traversal Tech Page 4


Chapter 4 – Dynamic Prog. and Traversal Tech Page 5
Chapter 4 – Dynamic Prog. and Traversal Tech Page 6
BREADTH FIRST SEARCH
 In breadth first search we start at a vertex v and mark it as having been reached (visited). The vertex v will at this time be
said to be unexplored.
 A vertex will be said to have been explored by an algorithm when the algorithm has visited all vertices adjacent from it. All
unvisited vertices adjacent from v are visited next. These are new unexplored vertices.

Chapter 4 – Dynamic Prog. and Traversal Tech Page 7


 Vertex v has now been explored. The newly visited vertices haven't been explored and are put onto the end of a list of
unexplored vertices.
 The first vertex on this list is the next to be explored. Exploration continues until no unexplored vertex is left. The list of
unexplored vertices operates as a queue and may
be represented using any of the standard queue representations.
 Algorithm BFS (Algorithm 6. 5) describes the details of the search. It makes use of two algorithms DELETEQ(v, Q) which
deletes a vertex from the queue Q and returns, in v, the index and the vertex deleted and ADDQ(v, Q) which adds vertex v to
the rear of queue Q.
 Let us try out the algorithm on the undirected graph of Figure 6.6(a). If the graph is represented by its adjacency lists as in
Figure 6.6(b) then the vertices get visited in the order 1, 2, 3, 4, 5, 6, 7, 8.

Chapter 4 – Dynamic Prog. and Traversal Tech Page 8


Chapter 4 – Dynamic Prog. and Traversal Tech Page 9
DEPTH FIRST SEARCH
 A depth first search of a graph differs from a breadth first search in that the exploration of a vertex v is suspended as soon as
a new vertex is reached.
 At this time the exploration of the new vertex u begins. When this new vertex has been explored, we continue to explore v.

Chapter 4 – Dynamic Prog. and Traversal Tech Page 10


 The search terminates when all reached vertices have been fully explored. This search process is best described recursively
as in Algorithm 6.7.

Chapter 4 – Dynamic Prog. and Traversal Tech Page 11


Chapter 4 – Dynamic Prog. and Traversal Tech Page 12

You might also like