Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- Floyd Warshall Algorithm by Nishith Lakhlani 2014 views
- Dijkstra's algorithm by gsp1294 7833 views
- The Floyd–Warshall algorithm by José Juan Herrera 5332 views
- (floyd's algm) by Jothi Lakshmi 309 views
- Floyd Warshall Algorithm by Md. Saidur Rahman... 1401 views
- Floyd warshall-algorithm by Malinga Perera 3689 views

No Downloads

Total views

8,243

On SlideShare

0

From Embeds

0

Number of Embeds

10

Shares

0

Downloads

0

Comments

0

Likes

14

No embeds

No notes for slide

- 1. S.SRIKRISHNANII yearCSE DepartmentSSNCE1The shortest distance between two points is under construction. – Noelie AltitoFLOYD’ ALGORITHM DESIGN
- 2. Introduction Problem statement Solution◦ Greedy Method (Dijkstra’s Algorithm)◦ Dynamic Programming Method Applications2
- 3. A non-linear data structure Set of vertices and edges Classification◦ Undirected graph◦ Directed graph (digraph) Basic terminologies◦ Path◦ Cycle◦ Degree3
- 4. Representation◦ Incidence Matrix◦ Adjacency Matrix◦ Adjacency List◦ Path Matrix Traversals◦ Depth first (Stack ADT)◦ Breadth first (Queue ADT)4
- 5. Standard Problems◦ Travelling salesman problem◦ Minimum spanning tree problem◦ Shortest path problem◦ Chinese postman problem5
- 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. 7GraphSource, SG (V,E) (S,V1)(S,V2)(S,V3)..(S,Vn)AlgorithmDataStructureProgramDijkstra’sAlgorithm.....
- 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. 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. 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. A sample graph:24103224 11850231365Sample Ip/Op11
- 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 22New York To Los Angels
- 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. 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

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment