Pairs Shortest Paths for Graphs with Small Integer Length Edges
Total Page:16
File Type:pdf, Size:1020Kb
Journal of Computer and System Sciences 1385 journal of computer and system sciences 54, 243254 (1997) article no. SS971385 View metadata,All Pairs citation and Shortest similar papers at Paths core.ac.uk for Graphs with Small Integer Length Edgesbrought to you by CORE provided by Elsevier - Publisher Connector Zvi Galil* Department of Computer Science, Tel-Aviv University, Tel Aviv, Israel and Columbia University, New York, New York 10027 and Oded Margalit Department of Computer Science, Tel-Aviv University, Tel Aviv, Israel Received August 26, 1993; revised May 24, 1995 P def l length of P is defined as dij = r=1 dkr&1kr . For every two The authors have solved the all pairs shortest distances (APSD) vertices v and v , on the path P, where \ =k and \1 \2 1 +1 problem for graphs with integer edge lengths. Our algorithm is sub- P \2=k+ , +1<+2 , we define d as the length of the sub- cubic for edge lengths of small (M) absolute value. In this paper we 2 \1 \2 path from v to v : +2 d . show how to transform these algorithms to solve the all pairs shortest \1 \2 r=+1+1 kr&1kr paths (APSP), in the same time complexity, up to a polylogarithmic We use the graph G and its edge distance matrix D inter- factor. For n=|V| the number of vertices, M the bound on edge length, changingly, i.e., we refer to ``paths'' in D which should be and | the exponent of matrix multiplication, we get the following read as ``paths in the corresponding graph G.'' A cycle is a results: 1. A directed nonnegative APSP(n, M) algorithm which runs in path from v to v .Anegative cycle is a cycle whose length O (T(n, M)) time, where i i is negative. M (|&1)Â2n(3+|)Â2, 1Mn(3&|)Â(|+1) Given a matrix X, we use the notation X for the element T(n, m)= ij {Mn(5|&3)Â(|+1), n(3&|)Â(|+1)Mn2(3&|)Â(|+1). in the ith row and jth column of X; There are two excep- tions: D and D*, where we use d and d*, respectively. 2. An undirected APSP(n, M) algorithm which runs in O (M (|+1)Â2n| ij ij log(Mn)) time. 3. A general APSP(n, M) algorithm which runs in The all pairs shortest distances (APSD) problem is the (3+|)Â2 O ((Mn) ). ] 1997 Academic Press problem of finding, for every pair of vertices vi and vj in V, the length of a shortest nonempty path from vi to vj .We n denote the solution matrix by D*=[d*ij ] i, j=1. 1. INTRODUCTION The all pairs shortest paths (APSP) problem is the problem of finding, not only the distances but a charac- In this paper we show how to change our algorithms for terization of all the shortest paths as well. the all pairs shortest distances (APSD) to solve the all pairs We use the notations APSD (n, M) and APSP (n, M) for shortest paths (APSP) problem. We do it for several cases the problems on graphs with n vertices and the edge lengths and the algorithms are as efficient as the corresponding are either or integers whose absolute value is bounded APSD ones in each case (up to a polylogarithmic factor). by M. We use fast witnessed Boolean matrix multiplications (see In the problems above we exclude empty paths. This [3]). is without loss of generality, since we can solve the prob- Let G=(V, E) be a directed graph with n=|V| vertices lem which allows empty paths by first solving the n problem without them and then we update the solution [v1, v2, ..., vn] and m=|E| edges. Let D=[dij] i, j=1 be a distance function from the edges to the real numbers with by taking d$*=minii [dii*, 0]. Updating the paths (not only the distances) is trivial too. .(dij= if and only if vi Ä vj is not an edge in E). We def Let A be an integer matrix and let a be an integer. define dii = 0. The notation [Aa] stands for a Boolean matrix defined A path P from vi to vj in G is a sequence of vertices vi= by vk0 , vk1 , ..., vkl=vj . The number of edges in the path is defined as l.Ifl=0 we call the path an empty path. The * Work partially supported by NSF Grants CCR-90-14605 and CISE 1, Aija [Aa]ij= Institutional Infrastructure Grant CDA-90-24735. {0, otherwise. 243 0022-0000Â97 25.00 Copyright 1997 by Academic Press All rights of reproduction in any form reserved. File: 571J 138501 . By:CV . Date:01:04:97 . Time:12:56 LOP8M. V8.0. Page 01:01 Codes: 5954 Signs: 3869 . Length: 60 pic 11 pts, 257 mm 244 GALIL AND MARGALIT Similarly define the notations [A=a], [A # B] for a set B, with a vertex vk on the path and a negative cycle containing etc. We also use relations on matrices which are defined vk (i, j, and k need not be distinct). This leads to the need to elementwise. AB, for example, holds if and only if define witnesses for paths, not necessarily shortest paths. Aij Bij for all i and j. We use our results on transitive closure in [3]. Let A and B be n_n Boolean matrices. Let C be the n_n We modify the algorithms of [1, 2] for the APSD Boolean matrix product of A and B be denoted by C=A } B problem. The main idea is to run the appropriate algorithm, n and defined as Cij=k=1Aik 7 Bkj . The trivial way to com- but to change the Boolean matrix multiplications to a wit- pute C takes 3(n3) time. There are faster ways to compute nessed Boolean matrix multiplication. This works fine for it. Denote the time complexity of the fastest n_n matrix the positive case (see Section 2). In the undirected case we multiplication over a ring by M(n). Define |, the exponent make some observations and a slight modification of the of matrix multiplication, to be the infimum over all real APSD algorithm (see Section 3). The most difficult case is numbers x for which matrix multiplication of n_n matrices the general one, there we encounter some difficulties, mainly can be computed in O(nx) time. Currently, the best bound because of cycles. We overcome them by reducing the on | is |<2.376 (see [4]). Although it is not exactly true problem in several steps: first we remove negative cycles, (the infimum might not be a minimum), we assume that then other & distances; then zero length cycles; until we M(n)=O(n|). A witnessed matrix multiplication algorithm reach a graph where the cycles cause no problem. The computes in addition to the Boolean matrix multiplication details are given in Section 5. In Section 6 we briefly discuss a witness matrix W such that if Cij=1, then Wij=k such the case of large edge lengths. that Aik=Bkj=1. In [3] we showed how to compute W in O (n|) time. 2. POSITIVE EDGE LENGTHS In this paper we use the results of [2] and some other new techniques to modify the APSD algorithms to the In this section we transform solution for the positive APSP case. The naive way to represent the solution to the unweighted APSD problem into APSP one. First we sketch APSP problem is to write down the full path for every one (without proof) the different parts of the APSD algorithm of the n2 pairs. Figure 1 shows that this output can be 0(n3) 3 (for more details and proofs see [2]). Then we show how to long (more than n Â27 edges to be precise). In fact, this transform each part such that we will find witnesses for the holds for an exponential number of input graphs. (Replace paths too. each cycle in Fig. 1 by an arbitrary connected graph of nÂ3 The algorithm is: vertices.) def (3&|)Â2 One way to avoid this cubic bottleneck is to compute wit- 1. For k=1, 2, ..., L0 = n , compute [D*k] nesses for shortest paths. We use the definition of witness as follows: [D*0]=I and [D*k+1]=[D*k]} matrix W as follows: A witness for a path from vi to vj is a [D=1]. vertex vk such that dij*=dik+d*kj . This definition is not suf- (|&1)Â2 3 r (3&|)Â2 2. For r=1, 2, ..., log1.5 n , let Lr=(2) n . ficient for graphs with zero length cycles, so we define a wit- [D*L ] is the output of the previous step. Compute ness matrix W for the APSP problem to be an n_n integer 0 [D*Lr+1] from [D*Lr] using the ``separator trick'' matrix W such that as follows: We consider in turn each vertex as a source and the layered graph obtained by single source shortest paths. 1. Wij is a witness for a path from vi to vj . We take a block of consecutive layers, choose the smallest 2. For every pair of vertices v and v , there exists a finite i j one and use it as a separator. Each path that goes beyond path P=[v , v , ..., v ], where k =i and k =j, such that k0 k1 kl 0 l the separator must go through the separator. Hence we k =W for every 1rl. r kr&1j minimize over the choice of the vertex on the separator.