Presentation is loading. Please wait.

Presentation is loading. Please wait.

Floyd’s Algorithm (shortest-path problem) Section 8.2.

Similar presentations


Presentation on theme: "Floyd’s Algorithm (shortest-path problem) Section 8.2."— 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 Floyd’s Algorithm ► Recall a graph can be represented using an N X N matrix AB C DE 8 21 8 3 4 1 1 ABCDE A0 B0 C0 D0 E0 8 8 3 34 4 2 2 1 18 81 1 1 1    

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(2 N ) comparisons O(N) memory ► Smith-Waterman Algorithm O(N 2 ) comparisons O(N 2 ) memory.  In this case the trade-off was worth it.

10 Floyd’s Algorithm ► Back to the shortest-path problem. AB C DE 8 21 8 3 4 1 1 ABCDE A0 B0 C0 D0 E0 8 8 3 34 4 2 2 1 18 81 1 1 1    

11 Floyd’s Algorithm ► First step, where can we go without using an intermediate vertex ► M k, where k the set of intermediate vertices ABCDE A0831 B8042 C34011 D1108 E2180 AB C DE 8 21 8 3 4 1 1 ABCDEA0831 B8042 C34011 D1108 E2180 M {}

12 Floyd’s Algorithm ► Second step, where can we go if we use A as an intermediate vertices ABCDE A0831 B8042 C34011 D1108 E2180 AB C DE 8 21 8 3 4 1 1 ABCDEA0831 B80492 C34011 D19108 E2180 M {A}

13 Floyd’s Algorithm ► Third step, where can we go if we use A and B as intermediate vertices ABCDE A0831 B8042 C34011 D1108 E2180 AB C DE 8 21 8 3 4 1 1 ABCDEA083110 B80492 C34011 D19108 E102180 M {A,B}

14 Floyd’s Algorithm ► Fourth step, where can we go if we use A, B, and C as intermediate vertices ► You tell me! ABCDE A0831 B8042 C34011 D1108 E2180 AB C DE 8 21 8 3 4 1 1 ABCDEA083110 B80492 C34011 D19108 E102180 M {A,B,C}

15 Floyd’s Algorithm ► Fourth step, where can we go if we use A, B, and C as intermediate vertices ABCDE A0831 B8042 C34011 D1108 E2180 AB C DE 8 21 8 3 4 1 1 ABCDEA07314 B70452 C34011 D15102 E42120 M {A,B,C}

16 Floyd’s Algorithm ► Fifth step, where can we go if we use A, B, C, and D as intermediate vertices ► You tell me! ABCDE A0831 B8042 C34011 D1108 E2180 AB C DE 8 21 8 3 4 1 1 ABCDEA07214 B70452 C24011 D15102 E42120 M {A,B,C,D}

17 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. ABCDE A0831 B8042 C34011 D1108 E2180 AB C DE 8 21 8 3 4 1 1 ABCDEA06213 B60452 C24011 D15102 E32120 M {A,B,C,D}

18 Floyd’s Algorithm ► Final step, where can we go if we use all the vertices as intermediates. ► You tell me! ABCDE A0831 B8042 C34011 D1108 E2180 AB C DE 8 21 8 3 4 1 1 ABCDEA06213 B60452 C24011 D15102 E32120 M {A,B,C,D,E}

19 Floyd’s Algorithm ► Final step, where can we go if we use all the vertices as intermediates. ► You tell me! ABCDE A0831 B8042 C34011 D1108 E2180 AB C DE 8 21 8 3 4 1 1 ABCDEA05213 B50352 C23011 D15102 E32120 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 Floyd’s Algorithm AB C DE 8 21 8 3 4 1 1 ABCDE A0831 B8042 C34011 D1108 E2180 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]


Download ppt "Floyd’s Algorithm (shortest-path problem) Section 8.2."

Similar presentations


Ads by Google