1 / 25

Floyd’s Algorithm (shortest-path problem)

Floyd’s Algorithm (shortest-path problem). Section 8.2. Shortest-path Problem. Given: A set of objects (called vertices) and A set of distances between objects (called edges) Find: The shortest path from a designated starting point The shortest path between any pair of vertices.

Download Presentation

Floyd’s Algorithm (shortest-path problem)

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. Floyd’s Algorithm(shortest-path problem) Section 8.2

  2. Shortest-path Problem • Given: • A set of objects (called vertices) and • A set of distances between objects (called edges) • Find: • The shortest path from a designated starting point • The shortest path between any pair of vertices

  3. Shortest-path Problem • Given: • A set of objects (called vertices) and • A set of distances between objects (called edges) • Find: • The shortest path from a designated starting point • The shortest path between any pair of vertices • Floyd’s algorithm solves this problem

  4. Shortest-path Problem • Given: • A set of objects (called vertices) and • A set of distances between objects (called edges) • Find: • The shortest path from a designated starting point • BTW, Dijkstra’s algorithm solves this one • The shortest path between any pair of vertices

  5. Floyd’s Algorithm • Dynamic Programming Algorithm • It uses an N X N matrix • N is the # of vertices • BTW, Pink Floyd’s The Wall is great movie to watch if you want to…

  6. 8 3 4 1 2 E C D A B 1 1 8 Floyd’s Algorithm • Recall a graph can be represented using an N X N matrix 8 3 1  4 8  2 1 3 4 1 1 1 8  2  1 8

  7. Floyd’s Algorithm • Dynamic Programming • Solves one step of the problem • Stores it, so it doesn’t have to be recomputed • Uses stored step to solve the next step • Avoids re-computing progressive steps • Many Algorithms (Brute Force, even Divide-and Conquer) re-compute the same information over and over again.

  8. Floyd’s Algorithm • Dynamic Programming • Uses extra memory to store steps (or sub-problems) • Avoids re-computation • However, the extra memory can be expensive.

  9. Floyd’s Algorithm • Famous Example of Dynamic Programming • Optimal sequence alignment algorithm • Most famous version is called the Smith-Waterman Algorithm • Align two sequences of length N • Previous algorithm O(2N) comparisons O(N) memory • Smith-Waterman Algorithm O(N2) comparisons O(N2) memory. • In this case the trade-off was worth it.

  10. 8 3 4 1 2 E C D A B 1 1 8 Floyd’s Algorithm • Back to the shortest-path problem. 8 3 1  4 8  2 1 3 4 1 1 1 8  2  1 8

  11. 8 3 4 1 2 A B C D E 1 1 8 Floyd’s Algorithm • First step, where can we go without using an intermediate vertex • Mk, where k the set of intermediate vertices M{}

  12. 8 3 4 1 2 A B C D E 1 1 8 Floyd’s Algorithm • Second step, where can we go if we use A as an intermediate vertices M{A}

  13. 8 3 4 1 2 A B C D E 1 1 8 Floyd’s Algorithm • Third step, where can we go if we use A and B as intermediate vertices M{A,B}

  14. 8 3 4 1 2 A B C D E 1 1 8 Floyd’s Algorithm • Fourth step, where can we go if we use A, B, and C as intermediate vertices • You tell me! M{A,B,C}

  15. 8 3 4 1 2 A B C D E 1 1 8 Floyd’s Algorithm • Fourth step, where can we go if we use A, B, and C as intermediate vertices M{A,B,C}

  16. 8 3 4 1 2 A B C D E 1 1 8 Floyd’s Algorithm • Fifth step, where can we go if we use A, B, C, and D as intermediate vertices • You tell me! M{A,B,C,D}

  17. 8 3 4 1 2 A B C D E 1 1 8 Floyd’s Algorithm • Fifth step, where can we go if we use A, B, C, and D as intermediate vertices • OK, here is the answer. M{A,B,C,D}

  18. 8 3 4 1 2 A B C D E 1 1 8 Floyd’s Algorithm • Final step, where can we go if we use all the vertices as intermediates. • You tell me! M{A,B,C,D,E}

  19. 8 3 4 1 2 A B C D E 1 1 8 Floyd’s Algorithm • Final step, where can we go if we use all the vertices as intermediates. • You tell me! M{A,B,C,D,E}

  20. Floyd’s Algorithm • So how do we actually compute (update) the matrix? • First of all… • Given N vertices how many steps are there? • i.e., how many times must the matrix be updated?

  21. Floyd’s Algorithm • for (int k = 0; k < N; k++) • Update the matrix, i.e. compute M{k} • Next… • How would you actually update each matrix cell? • i.e., how would you iterate through the matrix?

  22. 8 3 4 1 2 A B C D E 1 1 8 Floyd’s Algorithm Allowing no hops…M[D][E] = 8 What if we allow a hop through C? M[D][C] = 1 and M[C][E] = 1 If we allow a hop through C, what can we compare to see if M[D][E] needs to be updated?

  23. Floyd’s Algorithm • for (int k = 0; k < N; k++) • for (int i = 0; i < N; i++) • for (int j = 0; j < N; j++) • update M{k}[i][j] • What do we have to compare to see if M{k}[i][j] needs to be updated?

  24. Floyd’s Algorithm • for (int k = 0; k < N; k++) • for (int i = 0; i < N; i++) • for (int j = 0; j < N; j++) • if (M[i][k]+M[k][j] < M[i][j]) then • How do we do the update?

  25. Floyd’s Algorithm • for (int k = 0; k < N; k++) • for (int i = 0; i < N; i++) • for (int j = 0; j < N; j++) • if (M[i][k]+M[k][j] < M[i][j]) then • M[i][j] = M[i][k]+M[k][j]

More Related