6
Information Processing Letters 109 (2009) 242–247 Contents lists available at ScienceDirect Information Processing Letters www.elsevier.com/locate/ipl Faster multi-witnesses for Boolean matrix multiplication Leszek G ˛ asieniec a,,1 , Mirosław Kowaluk b,2 , Andrzej Lingas c,3 a Department of Computer Science, University of Liverpool, L69 3BX Liverpool, United Kingdom b Institute of Informatics, Warsaw University, Warsaw, Poland c Department of Computer Science, Lund University, 22100 Lund, Sweden article info Article history: Received 1 July 2008 Received in revised form 3 October 2008 Available online 31 October 2008 Communicated by C. Scheideler Keywords: Boolean matrix multiplication Witnesses for Boolean matrix product Time complexity, directed acyclic graph (dag) Lowest common ancestors in dags Algorithms Analysis of algorithms Combinatorial problems 1. Introduction The Boolean matrix multiplication stands for the matrix multiplication over the Boolean semi-ring ({1, 0}, , ). A witness of a C [i , j ] entry of the Boolean product C of two Boolean matrices A and B is any index l such that A[i , l] and B [l, j ] are equal to 1. The straightforward cubic- time algorithm for Boolean matrix multiplication yields all witnesses for each positive entry of the Boolean product. We shall denote the time required to compute the Boolean product of two n × n matrices by O (n ω ). By the algorithm of Coppersmith and Winograd for arithmetic matrix mul- tiplication [4] and the trivial reduction of Boolean matrix * Corresponding author. E-mail addresses: [email protected] (L. G ˛ asieniec), [email protected] (M. Kowaluk), [email protected] (A. Lingas). 1 Research supported in part by the Royal Society International Joint Project, IJP – 2006/R2. 2 Research supported by the grant of the Polish Ministry of Science and Higher Education N20600432/0806. 3 Research supported in part by VR grant 621-2005-4806. multiplication to 0–1-matrix multiplication, we have ω < 2.376 at present. The aforementioned sub-cubic method based on arithmetic matrix multiplication unfortunately does not exhibit witnesses explicitly. By repeatedly apply- ing the sub-cubic method, Alon and Naor have surprisingly shown that witnesses for all positive entries (one per en- try) of the Boolean product of two n × n Boolean matrices can be computed in time O (n ω ) [1]. Galil and Margalit [8] provided slightly asymptotically slower algorithm running in time O (n ω+ ) for any fixed > 0. We study here a generalization of the problem of com- puting a single witness where for each positive entry of the Boolean product we intend to determine k witnesses. We term it as the k-witness problem. More formally, for two valid indices i , j let #w(i , j ) be the number of distinct wit- nesses for the entry C [i , j ] in the Boolean product C of two Boolean matrices A and B . The k-witness problem is to compute min{k, #w(i , j )} witnesses for each entry C [i , j ] of C . The 1-witness problem occurs naturally in the design of fast algorithms for the all-pairs shortest path problem [2] and for the problem of reporting a common ancestor (if 0020-0190/$ – see front matter © 2008 Elsevier B.V. All rights reserved. doi:10.1016/j.ipl.2008.10.012

Faster multi-witnesses for Boolean matrix multiplication

Embed Size (px)

Citation preview

Information Processing Letters 109 (2009) 242–247

Contents lists available at ScienceDirect

Information Processing Letters

www.elsevier.com/locate/ipl

Faster multi-witnesses for Boolean matrix multiplication

Leszek Gasieniec a,∗,1, Mirosław Kowaluk b,2, Andrzej Lingas c,3

a Department of Computer Science, University of Liverpool, L69 3BX Liverpool, United Kingdomb Institute of Informatics, Warsaw University, Warsaw, Polandc Department of Computer Science, Lund University, 22100 Lund, Sweden

a r t i c l e i n f o

Article history:Received 1 July 2008Received in revised form 3 October 2008Available online 31 October 2008Communicated by C. Scheideler

Keywords:Boolean matrix multiplicationWitnesses for Boolean matrix productTime complexity, directed acyclic graph(dag)Lowest common ancestors in dagsAlgorithmsAnalysis of algorithmsCombinatorial problems

1. Introduction

The Boolean matrix multiplication stands for the matrixmultiplication over the Boolean semi-ring ({1, 0},∧,∨).A witness of a C[i, j] entry of the Boolean product C oftwo Boolean matrices A and B is any index l such thatA[i, l] and B[l, j] are equal to 1. The straightforward cubic-time algorithm for Boolean matrix multiplication yields allwitnesses for each positive entry of the Boolean product.We shall denote the time required to compute the Booleanproduct of two n × n matrices by O (nω). By the algorithmof Coppersmith and Winograd for arithmetic matrix mul-tiplication [4] and the trivial reduction of Boolean matrix

* Corresponding author.E-mail addresses: [email protected] (L. Gasieniec),

[email protected] (M. Kowaluk), [email protected](A. Lingas).

1 Research supported in part by the Royal Society International JointProject, IJP – 2006/R2.

2 Research supported by the grant of the Polish Ministry of Science andHigher Education N20600432/0806.

3 Research supported in part by VR grant 621-2005-4806.

0020-0190/$ – see front matter © 2008 Elsevier B.V. All rights reserved.doi:10.1016/j.ipl.2008.10.012

multiplication to 0–1-matrix multiplication, we have ω <

2.376 at present. The aforementioned sub-cubic methodbased on arithmetic matrix multiplication unfortunatelydoes not exhibit witnesses explicitly. By repeatedly apply-ing the sub-cubic method, Alon and Naor have surprisinglyshown that witnesses for all positive entries (one per en-try) of the Boolean product of two n × n Boolean matricescan be computed in time O (nω) [1]. Galil and Margalit [8]provided slightly asymptotically slower algorithm runningin time O (nω+ε) for any fixed ε > 0.

We study here a generalization of the problem of com-puting a single witness where for each positive entry ofthe Boolean product we intend to determine k witnesses.We term it as the k-witness problem. More formally, for twovalid indices i, j let #w(i, j) be the number of distinct wit-nesses for the entry C[i, j] in the Boolean product C of twoBoolean matrices A and B. The k-witness problem is tocompute min{k,#w(i, j)} witnesses for each entry C[i, j]of C .

The 1-witness problem occurs naturally in the design offast algorithms for the all-pairs shortest path problem [2]and for the problem of reporting a common ancestor (if

L. Gasieniec et al. / Information Processing Letters 109 (2009) 242–247 243

possible) for all pairs of vertices in an acyclic directedgraph (dag for short) [6]. Its generalization, the k-witnessproblem arises in the design of efficient algorithms forthe all-pairs k-shortest path problem and for the prob-lem of reporting k common ancestors (if possible) for allpairs of vertices in a directed acyclic graph (dag). Unfor-tunately, the aforementioned �(nω)-time methods for the1-witness problem do not seem to admit any straightfor-ward generalizations to include the k-witness problem.

We show here the k-witness problem for two n × n Bo-olean matrices to be solvable in expected time O (n2+o(1)k+ nωk(3−ω−α)/(1−α)), almost certainly (see Fact 2 for thedefinition of α). Our randomized algorithm combines theideas of Alon and Naor for the 1-witness problem withthe recent results on the so-called bounded k-recognitionproblem due to Aumann et al. [2]. We also show that thek-witness problem can be deterministically solved in timeO (n2.42524k0.6815) for k < n0.412 and time O (n2.5k0.5) fork � n0.412 by an algorithm based on rectangular matrixmultiplication.

Our results subsume the currently best known time up-per bound of O (nωk) for the k-witness problem due to Au-mann et al. [2]. They yield the same asymptotic time upperbounds for the all-pairs k-shortest paths problem and theall-pairs k-common ancestors problem in directed acyclicgraphs. For the related problem of all-pairs k-lowest com-mon ancestors problem, we derive directly the time upperbounds of O (n2.575) for k = 2,3, and O (n3) for k = O (1),again by using rectangular matrix multiplication.

2. Preliminaries

In this section we provide several basic facts relatedto Boolean matrix multiplication. The following fact thatrelates a basic graph property to the fast matrix multipli-cation is a folklore.

Fact 1. The transitive closure of any directed graph with n ver-tices can be computed in time O (nω).

Furthermore, we shall denote by ω(1, r,1) the exponentof the multiplication of an n × nr matrix by an nr × n ma-trix. Coppersmith [3] and Huang and Pan [9] proved thefollowing fact.

Fact 2. (See [3,9].) Let ω = ω(1,1,1) < 2.376 and let α =sup{0 � r � 1: ω(1, r,1) = 2 + o(1)}. Then, α > 0.294,

ω(1, r,1) � β(r), where β(r) = 2 + o(1) for r ∈ [0,α] andβ(r) = 2 + ω−2

1−α (r − α) + o(1) for r ∈ [α,1].

The following fact is due to Alon and Naor [1].

Fact 3. For all non-zero entries of the Boolean product of twon ×n Boolean matrices representatives of their witnesses can becomputed in total time O (nω).

3. The k-reconstruction problems

The k-reconstruction problem is related to the k-wit-ness problem. We adopt the following definitionsfrom [2].

Definition 1. For a collection C of n subsets of a set {1,2,

. . . ,m}, the k-reconstruction problem is to find for eachS ∈ C min(k,#S) elements of S, where #S stands for thecardinality of S. The bounded k-reconstruction problem is toreconstruct completely each set S ∈ C of size at most k.

Note that the bounded k-reconstruction problem doesnot care about sets of size larger than k.

Definition 2. A set A peels another set S if #(S ∩ A) = 1.

A sequence A1, . . . , Ak of sets is a peeling sequence for aset S if for i = 1, . . . ,k, Ai peels S \ ⋃i−1

j=1 A j . A collectionF of sets is a k-peeler for a set S if it contains a peelingsequence of k sets for S. F is a bounded k-peeler for S if itis a k-peeler for S provided that #S � k.

Moreover, for a collection of sets C = {S1, . . . , Sn}, F isa (bounded) k-peeler for C if it is a (bounded) k-peeler foreach S ∈ C .

Definition 3. For two subsets A and S of {1,2, . . . ,m},ISIZE(S, A) denotes the size of the intersection of S withA whereas ISUM(S, A) denotes the sum of the elements inthe intersection.

The main result of Section 2.1 in [2] can be rephrasedas follows.

Lemma 1. Let S be a subset of {1,2, . . . ,m}, and let F be a(bounded) k-peeler for S. Suppose that the values of ISIZE(S, A)

and ISUM(S, A) are given for all A ∈ F . Let MaxOcc(F ) be themaximum of the occurrences of a number in different sets in F .There is a peeling procedure which finds all elements of S, pro-vided that S has at most k elements, in time O (kMaxOcc(F )).

Furthermore, the authors of [2,10] provide the followingfacts on deterministic and randomized k-peelers.

Fact 4. For any collection C of n subsets of {1,2, . . . ,m} thereis an explicit construction of a k-peeler F satisfying #F =O (k logO (1) m) and MaxOcc(F ) = logO (1) m.

Fact 5. Let C be a collection of n subsets of {1,2, . . . ,m}. Withprobability not less than 1 − 1

2n (over uniform distribution ofthe elements of {1,2, . . . ,m}), there is a bounded k-peeler Ffor C satisfying #F = O (k + log k log n) and MaxOcc(F ) =O (log(nm)). In particular, F consists of O (2 j + log(nk)) setsof expected size m/2 j for j = 0, . . . , �log k�. It can be computedin expected time O (m log k).

By combining Lemma 1 with Facts 4, 5, we obtain thefollowing lemma.

Lemma 2. Let C be a collection of n subsets of {1,2, . . . ,m}. Iffor a bounded k-peeler F for C (Fact 4) the values of ISIZE(S, A)

and ISUM(S, A) for all pairs S ∈ C and A ∈ F can be computedin time I, then we can solve the bounded k-recognition problemfor C in total time I + O (nk logO (1) m).

Similarly, if for a bounded k-peeler F for C (Fact 5) the valuesof ISIZE(S, A) and ISUM(S, A), for all pairs S ∈ C and A ∈ F ,

244 L. Gasieniec et al. / Information Processing Letters 109 (2009) 242–247

can be computed in time I, then with probability not less thanhalf we can solve the bounded k-recognition problem in totaltime I + O (nk log(nm)).

4. The k-witness problem

For two n × n Boolean matrices M1 and M2, let W bethe collection of the sets of witnesses for the entries of theBoolean product M3 = M1 × M2. More formally, for 1 �i, j � n, let W i, j denote the set of all witnesses for theentry M3[i, j]. Then, we have W = {W i, j | 1 � i, j � n}.

The bounded k-witness problem for M3 can be seen asa variant of the bounded k-reconstruction problem for Win which we want to recover all witnesses for entries inM3 that have at most k witnesses. Note that m = n in thiscase and the number of sets to reconstruct (denoted by nin the previous section) is n2. In view of Lemma 2, thefollowing lemma is of key importance to our main result.

Lemma 3. For the bounded k-peeler F described in Fact 5the values of ISIZE(S, A) and ISUM(S, A), for all pairs S ∈ Wand A ∈ F , can be computed in expected time O (n2+o(1)k +nωk(3−ω−α)/(1−α) + nω logn).

Proof. Note that for a given A ∈ F , we can compute thevalues of ISIZE(S, A) for all S ∈ W by removing from M1the columns whose numbers do not appear in A as wellas removing from M2 the rows whose numbers do notappear in A and arithmetically multiplying the resulting0–1 matrices. Analogously, we can compute the values ofISUM(S, A) for all S ∈ W by replacing all 1 entries in thepruned M2 with their row numbers in the original M2.

Since the matrices are of sizes n × #A and #A × n, respec-tively, their multiplication takes ω(1, logn #A,1) time.

Let x j be defined by n/2 j = n1−x j (note that nx j = 2 j ).Then, by the structure of F specified in Facts 5 and 2, theexpected time of computing the values of ISIZE(S, A) andISUM(S, A) for all pairs S ∈ W and A ∈ F is as follows.

Case n1−α � k, i.e., 1 − α � logn k:

O

( �log k�∑j=0

nω(1,1−x j ,1)(nx j + log(n2k)

)) = O (n2+o(1)k).

Case log(n2k) < k < n1−α, i.e., O (log logn

logn ) < logn k < 1 − α:

O

( �log k�∑j=0

nω(1,1−x j ,1)(nx j + log(n2k)

))

= O

( �logk�∑j=0

n2+(ω−2)(1−x j−α)/(1−α)+x j

)

= O

( �logk�∑j=0

n2+(ω−2)+(1−(ω−2)/(1−α))x j

)

= O

( �logk�∑j=0

nω+x j(3−ω−α)/(1−α)

)= O (nωk(3−ω−α)/(1−α)).

Case k < log(n2k), i.e., logn k � O (log logn

logn ):

O

( �log k�∑j=0

nω(1,1−x j ,1)(nx j + log(n2k)

))

= O

( �logk�∑j=0

n2+(ω−2)(1−x j−α)/(1−α) log n

)

= O

( �logk�∑j=0

nω2 j(2−ω)/(1−α) logn

)= O (nω log n). �By Lemmata 2, 3 and Fact 5, we solve the bounded

witness problem for the product M3 in expected timeO (n2+o(1)k +nωk(3−ω−α)/(1−α) + nωk(2−ω)/(1−α) log n) +O (kn2 log(n2n)). Hence, we obtain the following theorem.

Theorem 1. For each entry M3[i, j] of the Boolean product M3of n × n Boolean matrices M1 and M2 having at most k wit-nesses, we can report all its witnesses in total expected timeO (n2+o(1)k + nωk(3−ω−α)/(1−α) + nω log n).

Applying the idea due to Alon and Naor [1] for com-putation of 1-witnesses we extend the method from The-orem 1 to solve the general k-witness problem for theproduct M3 = M1 × M2. Denote by select(k, M1, M2) a pro-cedure that returns all witnesses for each entry with atmost k witnesses in the product.

Our randomized algorithm, see Fig. 1, runs in a loga-rithmic number of phases. In the lth phase, the procedureselect(4k, M1, Ml

2) is run where Ml2 is a matrix resulting

from converting each 1 in M2 with probability 1 − 1/2l

to 0. Hence, if an entry M3[i, j] has more than 4k wit-nesses then with �(1) probability, there is a phase l inwhich the expected number of witnesses is in the inter-val [(3/2)k,3k] so that it is also with constant probabil-ity in the interval [k,4k]. Hence, at least k of them canbe reported by select(4k, M1, Ml

2). For the remaining en-tries of M3[i, j] all their witnesses are already reported byselect(4k, M1, M2) in phase 0.

The analysis of our algorithm relies on the followinglemma.

Lemma 4. For all integers 1 � i � n and 1 � j � n, Algo-rithm 1 outputs at least min{k,#w(i, j)} witnesses of M3[i, j]with probability �(1). It runs in expected time O (n2+o(1)k +nωk(3−ω−α)/(1−α)).

Algorithm 1.Set M0

2 to M2;for l = 1, . . . , �log n� + 1 do

1. Compute the arithmetic product Dl of M1 and Ml2;

2. Run the procedure select(4k, M1, Ml2).

3. For all 1 � i, j � n where Dl[i, j] � 4k, and l = 0 orDl−1[i, j] > 4k, report the witnesses of the entry with indices iand j of the Boolean product of M1 and Ml

2 producedby select(4k, M1, Ml

2) as the witnesses of M3[i, j];4. Set Ml+1

2 to the matrix resulting from converting each positiveentry of Ml

2 to 0 with probability 12 .

Fig. 1. Randomized algorithm for k-witnesses.

L. Gasieniec et al. / Information Processing Letters 109 (2009) 242–247 245

Proof. For #w(i, j) � 4k, the lemma follows directly fromthe correctness of select(4k, M1, Ml

2). If #w(i, j) > 4k letl′ be the largest l such that the entry with indices iand j of the Boolean product of M1 and Ml

2 has morethan 4k witnesses. Then, with �(1) probability, the num-ber of witnesses of the entry with indices i and j ofthe Boolean product of M1 and Ml′+1

2 is in the interval[k,4k]. Note that already for l = �log n�, the expected num-ber of witnesses for any entry of the Boolean productof M1 with Ml

2 cannot exceed 1 by a standard prob-abilistic argument. Hence, the expected value of l′ + 1does not exceed �log n� + 1 and the lemma again fol-lows from the correctness of select(4k, M1, Ml

2). By Theo-rem 1, select(4k, M1, Ml

2) can be implemented in expectedtime O (n2+o(1)k + nωk(3−ω−α)/(1−α) + nω logn). Conse-quently, Algorithm 1 runs in expected time O (n2+o(1)k +nωk(3−ω−α)/(1−α)). �

By iterating Algorithm 1 a poly-logarithmic number oftimes, we can produce for all entries M3[i, j] at leastmin{k,#w(i, j)} witnesses of M3[i, j] almost certainly, i.e.,with probability not less than 1 − 1/nβ for some β > 1.

Hence, by Lemma 4, we obtain our first main result.

Theorem 2. The k-witness problem for the Boolean product oftwo n × n Boolean matrices can be solved by a randomizedalgorithm in expected time O (n2+o(1)k + nωk(3−ω−α)/(1−α)),

almost certainly.

Algorithm 1 can be derandomized in the same wayas the randomized algorithm of Alon and Naor for the1-witness problem using the so called small probabilityspaces without worsening its asymptotic time complex-ity [1]. However, to obtain a truly deterministic algorithmwe would also need to derandomize select(k, M1, M2), i.e.,to have a deterministic version of Theorem 1.

5. The deterministic algorithm for k-witnesses

Our deterministic algorithm that computes k witnessesfor positive entries of the Boolean matrix product resem-bles the procedure for finding lowest common ancestorsfor all pair of vertices in a directed acyclic graph, based onrectangular matrix multiplication, given in [6].

Consider the product C of two n × n matrices A and B.

For a given 0 < r < 1, we divide A into n1−r rectangularsubmatrices Al , of size n × nr , and B into n1−r rectangularsubmatrices Bl of size nr × n for l = 1, . . . ,n1−r, see Fig. 1.

By computing the matrix products Cl = Al × Bl, whereAl and Bl are interpreted as 0 − 1 integer matrices, we ob-tain the number of witnesses in the interval (l − 1)nr �w < lnr , for l = 1, . . . ,nr . If for a pair i, j, there are k ma-trices Cl satisfying Cl(i, j) > 0 then it is sufficient to find awitness for each of these k entries Cl(i, j). Otherwise, weneed to find the remaining witnesses (if they exist) in theintervals (l − 1)nr � w < lnr where Cl(i, j) > 1. To achievethis we need to examine all the nr positions in such an in-terval. In the worst case, we need to consider up to k/2such intervals. Thus, the worst case time of finding k wit-nesses for a given pair i, j is O (knr).

Let k = nx . Assume first r > α, where α � 0.294. Thetime required to compute the matrix products Cl and todefine all positive entries in all products Cl is

O(n2+(ω−2) r−α

1−α +o(1)n1−r + n3−r) (1)

= O(n3−r(1− ω−2

1−α )−α ω−21−α +o(1) + n3−r) (2)

= O(n3−r(1− ω−2

1−α )−α ω−21−α +o(1)

)(3)

by Fact 2. (Since the term o(1) in the exponent cannot af-fect our main results with explicit numerical exponents,we may ignore it further without loss of generality.) Onthe other hand, the time needed to find k witnesses foreach of the relevant entries of C is O (n2nrk) = O (n2+r+x).

The algorithm obtains the minimum complexity for r =(1 − α ω−2

1−α − x)/(2 − ω−21−α ), which yields the time com-

plexity O (n2.42524k0.6815) for k < n0.412.Consider now the case r � α. The cost of computing the

matrix products Cl becomes O (n2+o(1)n1−r) = O (n3−r+o(1))

by Fact 2. (Again, we may ignore the term o(1) in theexponent further without loss of generality.) If k � n0.412

then the algorithm obtains the minimum complexity forr = 1−x

2 , which yields the time complexity O (n2.5k0.5).Comparing the obtained upper time bounds on the run-

ning time of our deterministic algorithm for the k-witnessproblem with that of nωk from [2], we obtain our secondmain result.

Theorem 3. The k-witness problem for the Boolean productof two n × n matrices can be deterministically solved in timeO (n2.42524k0.6815), for k < n0.412, and time O (n2.5k0.5), fork � n0.412, by the algorithm using rectangular matrix multipli-cation.

By straightforward computations, we obtain the follow-ing corollary.

Corollary 1. The worst-case time complexity of the methodbased on rectangular matrix multiplication is substantiallysmaller comparing to O (nωk)-time procedure from [2], fork > n0.1538.

6. Applications

Aumann et al. in [2] considered a natural extension ofthe basic all-pairs shortest path problem to include theproblem of all-pairs k-shortest paths. The latter problem isto find for each pair of vertices u, v in a graph k short-est (not necessarily) disjoint paths between u and v. Theyhave observed that the all-pairs k-shortest path problemfor a graph on n vertices can be solved with the same timecomplexity as the k-witness problem for Boolean n×n ma-trix multiplication. Hence, we obtain the following corol-lary from Theorems 2 and 3.

Corollary 2. For any graph with n vertices, the all-pairsk-shortest paths problem can be solved by a randomized al-gorithm in expected time O (nωk3−ω), and by a deterministicalgorithm in time O (n2.42524k0.6815), for k < n0.412, and intime O (n2.5k0.5), for k � n0.412 .

246 L. Gasieniec et al. / Information Processing Letters 109 (2009) 242–247

The problems of finding common ancestors (CA), andespecially, lowest common ancestors (LCA) in a directedacyclic graph (dag) have been quite extensively studiedrecently in the literature (see, e.g., [2]). The all-pairs CAproblem in a dag immediately reduces to the 1-witnessproblem for the Boolean product of the adjacency matrixA of the transitive closure of the dag with the transposeof A. Thus, it can be solved in time O (nω) by Facts 1 and 2.Let us term the natural generalization of the all-pairs CAproblem to include reporting k CAs (if possible) for eachpair of vertices of a dag as the all-pairs k-CA problem. Theall-pairs k-CA problem in a dag analogously immediatelyreduces to the k-witness problem for the Boolean productof the aforementioned matrix A with its transpose. Hence,we obtain our third corollary from Theorems 2 and 3.

Corollary 3. For any dag with n vertices, the all-pairs k-CAsproblem can be solved by a randomized algorithm in ex-pected time O (nωk3−ω), and by deterministic algorithm in timeO (n2.42524k0.6815), for k < n0.412, and in time O (n2.5k0.5), fork � n0.412 .

Similarly, one can consider a natural generalization ofthe all-pairs LCA to include reporting k (if possible) LCAsfor each pair of vertices of a dag and term it as the all-pairsk-LCAs problem. Unfortunately, Corollary 3 does not seemhelpful in efficiently solving the latter problem. We haveto adhere to a more direct method, similar to that for k-witnesses via rectangular matrix multiplication, to obtainan efficient solution in the next section.

7. Finding k-LCAs

To find a single LCA for any pair of vertices in a dagin constant time an O (n2.575)-time preprocessing is knownto be sufficient [6]. We can also determine an LCA for allpairs of vertices that have a unique LCA in time O (nω) [7].In this section we shall state the k-LCAs problem, i.e., theproblem of finding k LCAs (if there are that many available)for any pair of vertices in a dag, under the assumption thatk is a constant.

First, we consider an algorithm for k = 2. The idea ofthe algorithm is as follows. Initially, sort topologically thevertices of the input dag and enumerate them accordingto their rank in the resulting order. Next, compute the ma-trix A of the transitive closure of the dag in time O (nω)

by Fact 1. Further, compute the Boolean product of A withits transpose by decomposing A and its transpose intorectangular matrices analogously as in the algorithm fork-witnesses in Section 5. Namely, we set Al(i, j) = A(i, j),where 1 � i � n, 1 � l � n1−r , (l − 1)nr + 1 � j � lnr, for agiven 0 < r < 1.

Note that, when k = 2 we are primarily interested inthe number of common ancestors in the multiplied sub-matrices of A and its transpose. Let Cl denote the integermatrix resulting from the arithmetic multiplication of thelth submatrices Al and AT

l interpreted as 0–1 integer ma-trices (i.e., Cl = Al × AT

l for 1 � l � n1−r ). Observe thatcomputing Cl takes time O (nω(1,r,1)) and that Cl providesthe information on the number of LCAs in the interval[(l − 1)nr + 1, . . . , lnr]. Further, for each pair i, j, we find

Algorithm 21. for i ← 1 to n do2. for j ← i to n do3. begin4. KLCA(i, j) ← ∅; s ← i;5. while #KLCA(i, j) < k and s > 0 do6. begin7. if i and j and none element in KLCA(i, j) are descendants of s8. then KLCA(i, j) ← KLCA(i, j) ∪ {s};9. s ← s − 1;

10. end11. end

Fig. 2. The k-LCAs algorithm.

an LCA, say t , with the largest number. The total timetaken by the multiplication of the submatrices and findingLCAs is O (n1−r+ω(1,r,1) + n3−r + n2+r), see [6]. In the nextstep, we compare Cl(i, j) with Cl(t, t), for l equal to n1−r ,n1−r − 1, . . . ,1. Note that Cl(i, j) � Cl(t, t) always holdssince t is a common ancestor of i and j. If Cl(i, j) = Cl(t, t)then we compare Cm(i, j) with Cm(t, t), for the largestm < l such that Cm(i, j) > 0. Note that Cl(i, j) > Cl(t, t) orCm(i, j) > Cm(t, t) implies that in the considered vertex in-terval there is an LCA different from t. (Otherwise, t is theunique LCA of i, j.) Then, we find a largest vertex which isan LCA of the pair i, j that is not an ancestor of t in thisinterval. It takes time O (n3−r + n2+r) in total.

Similarly, one can look for triplets of LCA. After deter-mining the second LCA, say s, it is sufficient to examinewhether Cl(i, j) > Cl(t, t) + Cl(s, s) − Cl(t, s), for l equalto n1−r,n1−r − 1, . . . ,1. And if this happens there existsa third different LCA for the pair i, j in the correspondingvertex interval. What remains is to determine the largestvertex which is a common ancestor of i and j and it is notan ancestor of t and s. This takes time O (n3−r + n2+r).

Theorem 4. The problem of finding two or three LCAs (if thereare that many) for each pair of vertices in an n-vertex dag oncan be solved in time O (n2.575).

Proof. The total time taken by the algorithm isO (n1−r+ω(1,r,1) + n3−r + n2+r) which is minimized forr = 0.575, see [6]. �

Unfortunately, computing the number of all ancestorsof triplets of vertices is non-trivial, since we would have torely on the cardinality of the set of common ancestors ofthese vertices. A direct use of matrix multiplication leadsto a high polynomial (n6) time complexity. Therefore, it issimpler and more efficient to use an algorithm based onthe transitive closure mechanism, see Fig. 2.

On the conclusion of the execution of Algorithm 2, thesets KLCA(i, j) contain k largest ancestors of vertices i andj if there are that many, and their maximal number other-wise. Due to the composition effect of three nested loopsand the cost of the condition evaluation in time O (k) inline 7, the time complexity of the algorithm is O (kn3).

Theorem 5. For any constant k, the problem of finding k LCAsfor each pair of vertices of an n-vertex dag G is solvable in timeO (n3).

For non-constant k, the algorithm finding for each pairall its LCAs is more efficient, see [5].

L. Gasieniec et al. / Information Processing Letters 109 (2009) 242–247 247

8. Final remarks

The k-witness problem for the Boolean product of twon × n matrices trivially requires �(n2k + nω) time. Thislower bound is matched in case of the 1-witness problemby the algorithm of Alon and Naor [1] as well as in case ofthe n-witness problem by the straightforward cubic-timealgorithm for Boolean matrix multiplication. Our random-ized algorithm for the k-witness problem also matches thelower bound (up to no(1) factor) at the two extremes. How-ever the problem whether our result is also optimal for allintermediate values of k remains open.

References

[1] N. Alon, M. Naor, Derandomization, witnesses for Boolean matrixmultiplication and construction of perfect hash functions, Algorith-mica 16 (1996) 434–449.

[2] Y. Aumann, M. Lewenstein, N. Lewenstein, D. Tsur, Finding witnessesby peeling, in: Proc. CPM 2007, in: LNCS, vol. 4580, 2007, pp. 28–39.

[3] D. Coppersmith, Rectangular matrix multiplication revisited, Journalof Symbolic Computation 13 (1997) 42–49.

[4] D. Coppersmith, S. Winograd, Matrix multiplication via arithmeticprogressions, J. of Symbolic Computation 9 (1990) 251–280.

[5] S. Eckhardt, A. Mühling, J. Nowak, Fast lowest common ancestor com-putations in dags, in: Proc. of ESA 2007, 2007, pp. 705–716.

[6] A. Czumaj, M. Kowaluk, A. Lingas, Faster algorithms for finding low-est common ancestors in directed acyclic graphs, in: The SpecialICALP 2005 Issue, Theoretical Computer Science 380 (1–2) (2007)37–46.

[7] M. Kowaluk, A. Lingas, Unique lowest common ancestors in dags arealmost as easy as matrix multiplication, in: Proc. of ESA 2007, 2007,pp. 265–274.

[8] Z. Galil, O. Margalit, Witnesses for Boolean matrix multiplication andshortest paths, Journal of Complexity (1993) 417–426.

[9] X. Huang, V.Y. Pan, Fast rectangular matrix multiplications and appli-cations, Journal of Complexity 14 (1998) 257–299.

[10] P. Indyk, Explicit constructions of selectors and related combinatorialstructures, with applications, in: Proc. 13th Symposium on DiscreteAlgorithms (SODA), 2002, pp. 697–704.