2

I am having a hard time understanding Floyd-Warshall algorithm. I know how it works as in I know how to do it by hand but I need to understand it through a computer perceptive.

FOR k <-- 1 TO N DO
    FOR i <-- 1 TO N DO
        FOR j <-- TO N DO 
            IF Djk + Dkj < DiJ THEN
                Dij <-- djk + dkj 

k, i and j are variables for iteration and and it iterates till n value, and I guess it's a nested loop and then it looks at each node less then it finds shortest path?

keelar
  • 5,814
  • 7
  • 40
  • 79
Benjamin
  • 85
  • 1
  • 10

2 Answers2

4

A grossly simplified meaning of k in Floyd-Warshall is a "way point" in the graph. The last two lines could be interpreted as follows: "If you can get from i to k and then from k to j faster than from i to j through any path that you found so far, then the path from i to j through k becomes the new shortest path".

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

K represents an intermediate vertex. So, the outer loop basically says let the Kth vertex be the intermediate stopping point. The inner two loops then check each item in the adjacency matrix and checks if the distance using K ( using K as an intermediate vertex ) is less than the distance of going from vertex i to j ( the number in position i, j in the adjacency matrix ). If the distance is smaller then update d[i,j].

Rob L
  • 3,073
  • 6
  • 31
  • 61