 # Incorrect implementations of the Floyd--Warshall algorithm give correct solutions after three repeats

The Floyd--Warshall algorithm is a well-known algorithm for the all-pairs shortest path problem that is simply implemented by triply nested loops. In this study, we show that the incorrect implementations of the Floyd--Warshall algorithm that misorder the triply nested loops give correct solutions if these are repeated three times.

## Authors

##### This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

## 1 Introduction

The Floyd–Warshall algorithm is a well-known algorithm for the all-pairs shortest path problem cormen2009introduction ; floyd1962algorithm . Let be a complete directed graph, where is the set of vertices, and let be the length of the edges. We assume that has no negative cycles. The Floyd–Warshall algorithm maintains array d of size initialized by for all , and performs triply nested loops as shown in Algorithm 1. Then, eventually stores the shortest path distances for all .

A common mistake in implementing the Floyd–Warshall algorithm is to misorder the triply nested loops. The correct order is KIJ, and the incorrect versions, which are referred as IJK algorithm and IKJ algorithm, are shown in Algorithm 2 and Algorithm 3, respectively. These incorrect versions do not give correct solutions for some instance. However, we can prove that if these are repeated three times, we obtain the correct solutions. More precisely, we obtain the following theorems.

###### Theorem 1.

If we repeat IJK algorithm three times, it solves the all-pairs shortest path problem. Conversely, there exists an instance that needs three repeats to obtain a correct solution.

###### Theorem 2.

If we repeat IKJ algorithm two times, it solves the all-pairs shortest path problem. Conversely, there exists an instance that needs two repeats to obtain a correct solution.

It would be emphasized that these fixes (repeating incorrect algorithms three times) have the same time complexity as the correct Floyd–Warshall algorithm up to constant factors. Therefore, our results suggest that, if one is confused by the order of the triply nested loops, one can repeat the procedure three times just to be safe.

## 2 Proofs

Both theorems are proved in a similar way. The converse parts (existence of bad instances) are proved by the exhaustive search, and such instances are shown in Figures 1 and 2.

To prove the if part, we regard the algorithms as graph modification processes. Initially, we have the complete directed graph with edge length d initialized by . Each update modifies the edge length from to by the length of the path if it is shorter than the current length. It is important that this modification does not change the shortest path distances.

In the following, we fix arbitrary vertices and prove that, after several run of the algorithms, stores the shortest path distance from to . Since and are arbitrary, this proves the theorems. We take arbitrary shortest path from to (i.e., and ) and analyze the structure of the shortest path after single run of the algorithms.

We first prove Theorem 1. We use the following lemma. For integers and with , we denote by .

###### Lemma 3.

Let be an interval such that and for all . After the loop at , stores the shortest path distance from to .

###### Proof.

We prove this lemma by the induction on the length of the interval . If the length is at most three, the lemma immediately holds. In general case, let be such that is the highest in . Then, and satisfy the condition of the lemma. By the induction hypothesis, before the loop at , stores the shortest path distance from to . Also, by the induction hypothesis, after the loop at , stores the shortest path distance from to . Here, since , the update of is performed after the update of . Therefore, by the update , stores the shortest path distance from to . ∎

###### Proof of Theorem 1.

By Lemma 3, after the first run of the IJK algorithm, the modified graph has a shortest path whose vertices are upper unimodal, i.e., and . After the second run of the IJK algorithm, stores the shortest path distance from to because of the sequential update for . Also, stores the shortest path distance from to because of the sequential update for . Therefore, after the third run of the IJK algorithm, stores the shortest path distance from to because of the update

We then prove Theorem 2. We use the following lemma.

###### Lemma 4.

Let be an interval such that for all . After the loop for , stores the shortest path distance from to .

###### Proof.

We prove this by the induction on the length of the interval . If the length is at most three, the lemma immediately holds. In general case, we divide the interval into , , , (i.e., and ) such that each interval satisfies the condition of the lemma. Here, we observe that . By the induction hypothesis, before the loop at , all () store the shortest path distances. Hence, after the loop at , by the sequential update for , stores the shortest path distance. ∎

###### Proof of Theorem 2.

By Lemma 4, after the first run of the IKJ algorithm, the modified graph has a shortest path whose vertices are monotonically increasing except the last one, i.e., . After the second run of the IKJ algorithm, stores the shortest path distance from to because of the sequential update for . ∎

## References

•  Thomas H Cormen, Charles E Leiserson, Ronald L Rivest, and Clifford Stein. Introduction to Algorithms. MIT Press, 2009.
•  Robert W Floyd. Algorithm 97: shortest path. Communications of the ACM, 5(6):345, 1962.