1 / 18

Algorithms Bellman-Ford and Floyd

Algorithms Bellman-Ford and Floyd. Two basic algorithms for path searching in a graph. Evlogi Hristov. Telerik Corporation. Student at Telerik Academy. Table of Contents. Relaxing of edges and paths Negative Edge Weights Negative Cycles Bellman-Ford algorithm Description Pseudo code

adamma
Download Presentation

Algorithms Bellman-Ford and Floyd

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AlgorithmsBellman-Ford and Floyd Two basic algorithms for path searching in a graph EvlogiHristov Telerik Corporation Student at Telerik Academy

  2. Table of Contents • Relaxing of edges and paths • Negative Edge Weights • Negative Cycles • Bellman-Ford algorithm • Description • Pseudo code • Floyd-Warshall algorithm • Description • Pseudo code

  3. Relaxing Edges

  4. Relaxing Edges • Edge relaxation : Test whether traveling along a given edge gives a new shortest path to its destination vertex • Path relaxation:Test whether traveling through a given vertex gives a new shortest path connecting two other given vertices 2 + 2 = 4 if dist[v] > dist[u] + w dist[v] = dist[u] + w 4 -1 2 4 -1 = 3 7 3 2 5 2 + 5 = 7

  5. Negative Edges and Cycles

  6. Negative Edges and Cycles • Negative edges & Negative cycles • A cycle whose edges sum to a negative value • Cannot produce a correct "shortest path" answer if a negative cycle is reachable from the source 0 2 -2 -3 2 1 S 3 2 0 5 3

  7. Bellman-FordAlgorithm

  8. Bellman-Ford • Based on dynamic programming approach • Shortest paths from a single source vertex to all other vertices in a weighted graph • Slower than other algorithms but more flexible, can work with negative weight edges • Finds negative weight cycles in a graph • Worst case performance O(V·E)

  9. Bellman-Ford (2) • //Step 1: initialize graph • for each vertex v in vertices • if v is source then dist[v] = 0 • else dist[v] = infinity • //Step 2: relax edges repeatedly • for i = 1 to i = vertices-1 • for each edge (u, v, w) in edges • if dist[v] > dist[u] + w • dist[v] = dist[u] + w • //Step 3: check for negative-weight cycles • for each edge (u, v, w) in edges • if dist[v] > dist[u] + w • error "Graph contains a negative-weight cycle"

  10. Bellman-Ford Algorithm Live Demo ∞ ∞ -1 0 B 2 E A 2 3 1 -3 4 C D 5 ∞ ∞

  11. Bellman-Ford (3) • Flexibility • Optimizations http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm • Disadvantages • Does not scale well • Count to infinity (if node is unreachabel) • Changes of network topology are not reflected quickly (node-by-node) for i = 1 to i = n for each edge (u, v, w) in edges if dist[v] > dist[u] + w dist[v] = dist[u] + w

  12. Floyd-WarshallAlgorithm

  13. Floyd-Warshall • Based on dynamic programming approach • All shortest paths through the graph between each pair of vertices • Positive and negative edges • Only lengths • No details about the path • Worst case performance: O(V 3)

  14. Floyd-Warshall (2) let dist be a |V| × |V| array of minimum distances initialized to ∞ (infinity) for each vertex v dist[v][v] == 0 for each edge (u,v) dist[u][v] = w(u,v) // the weight of the edge (u,v) for k = 1 to k = |V| for i = 1 to i = |V| for j = 1 to j = |V| if (dist[i][j] > dist[i][k] + dist[k][j]) dist[i][j] = dist[i][k] + dist[k][j]

  15. Floyd-Warshall Algorithm Live Demo B -4 2 A 1 1 1 C D 4

  16. Bellman-Ford http://algoacademy.telerik.com

  17. Links for more information • Negative weights • http://www.informit.com/articles/article.aspx?p=169575&seqNum=8 • MIT Lecture and Proof • http://videolectures.net/mit6046jf05_demaine_lec18/ • Optimizations • fhttp://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithmy

  18. Free Trainings @ Telerik Academy • “C# Programming @ Telerik Academy • csharpfundamentals.telerik.com • Telerik Software Academy • academy.telerik.com • Telerik Academy @ Facebook • facebook.com/TelerikAcademy • Telerik Software Academy Forums • forums.academy.telerik.com

More Related