×
I'm working hard to be someone I'll be proud of.
--Your friends at LectureNotes
Close

Note for Design And Analysis Of Algorithm - DAA by Sunil Kumar B

  • Design And Analysis Of Algorithm - DAA
  • Note
  • 8 Topics
  • 19 Views
  • Uploaded 4 months ago
Sunil Kumar B
Sunil Kumar B
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

WWW.VIDYARTHIPLUS.COM UNIT-3 DYNAMIC PROGRAMING  The idea of dynamic programming is thus quit simple: avoid calculating the same thing twice, usually by keeping a table of known result that fills up a sub instances are solved.  Divide and conquer is a top-down method.  When a problem is solved by divide and conquer, we immediately attack the complete instance, which we then divide into smaller and smaller sub-instances as the algorithm progresses.  Dynamic programming on the other hand is a bottom-up technique.  We usually start with the smallest and hence the simplest sub- instances.  By combining their solutions, we obtain the answers to sub-instances of increasing size, until finally we arrive at the solution of the original instances.  The essential difference between the greedy method and dynamic programming is that the greedy method only one decision sequence is ever generated.  In dynamic programming, many decision sequences may be generated. However, sequences containing sub-optimal sub-sequences can not be optimal and so will not be generated. ALL PAIR SHORTEST PATH  Let G=<N,A> be a directed graph ’N’ is a set of nodes and ‘A’ is the set of edges.  Each edge has an associated non-negative length.  We want to calculate the length of the shortest path between each pair of nodes.  Suppose the nodes of G are numbered from 1 to n, so N={1,2,...N},and suppose G matrix L gives the length of each edge, with L(i,j)=0 for i=1,2...n,L(i,j)>=for all i & j, and L(i,j)=infinity, if the edge (i,j) does not exist.  The principle of optimality applies: if k is the node on the shortest path from i to j then the part of the path from i to k and the part from k to j must also be optimal, that is shorter. UNIT-3 THIYAGARAJAN

Text from page-2

WWW.VIDYARTHIPLUS.COM  First, create a cost adjacency matrix for the given graph.  Copy the above matrix-to-matrix D, which will give the direct distance between nodes.  We have to perform N iteration after iteration k.the matrix D will give you the distance between nodes with only (1,2...,k)as intermediate nodes.  At the iteration k, we have to check for each pair of nodes (i,j) whether or not there exists a path from i to j passing through node k. COST ADJACENCY MATRIX: D0 =L= 0 5 50 0 30  15  1 2 3 4 75 72 3 41   15 5 0 15 5 0 11 12 - 21 - - 24 - 32 - 41 – 43 - vertex 1: 7 5 7 12  3 4 9    1  2   11 12 - 21 212 - 24 - 32 - 41 412 43 – vertex 2: 7 5  7 7 12  2 10 3  5 4 9 1 11 11 12 - 124 21 212 24 321 32 - 324 41 412 43 4124 vertex 3: 7 5  7 12  10 3  UNIT-3 7 2 5 11 12 21 212 321 32 - 124 24 324 THIYAGARAJAN

Text from page-3

WWW.VIDYARTHIPLUS.COM 4 4 1 6 41 432 43 4324 vertex 4: 7 6 9 4  5 6 3 4 7 2 5 6 11 12 1243 124 241 2432 243 24 3241 32 3243 324 41 432 43 4324 At 0th iteration it nil give you the direct distances between any 2 nodes D0=  8 3 6 1 0 50 30 15 5   0 15 5  0 15  5 0 At 1st iteration we have to check the each pair(i,j) whether there is a path through node 1.if so we have to check whether it is minimum than the previous value and if I is so than the distance through 1 is the value of d1(i,j).at the same time we have to solve the intermediate node in the matrix position p(i,j). D1= 0 5   50 0 15 5 30 35 0 15 15 20 5 0 p[3,2]= 1 p[4,2]= 1 15 30 5 5 50 5 15 15 Fig: floyd’s algorithm and work  likewise we have to find the value for N iteration (ie) for N nodes. UNIT-3 THIYAGARAJAN

Text from page-4

WWW.VIDYARTHIPLUS.COM D2= 0 50 30 15 D3= D4= 5 0 35 20 20 10 15 5 0 15 5 0 0 5 20 10 45 0 15 5 30 35 0 15 15 20 5 0 0 5 15 20 0 10 30 35 0 15 20 5 P[1,3] = 2 P[1,4] = 2 P[2,1]=3 10 5 15 0 P[1,3]=4 P[2,3]=4  D4 will give the shortest distance between any pair of nodes.  If you want the exact path then we have to refer the matrix p.The matrix will be, P= 0042 3040 0100 0100 0 direct path  Since,p[1,3]=4,the shortest path from 1 to3 passes through 4.  Looking now at p[1,4]&p[4,3] we discover that between 1 & 4, we have to go to node 2 but that from 4 to 3 we proceed directly.  Finally we see the trips from 1 to 2, & from 2 to 4, are also direct.  The shortest path from 1 to 3 is 1,2,4,3. ALGORITHM : Function Floyd (L[1..r,1..r]):array[1..n,1..n] array D[1..n,1..n] UNIT-3 THIYAGARAJAN

Lecture Notes