SlideServe
Menu
Préférences
© SlideServe · v2026

Floyd’s Algorithm (shortest-path problem)

uriel-jimenez avatar
25 diapositives · 665 Views · Nov 05, 2014

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.

Floyd’s Algorithm (shortest-path problem) - slide 1
01 / 25Floyd’s Algorithm (shortest-path problem)
1 / 17

Télécharger la présentation

Floyd’s Algorithm (shortest-path problem)
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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]