Presentation is loading. Please wait.

Presentation is loading. Please wait.

Floyd’s Algorithm (shortest-path problem)

Similar presentations


Presentation on theme: "Floyd’s Algorithm (shortest-path problem)"— Presentation transcript:

1 Floyd’s Algorithm (shortest-path problem)

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 BTW, Dijkstra’s algorithm solves this one The shortest path between any pair of vertices

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 The shortest path between any pair of vertices Floyd’s algorithm solves this problem

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 Where there is no direct route use an ∞ symbol 8 - E _ D C B A 8 3 4 2 1 A B 1 3 4 2 C 1 1 D E 8

7 Complete initial route table, copying column vertices
- E _ D C B A 8 3 4 2 1 E D C B A Distance Matrix Route Table

8 First Iteration A B C D E - E D C B A 8 3 4 2 1 ∞
Sum of shaded values is compared to unshaded value in initial table If smaller value is updated Matching positions in route table are updated to A - E _ D C B A A B C D E 8 3 1 A A B C D E 8 4 2 B A B C D 9 A E 1 C A B C D E 3 4 1 1 9 1 8 D A B A C D E 2 1 8 E A B C D E

9 Second Iteration A B C D E A B C D E - E D C B A 8 3 4 2 1 ∞
Shade the second row and column of the previous table Sum of shaded values is compared to unshaded value in initial table Route table is updated with vertex B A B C D E A B C D E A _ 8 3 1 10 A A B C D B E B 8 _ 4 9 2 B A B C A E C _ 1 C A B C D E 3 4 1 D 1 1 9 _ 8 D A A C D E E 10 2 1 8 - E B A B C D E

10 Third Iteration A B C D E A B C D E - E D C B A 8 3 4 2 1 ∞
Shade the third row and column of the previous table Repeat as above…… Route table is updated with vertex C A B C D E A B C D E A _ 7 A A B C D C B 8 3 1 10 4 C B 8 7 _ 4 5 9 2 B C A B C A C E C C A B C D E 3 4 _ 1 1 D 1 5 1 _ 8 2 D A A C C D E 9 C E 10 4 2 1 8 2 - E C B B C D C E

11 Fourth Iteration A B C D E A B C D E - E D C B A 8 3 4 2 1 ∞
Shade the fourth row and column of the previous table Repeat as above…… Route table is updated with vertex D A B C D E A B C D E A _ 7 6 3 2 1 4 A A D C 3 D C D C D B 7 6 _ 4 5 2 B D C B C C E C 2 C D A B C D E 3 4 _ 1 1 D 1 5 1 _ 2 D A C C D C E 3 4 2 1 2 - E C D B C C E

12 Fifth Iteration A B C D E A B C D E - E D C B A 8 3 4 2 1 ∞
Repeat as above…… A B C D E A B C D E A _ 5 6 2 A A D D D D 1 3 E B 6 5 _ 4 3 4 5 2 B D E B C E E C E C 2 3 _ 1 C D B E C D E 4 1 D 1 4 5 1 _ 2 D A E C C D C E 3 2 1 2 - E D B C C E

13 Shortest Routes A B C D E A B C D E
From the final route table find the shortest route between AE AE -> look at row A, column E in route table -> via D ADE -> look at row A, column D in route table -> no detour -> look at row D, column E in route table -> via C ADCE -> look at row C, column E in route table -> no detour ADCE is the shortest route A B C D E A B C D E A _ 5 2 3 A A E D D D 1 B 5 _ 3 4 B E 2 B E E E C 2 3 _ 1 C D E C D E 1 D 1 4 1 _ 2 D A E C D C E 3 2 1 2 _ E D B C C E From the final distance table find the shortest distance AE -> look at row A, column E in distance table -> distance = 3

14 Floyd’s Algorithm ADCE is the shortest route between AE = 3
8 2 1 3 4 ADCE is the shortest route between AE = 3 Find’s the shortest distance between any pair of vertices It may be used with negative weights provided there is no cycle with a total negative weight Preferred in dense graphs (lot of edges vs vertices)


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

Similar presentations


Ads by Google