Download - floyds algorithm
-
8/3/2019 floyds algorithm
1/48
page 1
M
B
L
AX
48
20
10 19
18
710
23
Floyds Algorithm can be used to help solve Travelling Salesman
and other shortest routes problems.
Floyds uses a matrix form.
-
8/3/2019 floyds algorithm
2/48
page 2
D( 0 ) R( 0 )
This is the distance matrix.
It initially shows the direct
distance between one vertex
and the others. The infinity sign
denotes no direct route.
This is the route matrix.
It will eventually show the
next vertex that needs to be
taken on route to finding theshortest route to another vertex.
To view the process step by step click here
To view the completed matrices click here
A M L B X
A 23 10 18
M 23 10 48
L 10 10 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
g
g
g
g
g g
g
To look back at the network click here:
g
g
-
8/3/2019 floyds algorithm
3/48
D( 0 ) R( 0 )
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
Click here to see step:
A M L B X
A 23 10 18
M 23 10 48
L 10 10 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
g g
g g
g
g g
g g
-
8/3/2019 floyds algorithm
4/48
D( 0 ) R( 0 )
Click here to see step:
23 + 23 = 46, less than so
change.
A M L B X
A 23 10 18
M 23 10 48
L 10 10 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
g
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g
g g
g g
-
8/3/2019 floyds algorithm
5/48
D( 0 ) R( 0 )
Click here to see step:
23 + 23 = 46, less than so
change.
A M L B X
A 23 10 18
M 23 46 10 48
L 10 10 19 7
B 48 19 20
X 18 7 20
g
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g
g
g g
g g
-
8/3/2019 floyds algorithm
6/48
D( 0 ) R( 0 )
Click here to see step:
10 + 23 = 33, so we leave this
item
A M L B X
A 23 10 18
M 23 46 10 48
L 10 10 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g
g
g g
g g
-
8/3/2019 floyds algorithm
7/48
D( 0 ) R( 0 )
Click here to see step:
When is involved we leave
the item.g
A M L B X
A 23 10 18
M 23 46 10 48
L 10 10 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g
g
g g
g g
-
8/3/2019 floyds algorithm
8/48
D( 0 ) R( 0 )
Click here to see step:
18 + 23 = 41, so we replace the
item.
A M L B X
A 23 10 18
M 23 46 10 48
L 10 10 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g
g
g g
g g
-
8/3/2019 floyds algorithm
9/48
D( 0 ) R( 0 )
Click here to see step:
18 + 23 = 41, so we replace the
item.
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 19 7
B 48 19 20
X 18 7 20
C A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g
g g
g g
-
8/3/2019 floyds algorithm
10/48
D( 0 ) R( 0 )
Click here to see step:
23 + 10 = 33, so we leave thisitem.
48 g41
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 19 7
B 48 19 20
X 18 7 20
C A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g
g g
g g
-
8/3/2019 floyds algorithm
11/48
D( 0 ) R( 0 )
Click here to see step:
10 + 10 = 20, so we replace thisitem.
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 7 20
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g g
-
8/3/2019 floyds algorithm
12/48
D( 0 ) R( 0 )
Click here to see step:
Infinity is involved so we leavethis item.
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g g
-
8/3/2019 floyds algorithm
13/48
D( 0 ) R( 0 )
Click here to see step:
18 + 10 = 28 which is more than7 so we leave this item.
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g g
-
8/3/2019 floyds algorithm
14/48
D( 0 ) R( 0 )
Click here to see step:
Infinity is involved so we leavethis item.
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 7 20
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g g
-
8/3/2019 floyds algorithm
15/48
D( 0 ) R( 0 )
Click here to see step:
Infinity is involved so we leavethis item.
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g g
-
8/3/2019 floyds algorithm
16/48
D( 0 ) R( 0 )
Click here to see step:
Infinity is involved so we leavethis item.
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g g
-
8/3/2019 floyds algorithm
17/48
D( 0 ) R( 0 )
Click here to see step:
Infinity is involved so we leavethis item.
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g g
-
8/3/2019 floyds algorithm
18/48
D( 0 ) R( 0 )
Click here to see step:
We replace this item with23 + 18 = 41
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g g
-
8/3/2019 floyds algorithm
19/48
D( 0 ) R( 0 )
Click here to see step:
We replace this item with23 + 18 = 41
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g
-
8/3/2019 floyds algorithm
20/48
D( 0 ) R( 0 )
Click here to see step:
10 + 18 = 28, 7 is less than thisso leave it.
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g
-
8/3/2019 floyds algorithm
21/48
D( 0 ) R( 0 )
Click here to see step:
Infinity is involved so leave thisitem.
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
g
-
8/3/2019 floyds algorithm
22/48
D( 0 ) R( 0 )
Click here to see step:
18 + 18 = 36, so replace thisitem.
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5g
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
-
8/3/2019 floyds algorithm
23/48
D( 0 ) R( 0 )
Click here to see step:
18 + 18 = 36, so replace thisitem.
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20 36
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
We highlight the first column and row of the Distance matrix and compare
all other items with the sum of the items highlighted in the same row and
column.
If the sum is less than the item then it should be replaced with the sum.
g g
g g
-
8/3/2019 floyds algorithm
24/48
D( 0 ) R( 0 )
Click here to see step:
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20 36
A M L B X
A 1 2 3 4 5
M 1 2 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
g g
g g
-
8/3/2019 floyds algorithm
25/48
R( 0 )
Click here to see step:
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20 36
A M L B X
A 1 2 3 4 5
M 1 1 3 4 5
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
D( 0 )
g g
g g
-
8/3/2019 floyds algorithm
26/48
R( 0 )
Click here to see step:
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20 36
D( 0 )
A M L B X
A 1 2 3 4 5
M 1 1 3 4 1
L 1 2 3 4 5
B 1 2 3 4 5
X 1 2 3 4 5
g g
g g
-
8/3/2019 floyds algorithm
27/48
R( 0 )
Click here to see step:
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20 36
D( 0 )
A M L B X
A 1 2 3 4 5
M 1 1 3 4 1
L 1 2 1 4 5
B 1 2 3 4 5
X 1 2 3 4 5
g g
g g
-
8/3/2019 floyds algorithm
28/48
R( 0 )
Click here to see step:
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20 36
D( 0 )
A M L B X
A 1 2 3 4 5
M 1 1 3 4 1
L 1 2 1 4 5
B 1 2 3 4 5
X 1 1 3 4 5
g g
g g
-
8/3/2019 floyds algorithm
29/48
R( 0 )
Click here to see step:
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20 36
D( 0 )
A M L B X
A 1 2 3 4 5
M 1 1 3 4 1
L 1 2 1 4 5
B 1 2 3 4 5
X 1 1 3 4 1
g g
g g
-
8/3/2019 floyds algorithm
30/48
We have now completed one iteration. We rename the new matrices:
Click here to see step:
Subsequent iterations are now shown completed:
Click here to see final matrices:
R( 1 )D( 1 )
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20 36
A M L B X
A 1 2 3 4 5
M 1 1 3 4 1
L 1 2 1 4 5
B 1 2 3 4 5
X 1 1 3 4 1
g g
g g
-
8/3/2019 floyds algorithm
31/48
Click here to see step:
Subsequent iterations are now shown completed:
Click here to see final matrices:
R( 1 )D( 1 )
A M L B X
A 23 10 18
M 23 46 10 48 41
L 10 10 20 19 7
B 48 19 20
X 18 41 7 20 36
A M L B X
A 1 2 3 4 5
M 1 1 3 4 1
L 1 2 1 4 5
B 1 2 3 4 5
X 1 1 3 4 1
g
g g
g
-
8/3/2019 floyds algorithm
32/48
D( 1 ) R( 1 )
The items have been altered accordingly:
Click here to see step:
Subsequent iterations are now shown completed:
Click here to see final matrices:
A M L B X
A 46 23 10 71 18
M 23 46 10 48 41
L 10 10 20 19 7
B 71 48 19 96 20
X 18 41 7 20 36
A M L B X
A 1 2 3 4 5
M 1 1 3 4 1
L 1 2 1 4 5
B 1 2 3 4 5
X 1 1 3 4 1
-
8/3/2019 floyds algorithm
33/48
D( 2 ) R( 2 )
We can now rename the matrices:
Click here to see step:
Click here to see final matrices:
A M L B X
A 46 23 10 71 18
M 23 46 10 48 41
L 10 10 20 19 7
B 71 48 19 96 20
X 18 41 7 20 36
A M L B X
A 2 2 3 2 5
M 1 1 3 4 1
L 1 2 1 4 5
B 2 2 3 2 5
X 1 1 3 4 1
-
8/3/2019 floyds algorithm
34/48
D( 2 ) R( 2 )
Next iteration:
Click here to see step:
Click here to see final matrices:
A M L B X
A 46 23 10 71 18
M 23 46 10 48 41
L 10 10 20 19 7
B 71 48 19 96 20
X 18 41 7 20 36
A M L B X
A 2 2 3 2 5
M 1 1 3 4 1
L 1 2 1 4 5
B 2 2 3 2 5
X 1 1 3 4 1
-
8/3/2019 floyds algorithm
35/48
D( 2 ) R( 2 )
Next iteration, the items are altered appropriately :
Click here to see step:
Click here to see final matrices:
A M L B X
A 20 20 10 29 17
M 20 20 10 29 17
L 10 10 20 19 7
B 29 29 19 38 20
X 17 17 7 20 14
A M L B X
A 3 3 3 3 3
M 3 3 3 3 3
L 1 2 1 4 5
B 3 3 3 3 5
X 3 3 3 4 3
-
8/3/2019 floyds algorithm
36/48
D( 3 ) R( 3 )
The matrices are renamed :
Click here to see step:
Click here to see final matrices:
A M L B X
A 20 20 10 29 17
M 20 20 10 29 17
L 10 10 20 19 7
B 29 29 19 38 20
X 17 17 7 20 14
A M L B X
A 3 3 3 3 3
M 3 3 3 3 3
L 1 2 1 4 5
B 3 3 3 3 5
X 3 3 3 4 3
-
8/3/2019 floyds algorithm
37/48
The next iteration :
Click here to see step:
Click here to see final matrices:
D( 3 ) R( 3 )
A M L B X
A 20 20 10 29 17
M 20 20 10 29 17
L 10 10 20 19 7
B 29 29 19 38 20
X 17 17 7 20 14
A M L B X
A 3 3 3 3 3
M 3 3 3 3 3
L 1 2 1 4 5
B 3 3 3 3 5
X 3 3 3 4 3
-
8/3/2019 floyds algorithm
38/48
In this iteration, you do not need to change any of the items, so we go
onto the next iteration :
Click here to see step:
Click here to see final matrices:
D( 3 ) R( 3 )
A M L B X
A 20 20 10 29 17
M 20 20 10 29 17
L 10 10 20 19 7
B 29 29 19 38 20
X 17 17 7 20 14
A M L B X
A 3 3 3 3 3
M 3 3 3 3 3
L 1 2 1 4 5
B 3 3 3 3 5
X 3 3 3 4 3
-
8/3/2019 floyds algorithm
39/48
Click here to see step:
Click here to see final matrices:
A M L B X
A 20 20 10 29 17
M 20 20 10 29 17
L 10 10 20 19 7
B 29 29 19 38 20
X 17 17 7 20 14
A M L B X
A 3 3 3 3 3
M 3 3 3 3 3
L 1 2 1 4 5
B 3 3 3 3 5
X 3 3 3 4 3
D( 4 ) R( 4 )
-
8/3/2019 floyds algorithm
40/48
Click here to see step:
Click here to see final matrices:
There is only one item that we need to change in this
case:
D( 4 ) R( 4 )
A M L B X
A 20 20 10 29 17
M 20 20 10 29 17
L 10 10 14 19 7
B 29 29 19 38 20
X 17 17 7 20 14
A M L B X
A 3 3 3 3 3
M 3 3 3 3 3
L 1 2 1 4 5
B 3 3 3 3 5
X 3 3 3 4 3
-
8/3/2019 floyds algorithm
41/48
These are the final matrices, remember that you can
use them to redraw the original network. You can then
use this to help us solve travelling salesman
problems:
Click here to see new network:
D( 5 ) R( 5 )
A M L B X
A 20 20 10 29 17
M 20 20 10 29 17
L 10 10 14 19 7
B 29 29 19 38 20
X 17 17 7 20 14
A M L B X
A 3 3 3 3 3
M 3 3 3 3 3
L 1 2 5 4 5
B 3 3 3 3 5
X 3 3 3 4 3
-
8/3/2019 floyds algorithm
42/48
page 1
M
B
L
AX
29
20
10 19
17
710
20
29
17
This network now gives you a better idea of the quickest routes.
Click below to try a question:
The route matrix gives us an idea about the next vertex to visit on route -
1 represents A, 2 - M, etc.
-
8/3/2019 floyds algorithm
43/48
1
5
4
3
2
75 35
32
15
40
30
70
Try this one! Click below when you have completed it to
check the answers:
-
8/3/2019 floyds algorithm
44/48
1 2 3 4 5
1 60 30 40 45 77
2 30 30 50 15 47
3 40 50 70 35 67
4 45 15 35 30 32
5 77 47 67 32 64
1 2 3 4 5
1 2 2 3 2 2
2 1 4 4 4 4
3 1 4 4 4 4
4 2 2 3 2 5
5 4 4 4 4 4
D( 5 ) R( 5 )
These are the completed matrices. Are yours correct?
-
8/3/2019 floyds algorithm
45/48
1
5
4
3
2
20 15 12
35
50
50
10
Qu2.
Answers.
-
8/3/2019 floyds algorithm
46/48
D( 5 ) R( 5 )
These are the completed matrices. Are yours correct?
1 2 3 4 5
1 100 50 50 65 60
2 50 40 20 35 30
3 50 20 20 15 10
4 65 35 15 24 12
5 60 30 10 12 20
1 2 3 4 5
1 2 2 3 3 3
2 1 3 3 4 3
3 1 2 5 4 5
4 3 2 3 5 5
5 3 3 3 4 3
-
8/3/2019 floyds algorithm
47/48
1
4
3
2
8
3 5
4
3
2
Qu2.
Answers.
-
8/3/2019 floyds algorithm
48/48
D( 4 ) R( 4 )
These are the completed matrices. Are yours correct?
1 2 3 4
1 6 3 6 4
2 3 6 5 3
3 6 5 4 2
4 4 3 2 4
1 2 3 4
1 2 2 4 4
2 1 1 3 4
3 4 2 4 4
4 1 2 3 3