Journal of Graph Algorithms and Applications on the Shoshan-zwick Algorithm for the All-pairs Shortest Path Problem

The Shoshan-Zwick algorithm solves the all-pairs shortest paths problem in undirected graphs with integer edge costs in the range {1, 2,. .. , M}. It runs iñ O(M · n ω) time, where n is the number of vertices, M is the largest integer edge cost, and ω < 2.3727 is the exponent of matrix multiplication. It is the fastest known algorithm for this problem. This paper points out and corrects an error in the Shoshan-Zwick algorithm.


Introduction
The Shoshan-Zwick algorithm [22] solves the all-pairs shortest paths (APSP) problem in undirected graphs, where the edge costs are integers in the range {1, 2, . . . , M }. This is accomplished by computing O(log(M · n)) distance products of n × n matrices with elements in the range {1, 2, . . . , M }. The algorithm runs inÕ(M · n ω ) time, where ω < 2.3727 is the exponent for the fastest known matrix multiplication algorithm [29].
The APSP problem is a fundamental problem in algorithmic graph theory. Consider a graph with n nodes and m edges. For directed or undirected graphs with real edge costs, we can use known methods that run in O(m · n + n 2 · log n) time [8,11,17] and O(n 3 ) time [10]. Sub-cubic APSP algorithms have been obtained that run in O(n 3 · ((log log n)/ log n) 1/2 ) time [12], O(n 3 · log log n/ log n) time [24], O(n 3 / √ log n) time [9], O(n 3 · (log log n/ log n) 5/7 ) time [15], O(n 3 · (log log n) 2 log n) time [25], O(n 3 · log log n/ log n) time [26], O(n 3 · √ log log n/ log n) time [31], O(n 3 / log n) time [3], and O(n 3 · log log n/ log 2 n) time [16]. For undirected graphs with integer edge costs, the problem can be solved in O(m · n) time [27,28]. Fast matrix multiplication algorithms can also be used for solving the APSP problem in dense graphs with small integer edge costs. [13,21] provide algorithms that run inÕ(n ω ) time in unweighted, undirected graphs, where ω < 2.3727 is the exponent for matrix multiplication [29]. [4] improves this result with an algorithm that runs in o(m · n) time.
In this paper, we revise the Shoshan-Zwick algorithm to resolve some issues that cause the algorithm to behave erroneously. This behavior was identified when, in the process of implementing the algorithm as part of a more elaborate procedure (i.e., identifying negative cost cycles in undirected graphs), we discovered that the algorithm is not producing correct results. Since the Shoshan-Zwick algorithm is incorrect in its current version, any result that uses this algorithm as a subroutine is also incorrect. For instance, the results provided by Alon and Yuster [2], Cygan et. al [7], W. Liu el al [19], and Yuster [30] all depend on the Shoshan-Zwick algorithm. Thus, their results are currently incorrect. By applying our revision to the algorithm, the issues with the above results are resolved. We also discuss issues concerning the implementation of the Shoshan-Zwick algorithm using known sub-cubic matrix multiplication algorithms.
The principal contributions of this paper are as follows: (i) A counter-example that shows that the Shoshan-Zwick algorithm is incorrect in its current form.
(ii) A detailed explanation of where and why the algorithm fails.
(iii) A modified version of the Shoshan-Zwick algorithm that corrects the problems with the previous version.
The corrections do not affect the time complexity of the algorithm.
(iv) A discussion concerning implementing the matrix multiplication subroutine that is used in the algorithm.
The rest of the paper is organized as follows. We describe the Shoshan-Zwick algorithm in Section 2. Section 3 establishes that the published version of the algorithm is incorrect by providing a simple counter-example. The origins of this erroneous behavior is identified in Section 4. In Section 5, we present a revised version of the algorithm and provide a formal proof of correctness. In Section 6, we discuss the efficacy of the Shoshan-Zwick algorithm. Section 7 concludes the paper by summarizing our contributions and discussing avenues for future research.

The Shoshan-Zwick Algorithm
In this section, we review the Shoshan-Zwick algorithm for the APSP problem in undirected graphs with integer edge costs. Consider a graph G = (V, E), where V = {0, 1, 2, . . . , n} is the set of nodes, and E is the set of edges. The graph is represented as an n × n matrix D, where d ij is the cost of edge (i, j) if (i, j) ∈ E, d ii = 0 for 1 ≤ i ≤ n, and d ij = +∞ otherwise. Note that, without loss of generality, the edge costs are taken from the range {1, 2, . . . , M }, where M = 2 m for some m ≥ 1.
The algorithm computes a logarithmic number of distance products in order to determine the shortest paths.
Let A and B be two n × n matrices. Their distance product A ⋆ B is an n × n matrix such that: The distance product of two n × n matrices with elements within the range {1, 2, . . . , M, +∞} can be computed inÕ(M · n ω ) time [1]. The distance product of two matrices whose finite elements are taken from {1, 2, . . . , M } is a matrix whose finite elements are taken from {1, 2 . . . , 2 · M }. However, the Shoshan-Zwick algorithm is based on not allowing the range of elements in the matrices it uses to increase. Hence, if A is an n × n matrix, and a, b are two numbers such that a ≤ b, the algorithm utilizes two operations, namely clip(A, a, b) and chop(A, a, b), that produce corresponding n × n matrices such that The algorithm also defines n × n matrices A B, A¯ B, and A B such that Finally, if C = (c ij ) and P = (p ij ) are matrices, the algorithm defines the Boolean matrices (C ≥ 0) and (0 ≤ P ≤ M ) such that  16:

A Counter-Example
In this section, we provide a detailed presentation of the counter-example presented that shows that the Shoshan-Zwick algorithm is incorrect as presented in [22]. Consider the APSP problem with respect to graph G ′ presented in Figure 1.
Graph G ′ can also be represented as a 3 × 3 matrix: Figure 1: Graph G ′ of the counter-example for the Shoshan-Zwick algorithm.
We now walk through each step of the algorithm. First, l = ⌈log 2 3⌉ = 2, and m = log 2 4 = 2. This means that in the for loop in lines 3 to 5, we set D = clip(D ⋆ D, 0, 8) three times. At each such step, we get The next step is to set A 0 = D − M, which gives us In the for loop in lines 7 to 9, we compute A k = clip(A k−1 ⋆ A k−1 , −4, 4) for k = 1 and k = 2. This gives us In lines 10 to 12, we set C 2 = −4, P 2 = clip(D, 0, 4), and Q 2 = +∞. Thus, we have We now compute the for loop in lines 13 to 17. This means we run the lines twice, i.e., for k = 1 and k = 0. After this loop, we get In the for loop in lines 18 to 20, we set B k = (C k ≥ 0) for k = 1 and k = 2. This means We then set B 0 = (0 ≤ P 0 < 4), R = P 0 mod 4, and ∆ = 4 · 2 k=0 2 k · B k + R according to lines 21 to 23: The algorithm terminates by returning ∆ in line 24. The resulting shortest path costs are presented in Table 1.
However, if we examine graph G ′ (Figure 1), we find that these results are incorrect. The correct shortest paths are provided in Table 2. Therefore, the Shoshan-Zwick algorithm is incorrect.

The Errors in the Algorithm
In this section, we describe what causes the erroneous behavior of the Shoshan-Zwick algorithm. Recall that ∆ is the matrix that contains the costs of the shortest paths between all pairs of vertices after the algorithm terminates.
Moreover, let δ(i, j) denote the cost of the shortest path between nodes i and j. After the termination of the algorithm, we must have ∆ ij = δ(i, j) for any i, j ∈ {1, . . . , n}. However, as we showed in the counter-example in Section 3, it may be the case that ∆ ij = δ(i, j) for some i, j at termination. The exact errors of the algorithm are as follows: 1. R is not computed correctly.
2. B 0 is not computed correctly.
3. ∆ is not computed correctly, since M · B 0 + R is part of the sum producing it.
In the rest of this section, we illustrate what causes these errors.
It is clear from line 23 of Algorithm 2.1 that the matrices B k (for 0 ≤ k ≤ l) represent the ⌈log 2 n⌉ most significant bits of each distance. That is, while R represents the remainder of each distance modulo M . This is also illustrated in [22,Lemma 3.6], where Hence, for every i, j, we must have The first error of the algorithm arises immediately from the key observation that P 0 can have entries with negative values. This means that line 22 (that sets R ij = (P 0 ) ij mod M ) is not correctly calculating R ij = δ(i, j) mod M since δ(i, j) ≥ 0 by definition, while (P 0 ) ij can be negative. A closer examination of how P 0 obtains its negative values reveals another error of the algorithm. The following definitions are given in [22,Section 3]. Consider a set Y ⊆ [0, M · n]. Note that [0, M · n] includes any value that δ(i, j) can take, since n is the number of nodes, and M is the maximum edge cost. Let Y = ∪ p r=1 [a r , b r ], where a r ≤ b r , for 1 ≤ r ≤ p and b r < a r+1 , for 1 ≤ r < p. Let ∆ Y be an n × n matrix, whose elements are in the range {−M, . . . , M } ∪ {+∞}, such that for every 1 ≤ i, j ≤ n, we have otherwise. (2) By [22,Lemma 3.5], Recall that 2 m = M . Note that by definition of Y 0 , when calculating P 0 = ∆ Y 0 , it can only be the case that a r = b r . Moreover, b r = 2 m+1 · (r − 1) where p is such that 2 m+1 · (p − 1) ≤ M · n < 2 m+1 · p. But then: That is, ) covers all possible values that δ(i, j) may take for any i, j. Hence, Let us examine the values that (P 0 ) ij takes by equation (3): • For 2 m+1 ≤ δ(i, j) ≤ 2 m+1 + 2 m , we have (P 0 ) ij = δ(i, j) mod 2 m+1 .
More formally, equation (3) can be rewritten as follows: Moreover, equation (4) implies that while Recall now that we must have (B 0 ) ij = 1 if and only if δ(i, j) mod 2 m+1 ≥ 2 m . However, from equations (5) and (6) It is clear that in the presence of these two identified errors (in calculating R and B 0 ), the algorithm is not computing ∆ correctly.

The Revised Algorithm
In this section, we present a new version of the Shoshan-Zwick algorithm that resolves the problems illustrated in Section 4. Lines 1 to 20 of Algorithm 2.1 remain unchanged. We make the following changes to lines 21 to 24: 1. We replace B 0 withB 0 and setB 0 to (−M < P 0 < 0) in line 21.
2. We replace R withR and setR to P 0 in line 22.
Note that we have replaced B 0 and R withB 0 andR, respectively. The purpose of the change in notation is to show that these matrices no longer represent the incorrect versions from the original (erroneous) algorithm. We refer to our counter-example in Section 3. Since the algorithm is correct up to line 20, we will examine how the algorithm operates in lines 21 to 24. Hence, we setB 0 = (−4 < P 0 < 0),R = P 0 , and ∆ = 4 · 2 k=1 2 k · B k + 8 ·B 0 +R. Line 24 returns ∆. The elements of ∆ reflect the correct shortest path costs given in Table 2. Therefore, the revised algorithm works correctly for our counter-example. The corrections in the algorithm are not limited to the counter-example, as shown in the theorem below: Proof. It suffices to show that 2 · M ·B 0 +R represents what the original algorithm intended to represent with M · B 0 + R. That is, by equation (1), it suffices to show that (2 · M ·B 0 +R) ij = δ(i, j) mod 2 m+1 , for every First, we consider the case where δ(i, j) mod 2 m+1 ≤ 2 m . Equation (5) indicates that 0 ≤ (P 0 ) ij ≤ M .

Efficacy
In this section, we identify issues with implementing the Shoshan-Zwick algorithm. Recall that the algorithm runs inÕ(M · n ω ) time, where ω is the exponent for the fastest known matrix multiplication algorithm. This means that the running time of the algorithm depends on which matrix multiplication algorithm is used. We will discuss two subcubic matrix multiplication algorithms and show why it is impractical to use them in the Shoshan-Zwick implementation.
The current fastest matrix multiplication algorithm is provided by [29], where ω < 2.3727. This approach tightens the techniques used in [5], which gives a matrix multiplication algorithm where ω < 2.376. Although both matrix multiplication algorithms are theoretically efficient, neither one is practical to implement. They both provide an advantage only for matrices that are too large for modern hardware to handle [20].
We next consider Strassen's matrix multiplication algorithm [23], which runs in O(n 2.8074 ) time. Recall that the algorithm computes A · B = C by partitioning the matrices into equally sized block matrices To reduce the total number of multiplications, seven new matrices are defined as follows: With these new matrices, the block matrices of C can be redefined as The process of dividing C repeats recursively n times until the submatrices degenerate into a single number. Although Strassen's algorithm is faster than the naive O(n 3 ) matrix multiplication algorithm, we cannot directly use it in the Shoshan-Zwick algorithm. Recall that the naive approach uses matrix multiplication over the closed semi-ring {+, ·}. The Shoshan-Zwick algorithm, on the other hand, actually uses matrix multiplication over the closed semi-ring {min, +}, which is known as "funny matrix multiplication" or the "distance product" in the literature. Note that the sum operation in the naive approach is equivalent to the min operation in "funny matrix multiplication". However, Strassen's algorithm requires an additive inverse. This implies that an inverse for the min operation is needed in "funny matrix multiplication". Such an inverse does not exist. In fact, we cannot multiply matrices with less than Ω(n 3 ) operations when only the min and sum operations are allowed [1,18]. Thus, Strassen's algorithm cannot directly be used for computing shortest paths.
One potential solution is to encode a matrix used for distance products such that regular matrix multiplication works. [1] provides an approach for this conversion as follows: Suppose we want to convert an n × n matrix A to A ′ . We set a ′ ij = (n + 1) a ij −x , where x is the smallest value in A. We perform the same conversion from B to B ′ . We then obtain C ′ = A ′ · B ′ by: (n + 1) a ik +b kj −2·x .
We then use binary search to find the largest s ij such that s ij ≤ a ′ ik + b ′ kj − 2 · x, and we set c ij = s ij + 2 · x. This gives us C, which is the distance product of matrices A and B.
[1] states that the algorithm performs O(n ω ) operations on integers which are ≤ n·(n+1) 2·M , where M is the magnitude of the largest number. For large numbers, we would need O(M · log M ) operations on O(log n)-bit numbers. As a result, the total time to compute the distance product is O(M · n ω · log M ). If we apply this to the Shoshan-Zwick algorithm, the algorithm takesÕ(M 2 · n ω · log M ) time.
Although the above algorithm provides a subcubic approach for implementing the Shoshan-Zwick algorithm, it is not necessarily the most efficient implementation. This is because there exist other efficient APSP algorithms for integer edge costs. For instance, we can implement Goldberg's O(m + n · log N ) time single source shortest path algorithm [14], where N is the largest edge cost, and run it n times; one for each vertex. Goldberg's implementation is one of the currently known fastest implementations available. The resulting implementation is substantially faster compared to the Shoshan-Zwick algorithm.

Conclusion
In this paper, we revised the Shoshan-Zwick algorithm to resolve issues related to its correctness. We first provided a counter-example which shows that the algorithm is incorrect. We then identified the exact cause of the problem and presented a modified algorithm that resolves the problem. We also explained the efficacy issues of the algorithm. An interesting study would be to implement the Shoshan-Zwick algorithm and profile it with efficient APSP algorithms for graphs with integer edge costs.