Sie sind auf Seite 1von 2

Floyd’s algorithm

Illustration of the two methods

A 4 B

4 2
18

C D
5

Initial distance matrix Initial route matrix

A B C D A B C D
A - 4 ∞ 7 A A B C D
B 4 - 18 ∞ B A B C D
C 4 18 - ∞ C A B C D
D 7 2 5 - D A B C D

1st iteration (A) Method 1 Method 2


A B C D A B C D A B C D
A - 4 ∞ 7 A A B C D A A B C D
B 4 - 18 11 B A B C A B A B C A
C 4 8 - 11 C A A C A C A A C A
D 7 2 5 - D A B C D D A B C D
No difference
nd
2 iteration (B)
A B C D A B C D A B C D
A - 4 22 7 A A B B D A A B B D
B 4 - 18 11 B A B C A B A B C A
C 4 8 - 11 C A A C A C A A C A
D 6 2 5 - D B B C D D B B C D
No difference
rd
No changes on 3 iteration (C)

4th iteration (D)


A B C D A B C D A B C D
A - 4 12 7 A A B D D A A B D D
B 4 - 16 11 B A B D A B A B A A
C 4 8 - 11 C A A C A C A A C A
D 6 2 5 - D B B C D D B B C D

Different value in row B column C


The example over the page illustrates the differences between the two methods for applying Floyd’s
algorithm.
Both methods can be used to find the shortest route between any two vertices. All the distance
matrices will be the same whichever method is being used.
The methods differ in the way that the route matrices are completed. The difference occurs when a
shorter route between two vertices is found which passes through two or more other vertices.
In the example the route matrices are identical for the first three iterations. In the fourth iteration a
shorter route from B to C is found. Consequently the value in row B column C must be replaced.

Method 1 Method 2
The value is replaced by the letter associated The value is replaced with the letter in row B
with this iteration, i.e. D column D

The steps for constructing the shortest route from B to C from the final route matrix are as follows.

Method 1 Method 2
Check the value in row B column C (D) Check the value in row B column C (A)
So the last leg of the route is D → C So the first leg of the route is B → A
Check the value in row B column D (A) Check the value in row A column C (D)
The last 2 legs of the route are A → D → C The first 2 legs of the route are B → A → D
Check the value in row B column A (A) Check the value in row D column C (C)
The route is B → A → D → C The route is B → A → D → C

Das könnte Ihnen auch gefallen