A new upper bound on the complexity of the all pairs shortest path problem

A new upper bound on the complexity of the all pairs shortest path problem

Information Processing Letters 43 (1992) 195-199 North-Holland 28 September 1992 A new upper bound on the complexity of the all pairs shortest path ...

262KB Sizes 0 Downloads 53 Views

Information Processing Letters 43 (1992) 195-199 North-Holland

28 September 1992

A new upper bound on the complexity of the all pairs shortest path problem * Tadao Takaoka Department of Computer Science, Ibaraki University, Hitachi, Ibaraki 316, Japan Communicated by S.G. Akl Received 18 November 1991 Revised 17 June 1992

Abstract Takaoka, T., A new upper bound on the complexity of the all pairs shortest path problem, Information Processing Letters 43 (1992) 195-199. A new algorithm is invented for the all pairs shortest path problem with O(n3(log log n / l o g n) 1/2) time on a uniform cost RAM. This is an improvement of Fredman's result for this problem by a factor of (log n / l o g log n) 1/6. A parallel algorithm for this problem on a P R A M - E R E W is also presented.

Keywords: Analysis of algorithms; graph algorithms; all pairs shortest paths

I. Introduction

The all pairs shortest path (APSP) problem is to compute shortest paths between all pairs of vertices of a directed graph with nonnegative real numbers as edge costs. We present an algorithm that computes shortest distances between all pairs of vertices, since shortest paths can be computed easily as by-products as in most other algorithms. For example, shortest paths are actually computed with Floyd's algorithm in [2, pp. 211-212]. It is well known that the time complexity of (n, n)-distance matrix multiplication (DMM) is asymptotically equal to that of the APSP problem for a graph with n vertices, as shown in [1, pp.

Correspondence to: Dr. T. Takaoka, Department of Computer Science, Ibaraki University, Hitachi, Ibaraki 316, Japan. * Partially supported by a research grant from Hitachi Engineering Co., Ltd.

202-206]. Based on this fact, Fredman [8] invented an algorithm for DMM of O(n3(log log n / l o g n) '/3) time, which is o ( n 3 ) , meaning that the APSP problem can be solved with the same time complexity. In the average case, Moffat and Takaoka [10] solved this problem with O ( n 2 log n) expected time. Our algorithm in this paper solves DMM in O(n3(log log n / l o g n) '/2) time, meaning that the APSP problem can be solved with the same time complexity. This is an asymptotic improvement of Fredman's algorithm by a factor of (log n / l o g log n ) 1/6. We use the conventional RAM model where only arithmetic operations and random accessibility with O(log n) bits are used in addition to control instructions. Dobosiewicz [7] gave a slightly better bound of

0020-0190/92/$05.00 © 1992 - Elsevier Science Publishers B.V. All rights reserved

195

Volume 43, Number 4

INFORMATION PROCESSING LETTERS

O(n3/(log /7) 1/2) with extended operations such as the normalization capability of floating point numbers in O(1) time. If edges costs are bounded by a small positive integer, the problem is solved much faster by Lingas [9] and Alon, Galil and Margalit [3], but the problems treated in these papers are regarded as being in the different category where the binary-expanded forms of distance data arc explicitly used. In this paper, operations on distance data are only additions and comparisons. Naturally we assume that the distance data arc expressed by O(log n) bits whether they may be integers or real numbers. In Section 2, the outermost framework of the algorithm is described, in which the given matrices are divided into much smaller submatrices. In Section 3, these submatrices are further divided into rectangular matrices and a method is given for multiplying these rectangular matrices by table-lookup. In Section 4, a general scheme for encoding a sequence of integers into an integer is given. This encoding scheme is used in the indexing in the table used in Section 3. In Section 5, a method for constructing the table used in Section 3 is described. In Section 6, the size of the table used in Section 3 is determined in such a way that the time for table construction be controlled within the time complexity of the main part of the algorithm. In the last section a possible parallel implementation is mentioned using the P R A M E R E W model. The base of logarithm is assumed to be two in this paper and fractions are rounded up if necessary.

2. Distance matrix multiplication by divide-andconquer Let A and B be (n, n) matrices whose components are nonnegative real numbers. The product C = A - B is defined by

cij= min {aik+bk, } ( i , j = l . . . . . n). l<~k~n

(1)

The operation in the right-hand side of (1) is called distance matrix multiplication and A and

196

28 September 1992

B are called distance matrices in this context. This is also viewed as computing n 2 inner products defined for a = ( a I. . . . . a,,) and b = (b I. . . . . b,,) by

a'b=

min { a k + b k } . 1 ~k ~n

Now we divide A, B and C into (m, m),submatrices for N = n / m as follows:

A =

B =

AII

...

AIN],

A ;'1

"'"

ANN

BI1

...

BN 1

"'"

I BJ?vl,

C = [ cll

"'"

[ C,v,,, . . . . .

BNN

eli"4 C,,:,,e

Then C can be computed by

Ci,=

rain

{A,k'Bki } (i,j= l ..... N),

(2)

I ~
where the product of submatrices is defined similarly to (1) and the "rain" operation is defined on matrices by taking the "min" operation componentwise. Since comparisons and additions of distances are performed in a pair, we omit counting the number of additions for measurement of complexity. We have N 3 multiplications of distance matrices in (2). Let us assume that each multiplication of (m, m) matrices can be done in T(m) computing time, assuming that a precomputed table is available at no cost. The time for constructing the table is reasonable when m is small and will be mentioned in Section 6. Then the total computing time is given by

O(n3/m + (n/m)3T(m)).

(3)

In the next section we show that T ( m ) = O(mZS). Thus the time given in (3) becomes

O(n3/ ~ ) .

Volume 43, Number 4

I N F O R M A T I O N PROCESSING LETTERS

3. Distance matrix multiplication by table-lookup In this section we divide (m, m)-matrices into strips for M = m / l where fm- ~< 1 ~< m as follows: A =

28 September 1992

It is obvious that it takes O(12m) time to make lists Hr., and L,., for all r and s. As observed by F r e d m a n [8], we have ai,. + brj <~ air +

b,~ ~ air - ais ~ b.~j -- bri.

He observed that the information of ordering for all i, j, r and s in the right-hand side of the above formula is sufficient to determine the product A - B by a p r e c o m p u t e d decision tree. We observe that to c o m p u t e each (i, j) c o m p o n e n t of A • B it is enough to know the above ordering for all r and s by

B =

a,r-ai,.<~b,j-b,.i~H,.,[i] where A i are (m, l) matrices and Bj are (/, m) matrices. We regard later A and B as (m, rn)submatrices in (2). Now the product C = A • B is given by C=

min

l<~k<~M

{Ak'Bk}.

(4)

We show later that A k - B k can be c o m p u t e d in O(12m) time, assuming that a p r e c o m p u t e d table is available. T h e n the right-hand side of (4) can be c o m p u t e d in time O(rn3/l + lm2). Setting l to f m , this time becomes O(mZ5). H e r e a f t e r we assume l = ~/m. Now we show that for (m, l) matrix A and (l, rn) matrix B, A .B can be c o m p u t e d in O(lZm) time. We assume that the lists

( a l , . - a l ....... a . , , . - a ..... )

(l~r
and

( b ~ l - b ~ j . . . . . b ..... -b,.m)

(l~r
are already sorted for all r and s such that 1 ~ r < s ~< l. Let E,., and F,.~ be the sorted lists, respectively. For each r and s, we merge lists E,~ and F,, to form list Gr,,. This takes O(12m) time. The time for sorting will be m e n t i o n e d in Section 6. Let H r , be the list of ranks of a ~ r - a i s ( i = 1. . . . . m) in G,.~ and L,., be the list of ranks of b , j - b , . j ( j = 1. . . . . rn) in Gr,. Let Hr,,[i] and L,.,[j] be te ith and j t h c o m p o n e n t s of H,~, and L,,,, respectively. T h e n we have

Hereafter,

we

use

~< L,.,[ j ] . a

short

cut

(5) notation

a~2al3 • •. al- t ~ to express the sequence a12a13...allaz3a24...azl...al

it.

T h e list H [ i ] = H j z [ i ] H I 3 [ i ] . . . H t ifli] is encoded into a single integer in lexicographic order as shown in the next section and h(H[i]) is assumed to represent that integer. Similarly, h(L[ j]) represents the positive integer for the list L[j] = LI2[j]LI,~[J]...Lt lflJ]. T h e time for this encoding for all H[i] and L[j] is O(12m). Then the p r e c o m p u t e d table I gives the desired index for the inner product, that is,

I [ h ( H [ i ] ) , h( L [ j ] ) ] = k k is the index for rain {aik + b k }.

(6)

l <~k <~l

After the encoding we can c o m p u t e m 2 inner products in O(rn 2) time, assuming that the prec o m p u t e d table I is available. H e n c e A k . B k in (4) can be c o m p u t e d in O(12m) time.

4. Encoding lists in lexicographic order T h e sequence of k integers x k 1...xtxc~, where 0 ~
h(Xk-l...xlxo)

=xk

l'Iz k - l +

"'" +x I ' I t + x o.

It is obvious that this m a p p i n g h is one-to-one and onto. T h e mapping h can be c o m p u t e d in 197

Volume 43, Number 4

INFORMATION PROCESSING LETTERS

O ( k ) time using Horner's method, assuming that arithmetic operations on integers up to /xk can be performed in O(1) time. We assume later that /x and k are small enough so that these operations can be done in O(1) time. We note that the inverse h ~ of h, that is, decoding can be done in O ( k ) time by successive division. The e n c o d i n g / decoding method in this section is used in this p a p e r with # and k replaced by 2m and I ( I 1)/2, respectively.

5. How to compute table I Let

H=HI2HI3...HI

11

and

L=LI2LI3...

Ll 1l be any sequences of 1 ( 1 - 1 ) / 2 integers whose values are between 0 and 2 m - 1 . Let i and j be positive integers representing H and L in lexicographic order, that is, h ( H ) = i and h(L) = j. Then I is defined by

k,

undefined,

otherwise.

This table can be used for table I in (6). The time for computing table I is given with a constant c>lby 0 ( I 2(2rn) 2(9 ) = O ( c '2'°g '") = O(c'" i,,~,,). Example. Let H and L be given in two-dimensional forms as follows, where l = 3 and m = 5: H= k=2

6 ,

L=

9 •

since H12>L~: and H2~
h ( H ) = 456,

h ( L ) = 329.

6. How to determine the size of submatrices

It is time to determine the size of submatrices in Section 1. Let rn in Section 2 be given by rn = log n / ( I o g c log log n). 198

Then the time for making the table I of this size is easily shown to be O(n) which can be absorbed in the main computing time. Substituting this value of rn for O ( n 3 / f m ) we have the overall computing time for distance matrix multiplication O(n3(log log

n/log n) 1/2)

as claimed, and consequently we have proven that the APSP can be solved in the same complexity. We note that the time for sorting to obtain the lists Er., and F,.~ in Section 3 is O ( m 25 log m). This task of sorting, which is called presort, is done for all A 0 and Bij in Section 2 in advance, taking

0 ( ( n/m)2m25 log m ) = O ( n 2 ( l o g n loglog n) '/2) time where m = O(log n / l o g log n), which is absorbed into the above main complexity.

if there is k such that Hk ~< L k , for all s > k and H, k > Lrk for all r < k,

I[i, j ] = ]

28 September 1992

7. Parallelization

The simple structure of our algorithm makes a parallelization possible. Here we use a P R A M with E R E W memory (see [4] for parallel computational models). First we separate the computation in (2) into two parts: (I) Computation of n 2 minima of N numbers. (II) C o m p u t a t i o n of N 3 products AikBkj

(i,j,k = 1..... N). We design a parallel algorithm for (I) first. According to Dekel and Sahni [5], there is a P R A M - E R E W algorithm for finding the minim u m of N numbers with O ( N / l o g N ) processors and O(log N ) time. Applying this, we have a parallel algorithm for (I) with O ( n 3 log log n/log2n) processors and O(log n) time, since the n 2 minima can be computed in parallel. Next we design a parallel algorithm for (II). In the computation of A k B k in (4), there a r e 0(• 2) independent mergings of lists of length m. Such computation is done for k = 1. . . . . m/l. There are also M - 1 " m i n " operations on (m, m)-

Volume 43, Number 4

INFORMATION PROCESSING LETTERS

matrices. It is obvious that there is a parallel algorithm for these operations with O ( m l S / log m) processors and O ( m log m) time, where 1 = f ~ . It is also obvious that the tasks of encoding and table-lookup in Section 3 can be done within the above complexities of processors and time. Since N 3 products in (II) can be computed in parallel, we have a parallel algorithm for (II) with P processors and T time, where m = ®(log n / l o g log n)

28 September 1992

This is compared with the algorithm by the combination of Dekel, Nassimi and Sahni [5] and Dekel and Sahni [6], with O ( n 3 / l o g n) processors and O(log2n) time on the same computational model. In the average case, there is a more efficient parallel algorithm by Takaoka [11] with P - O ( n 25) and T - O ( l o g log n) on a PRAMCRCW.

Acknowledgment

and

P = O((n/m)3m"5/log m) = O(n3(log log n)'/2/(log

The author thanks an anonymous referee who helped improve this p a p e r considerably. n)3/2),

T = O ( m log m ) = O(log n).

References

The lists Er~ and Fr, have to be broadcast to the computation of N 3 products of AikBkj. This is done with O(n3(log log

n)l/2/(log n) 3/2)

processors and O(log n) time since a datum can be broadcast to N locations with O ( N / l o g N ) processors and O(log N ) time. The task of table construction and presort described in Sections 5 and 6 can be clone within the above complexities. Note that the complexities of (II) dominates those of (I) and those for presort and table construction are much lower. To solve the APSP problem, we use the socalled repeated squaring instead of recursion for sequential algorithms in [1]. Since log n squarings of distance matrices solve the problem, we can say that we can solve the APSP with a parallel algorithm on a P R A M - E R E W with P processors and T time, where P = O ( n 3 ( l o g log T = O(log2n).

n)'/2/(log

n)3/2),

[1] A.V. Aho, J.E. Hopcroft and J.D. Ullman, The Design and Analysis of Computer Algorithms (Addison-Wesley, Reading, MA, 1974). [2] A.V. Aho, J.E. Hopcroft and J.D. Ullman, Data Structures and Algorithms (Addison-Wesley, Reading, MA, 1983). [3] N. Alon, Z. Galil and O. Margalit, On the exponent of the all pairs shortest path problem, in: Proc. 32nd Ann. Syrup. on Foundations of Computer Science (1992). [4] S.G. Akl, The Design and Analysis of Parallel Algorithms (Prentice-Hall, Englewood Cliffs, N J, 1989). [5] E. Dekel, D. Nassimi and S. Sahni, Parallel matrix and graph algorithms, SIAMJ. Comput. 10 (1981) 657-675. [6] E. Dekel and S. Sahni, Binary trees and parallel scheduling algorithms, IEEE Trans. Comput. 32 (1983) 307-315. [7] W. Dobosiewicz, A more efficient algorithm for rain-plus multiplication, Internat. J. Comput. Math. 32 (1990) 4960. [8] M.L. Fredman, New bounds on the complexity of the shortest path problem, SlAM J. Comput. 5 (1976) 83-89. [9] A. Lingas, Bit complexity of matrix products, lnform. Process. Lett. 38 (5) (1991) 237=242. [10] A. Moffat and T. Takaoka, An all pairs shortest path algorithm with expected running time O(n21og n), SlAM J. Comput. 16 (1987) 1023-1031. [11] T. Takaoka, An efficient parallel algorithm for the all pairs shortest path problem, WG 88, Lecture Notes in Computer Science 344 (Springer, Berlin, 1988) 276-287.

199