Volume 15, Number 4
INFORMATION PROCESSING LETTERS
31 October 1982
FAST COMPOSITION OF SPARSE MAPS Micha SHARIR * School of Mathematical Sciences and Courant Institute of Mathematical Sciences, Tel Aviv University, Ranrtii Aviv, Tel Aviv 69978, Israel Received 16 ldovember 198 1; revised version received 2 1 May 1982 A fast algorithm for composition of sparse maps (i.e., maps which act as the identity on most of the elements of their domain) is presented.
Keyworris: Analysis of algorithms, data structures
In this paper we consider the following problem: Given a sequence of sparse maps f, 9.. . , f,, all defined on a finite set A of size n, and all mapping A to A, compute their composition
function. Generalizations of the technique outlined in this paper can be used to enhance the efficiency of motion-planning algorithms in robotics studied by the author and by Schwartz
+=f,of,_,o
VI-
l
** of,
efficiently. A map f acting on A is called sparse if the cardinality of the set
We assume that each of the maps fi is represented by its restriction to the set D,,, i.e., as the set of pairs
D, = {x E A: f(x) * x} is small relative to the size of A. A trivial method of computing 6, which applies to sparse as well as nonsparse maps, is simply to compute +(x) for each x E A straight from the definition of +. This trivial method requires O(mn) steps. In this note we will present a more efficient method, which requires only O(n + Cp_, ]Dri1)steps. The problem attacked in this paper somewhat resembles the problem of composition of sparse relations discussed by Hunt, Szymanski and Ullman [ 1, 21, although the relations that they handle must be really sparse (i.e., the total size of such a relation must be ismall), whereas the functions that we handle here are sparse only in terms of the number of nonidentity relationships in each such * Work on this papl:r has been supported in part by ONR Grant NO00147%rZ-0571 at the Courant Institute, and by the Bat-Sheva Fund at Tel Aviv University.
that there is no need to construct the sets D,, (a task that would itself require O(mn) steps). We also assume that the elements of A are represented as small integers, or as cell pointers, so that access to various maps and attributes associated with each such element is fast and can be performed in a fixed number of steps, independent of the size of A. We will present our algorithm in a somewhat crude and abstract form. Additional taining to its efficient implemealtation can be easily worked out by the reader. The algorithm maintains a forest K and associates a value v(n) E A with each of the nodes n of K. We will use the notation n, to denote a node in K whose value is x E A; note, however, that a given value x E A can appear as the value v(n X) attached to more than one node n, E K, so that n X so
OOZO-0190/82/(~000-0000/$02.75 0 1982 North-Holland
!83
Volume 15, Number 4
is tm,t uniquely defined by x. Each root node in K represents an element in the range of the composition of the maps processed so far, and all such range elements are represented by roots. Initially each x E A isrepresented by a root x* of a singleton tree, and these singleton trees collectively constitute the forest K. We shall call x* the initial node of x. As the algorithm proceeds, new nodes are added to K. At each moment each initial node X* will belong to some tree of K, and the value v(r) associated with the root node r of this tree will be the image of x under the composition F of all maps processed so far. The algorithm composes the maps one at a time, starting with f ,. The algorithm processes fj in the following way: Let f[l denote the map fj restricted to its noniGenfity domain D = D,,, and let R denote the set of all roots in the current forest K. For each pair [x, y] E fi do the following: Step (i). If x is not the value v(n,) associated with any node n, E R, do nothing (since x is not in the range of F, and hence the relationship fj(x) = y will not affect the value of the composed map)* Step (ii). Otherwise, let n XE R be, the unique root whose associated value v(n,) is x. If y B D (i.e., fj(y) = y) and y is the value v(n,) associated with some unique root nY E R, or if a new root n,, has already been created for y during the processing of fj by Step (iii j below, make n s the son of n,,. (Note that in this case n,, will still be a root when we have finished processing fj.) Step (iii). Otherwise, fj(y) * y or y is not the value of any v G R. In these cases we create a new root n,,, put v(n,) = y and make n, the son of nY. (Note that it is possible for fj to map several distinct elements x E A into the same y. In such cases, we will create the new root n,, only once, and link all the roots representing such elements x to nY.) Once we have processed all the given maps in this manner, we will be left with a forest K whose size is at most n + Cp_ IDF,.Then the final phase of the algorithm traces paths in K from leaves to roots, traversing each arc of K only once, and computing a map ‘final_ range’ which maps each node u E K to the value associated with the root of the tree containing u. The composed map we 184
31 October1982
INFORMATION PROCESSING LE’M’ERS
desire is then simply ( [x, final, range (initial_ node(x))] : x E A). Re.mds. (1) It may happen that, during the processing of some map 4, some y E A will have temporarily two roots n,, and nk associated with it. such that nY is the ‘old root’ whose value is y just prior to the processing of fi, whereas n\ is a new root added by Step (iii) of the algorithm as a pair [x, y] E fj is processed. When this happens, the root to be associated with y during further processing of q is nb, except for the processing of the pair [y, fj(y)] (whrch must appear in q), in which nY is to be used. (2) Various auxiliary fields and attribute bits need be temporarily attached to elements x of A during the processing of each of the maps f{, such as the old root of x, an indication whether x is in the domain of f& etc. Special care has to be taken in manipulating these fields, so as not to use more than O(]Df I) time. We leave it to the reader to work out these finer details. Example. Let A = (1,. . . ,8), and let the sequence of maps to be composed be If,, f,, f,, f,], where f, = {[
1,219 [WI),
f, = ([ 1.419[5,6]), f, = {[4S], [6,7], [7,5]), f4 = ([2,3l, [3,5], [5,31). (These values represent the maps restricted to their nonidentity domains.) The structure of the forest K after each composition step is shown below. (Each node there is marked with its value; initial nodes are starred, and each newly added arc is labeled by the algorithm step which has created it.) Initially:
After processing f , : 20
3-9 (iii)
(ii)
i
1*
I
2*
;* ;* ;* ;* ;*
After
prtxessing I, (note that ( I ,4]
is
di
by Step (i):
(ii)
After p
t (iii)
2Q 3*0
5Q (iii)
(ii)
by the algorithm can constant time.
6* +
I
A
1” 2* After proceming
The author wishes to thank Jacob T. Schwartz for his help in preparing this manuscript. f4:
[I] H.B. Hunt 111,T.G. Szymanskiand J.D. Ullman. Operations cm sparserelations,Comm. ACM 20 ( 1977) 171- i76. 121T.G. Szymanski and J.D. Pullman, Evaluating relational arguments, SIAN J. with dense and Cornput. 7 (w.77) t09- t22. Sbafif, On the piano movers probkm ri#d pdygortd bfxiy moving amid pdygonal hdem, Comm, Pure Appl. Math,, to appear.
To establish the correctness of our composition algarithm if suificies to grove, inductively, that at