All pairs shortest path algorithm


Published on

Published in: Design, Technology
  • Be the first to comment

All pairs shortest path algorithm

  1. 1. S.SRIKRISHNANII yearCSE DepartmentSSNCE1The shortest distance between two points is under construction. – Noelie AltitoFLOYD’ ALGORITHM DESIGN
  2. 2.  Introduction Problem statement Solution◦ Greedy Method (Dijkstra’s Algorithm)◦ Dynamic Programming Method Applications2
  3. 3.  A non-linear data structure Set of vertices and edges Classification◦ Undirected graph◦ Directed graph (digraph) Basic terminologies◦ Path◦ Cycle◦ Degree3
  4. 4.  Representation◦ Incidence Matrix◦ Adjacency Matrix◦ Adjacency List◦ Path Matrix Traversals◦ Depth first (Stack ADT)◦ Breadth first (Queue ADT)4
  5. 5.  Standard Problems◦ Travelling salesman problem◦ Minimum spanning tree problem◦ Shortest path problem◦ Chinese postman problem5
  6. 6.  To find the shortest path between source andother vertices Greedy method Assumptions◦ A directed acyclic graph◦ No negative edges Unweighted or weighted Graph6
  7. 7. 7GraphSource, SG (V,E) (S,V1)(S,V2)(S,V3)..(S,Vn)AlgorithmDataStructureProgramDijkstra’sAlgorithm.....
  8. 8. 8Step 1•Assign to every node a distance value. Set it to zero for our initialnode and to infinity for all other nodes.Step 2•Mark all nodes as unvisited. Set initial node as current.Step 3•For current node, consider all its unvisited neighbours and calculatetheir distance (from the initial node).Step 4•If this distance is less than the previously recorded distance (infinityin the beginning, zero for the initial node), overwrite the distance.
  9. 9. 9Step 5•When we are done considering all neighbours of the current node,mark it as visited.Step 6•A visited node will not be checked ever again; its distance recordednow is final and minimal.Step 7•Set the unvisited node with the smallest distance (from the initialnode) as the next "current node" and continue from step 3Step 8•Stop
  10. 10. Pseudo code1. function Dijkstra (Graph, source):2. for each vertex v in Graph: // Initializations3. dist[v] := infinity // Unknown distance function from source to v4. previous[v] := undefined // Previous node in optimal path from source5. dist[source] := 0 // Distance from source tosource6. Q := the set of all nodes in Graph // All nodes in the graph are unoptimized -thus are in Q7. while Q is not empty: // The main loop8. u := vertex in Q with smallest dist[]9. if dist[u] = infinity:10. break // all remaining vertices are inaccessible from source11. remove u from Q12. for each neighbor v of u: // where v has not yet been removed from Q13. alt := dist[u] + dist_between(u, v)14. if alt < dist[v]: // Relax (u,v,a)15. dist[v] := alt16. previous[v] := u17. return dist[]Running time: O((n+|E|)log n)10
  11. 11. A sample graph:24103224 11850231365Sample Ip/Op11
  12. 12. Result and Scope of DA The shortest path between a source vertex toall other vertices have been found Problem statement could me modified to:◦ To find shortest path between all vertices to aparticular vertex (destination)◦ How do I change the algorithm ?12
  13. 13. Problem Extensions The SINGLE-SOURCE SHORTEST PATH PROBLEM, in whichwe have to find shortest paths from a source vertex v toall other vertices in the graph. The SINGLE-DESTINATION SHORTEST PATH PROBLEM, inwhich we have to find shortest paths from all vertices inthe graph to a single destination vertex v. This can bereduced to the single-source shortest path problem byreversing the edges in the graph. The ALL-PAIRS SHORTEST PATH PROBLEM, in which wehave to find shortest paths between every pair ofvertices v, v in the graph.13
  14. 14. GraphSource, SG (V,E)Dijkstra’sAlgorithm.....(S,V1)(S,V2)(S,V3)..(S,Vn)(-)GraphDestination, DG (V,E)Dijkstra’sAlgorithm.....(D,V1)(D,V2)(D,V3)..(D,Vn)SINGLE-SOURCE SHORTEST PATH PROBLEMSINGLE-DESTINATION SHORTEST PATH PROBLEMGraphG (V,E)Dijkstra’sAlgorithm.....(V1,V1)(V1,V2)(V1,V3)..(Vn,Vn)ALL - PAIRS SHORTEST PATH PROBLEM14
  15. 15. All Pairs Shortest Path problem The all-pairs shortest path algorithm is to determinea matrix A such that A(i, j) is the length of theshortest path between i and j. Input given as a matrix form Output is an nXn matrix D = [dij] where dij is theshortest path from vertex i to j.Wij =0, if i=jW(i, j), if (i,j) ε E∞, if (i,j) ε E15
  16. 16. Solution 1 If there are no negative cost edges applyDijkstra’s algorithm to each vertex (as thesource) of the digraph. Disadvantage Running time increases to O(n(n+|E|)log n) Therefore we go for Dynamic Programming16
  17. 17. 17Dynamic Programming An algorithm design method that can be usedwhen the solution to the problem can be viewedas a result of a sequence of decisions. Best examples: Ordering matrix multiplication Optimal binary search trees All pairs-shortest path
  18. 18. 18Solution 2 To find the shortest path from i to j (i!=j) Assume some intermediate vertex k (or novertices also) The shortest path from i to j is the shortestpath from [(i,k) + (k,j) or i to j ] which ever isshorter. We use associated matrices and its powers tocalculate the shortest path from i to k and alsok to j. Matrix obtained in O(n.n.n)
  19. 19. 19Associated Matrices16342A0 1 2 31 0 4 112 6 0 23 3 ∞ 0A1 1 2 31 0 4 112 6 0 23 3 7 0A2 1 2 31 0 4 62 6 0 23 3 7 0A3 1 2 31 0 4 62 5 0 23 3 7 0Ak(i,j) = min {Ak-1(i,j), Ak-1(i,k) + Ak-1(k,j)}, k>=1* Not true for negative edges
  20. 20. 20Pseudo Code1. algorithm allpairs (cost, A, n):2. //cost[1:n, 1:n] is the cost adjacency matrix of a graph with nvertices3. //A[i,j] is the cost of a shortest path from vertex i toj .4. //cost[i,i] = 0 for 1<=i<=n.5. {6. for(i=0;i<n;i++)7. for(j=0;j<n;j++)8. A[i][j]=cost[i][j] //copy cost into A9. for(k=0;k<n;k++)10. for(i=0;i<n;i++)11. for(j=0;j<n;j++)12. A[i,j] = min { A[i,j] , A[i,k] + A[k,j] };13. }
  21. 21. 21Applications To automatically find directions betweenphysical locations Vehicle Routing and scheduling In a networking or telecommunicationapplications, Dijkstra’s algorithm has been usedfor solving the min-delay path problem (whichis the shortest path problem). For example indata network routing, the goal is to findthe path for data packets to go through aswitching network with minimal delay.
  22. 22. 22New York To Los Angels 
  23. 23. 23A Real Life Problem Whole pineapples are served in a restaurant in London. Toensure freshness, the pineapples are purchased in Hawaii and airfreighted from Honolulu to Heathrow in London. The followingnetwork diagram outlines the different routes that thepineapples could take.1056857766588105487544635671
  24. 24. 24References Data Structures and Algorithm Analysis in C,Second Edition, M.A. Weiss Fundamentals of Computer Algorithms, SecondEdition, Ellis Horowitz, Sartaj Sahni,Sanguthevar Rajasekaran Introduction to Design and Analysis ofAlgorithms, Fifth Edition, Anany Levitin
  25. 25. 25