Notations we will be using

There are 4 vertices in the graph so, our tables (Distance and Sequence) will have 4 rows and 4 columns.

Distance table D

This table holds the weight of the respective edges connecting vertices of the graph. So, if there in an edge u --> v connecting vertex u to vertex v and having weight w then we will fill the distance table D[u][v] = w. If there is no edge connecting any two vertex u to v then in that case we will fill D[u][v] = INFINITY.

D0

1

2

3

4

1

2

3

4

From the graph above we will get the following distance table.

D0

1

2

3

4

1

-

5

1

2

2

5

-

3

INF

3

1

3

-

4

4

2

INF

4

-

INF means INFINITY

Sequence table S

This table holds the vertex that will be used to find the shortest path to reach from vertex u to vertex v.

S0

1

2

3

4

1

2

3

4

From the graph above we will get the following sequence table.

S0

1

2

3

4

1

-

2

3

4

2

1

-

3

4

3

1

2

-

4

4

1

2

3

-

INF means INFINITY

Important Condition

We will fill the cell Cij in distance table Dk using the following condition.

Is dij > dik + dkj [in distance table Dk-1]
If YES then fill the cell Cij in Dk table with the value dik + dkj of Dk-1 table
If NO then fill the cell Cij in Dk table with the value dij of Dk-1 table
where
i = row number
j = column number
k = iteration number
i.e., we will always fill the cell Cij in Dk table with the smallest value.

Note! If a graph has N vertices then we will be iterating N times.

The graph has 4 vertices so, we will be iterating 4 times.

Solution

After completing the 4 iterations we will get the following distance array.