310 likes | 614 Views
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.
E N D
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
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
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
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…
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
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.
Floyd’s Algorithm • Dynamic Programming • Uses extra memory to store steps (or sub-problems) • Avoids re-computation • However, the extra memory can be expensive.
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.
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
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{}
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}
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}
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}
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}
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}
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}
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}
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}
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?
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?
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?
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?
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?
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]