sorting of linear combinations of numbers - CiteSeerX

3 downloads 0 Views 180KB Size Report
Abstract. We address here the problem of sorting a set of linear combinations of three arrays of num- bers. Though it has many practical signi - cance, the ...
SORTING OF LINEAR COMBINATIONS OF NUMBERS Arun K Pujari and Alluri Sudhakar

Arti cial Intelligence Lab., University of Hyderabad Hyderabad, 500 046, INDIA Email: [email protected]

Abstract

We address here the problem of sorting a set of linear combinations of three arrays of numbers. Though it has many practical signi cance, the problem was not addressed earlier. We propose an ecient algorithm for this problem. The main contribution of the work is that we identi ed ranges of the parameters of linear combinations for which the sorting order is constant. These ranges for all values of theparameters are compiled in a pre-sorting stage requiring O(n4 logn) and the actual sorting is accomplished in a tree search of O(logn) time. The algorithm is very efective when the sorting is required for very large number of parameters.

1 INTRODUCTION

Sorting of a set of numbers has been one of the fundamental problems of computer science research. This classical problem has been studied extensively and several algorithms are devised for sorting problem as well as for several related problems. The major motivation for such extensive study is that sorting nds its applications in almost every branch of computer science. One sorting problem that has escaped the attention of researchers is the sorting of the linear combinations of a given set of numbers. The problem can be stated as follows: Let X i = fxi1 ; xi2 ; : : : ; xin g; 1  i  m; be m arrays of n numbers. For a set of parameters t1 ; t2 ; : : : ; tm ; de ne W (t1 ; t2 ; : : : ; tm ) = mi=1 ti Xi : The problem is to sort W 's when ti 's are varying for a given set of X i 's. Perhaps the trivial way would be to compute the each case of linear combinations separately and to sort the resulting number using the usual sorting algorithms. We are prompted to ask whether there can be a more ecient algorithm to sort these distinct W 's when these are the linear combinations of a xed set of Xi 's. It may be noted here that this problem is relevant only when there is a need to sort frequently several linear combinations of the same set of arrays. Consider an instance of this problem in computer graphics. Let a scene be described of a set of n threedimensional points, fPi(xi ; yi ; zi ); 1  i  ng and the

scene is to be displayed along a set of viewing directions rapidly. Any viewing algorithm of 3D objects necesarily uses (a) the projection of the coordinates on to the viewing plane and (b) some sort of sorting of the projected coordinates. The projection can be obtained by a linear combination and thus we require to sort the projected coordinates of these points rapidly for several linear combinations of the given set of points. We call this problem as the 3D instance of the sorting problem as m here is 3. The problem was rst addresses by Pujari [2] for 2D and an ecient algorithm is proposed. In the present paper we investigate the 3D case of the sorting problem. It may be noted that the extension of the results of 2D to the cases of 3D is not trivial. Though intuitively, the 3D problem can be visualised as a series of 2D problems, the series is not nite. The main contribution of the present work is to provide an insight to the problem. We observe that the all linear combinations can be identi ed by two parameters t1 and t2 . (In case of 2D there can be just one parameter.) We identify the ranges of these two parameters for which the sorted order remains constant. This requires rst nding the ranges of t2 using the earlier algorithm and for each valuese of t2 we determine subranges of t1 . The second part of the method is depended on the higher derivatives of slopes. Thus, the algorithm requires O(n4 logn) time. We, however, believe that the proposed algorithm can be re ned to get a lower complexity.

2 THE 2D INSTANCE

In this section, we discuss the intuitive ideas of the problem. We rst study the 2D instance here. Let the arrays X and Y be given and we are to sort W = X + tY for all t. Without loss of generality, we can assume that all elements of X and Y are strictly positive and distinct. The problem can be clearly explained with the help of the following diagram (Figure 1). In this illustration, n = 5 and there are ve points Pi ; 1  i  5 and fXi g ( fYi g, respectively) constitute the x-coordinates (y- coordinates) of these points. When these points are projected on to the line CD, the distances of these projected points from C can be expressed as Wi = Xi + tYi for some t. When the value of t varies from ?1 to 1 the line rotates through an angle of 360 degree. How-

ever, the relative order of the projection does not change so continuosly as the line makes the complete rotation. It can be seen that when the line goes from OA to OB, the sorted order of the projections of the points remains constant, namely (P1 ; P3 ; P2 ; P4 ; P5 ). The sorted order before OA (assuming that the line is moving clockwise) the order is (P1 ; P2 ; P3 ; P4 ; P5 ) and after OB the order is (P1 ; P3 ; P4 ; P2 ; P5 ). Thus the main idea is to nd the ranges of the values of t for which the sorted order remains constant. If these ranges are computed a priori, the sorting can be accomplished by simply a table lookup which determines the value of t and its associated permutation. One can also notice that whenever the sorted order changes it is only a interchange of the order of two consecutive points (i.e., a tansposition). The rotation can be parametrized into four distinct cases each corresponding to a rotation through 90 degree. These 4 cases are Case 1: W = X + tY; t  0 Case 2: W = ?tX + Y; t  0 Case 3: W = tX ? Y; t  0 Case 4: W = ?X ? tY; t  0 Y

P1

P3 P5

In view of the above characteristics, if these subintervals (equivalently, j 's) and the associated permutations j 's are constructed in advance then sorting of W , for any value of t, involves only searching of the subinterval in which the given t lies. A binary search for locating the interval containing t can be accomplished in O(log ki ), which is (O(log(n)) ). If we consider X , Y as the xcoordinates and y-coordinates of n planar points then Z is the transformed coordinate of these points as viewed from an angle , 0    . Thus identifying di erent subintervals for t in terms of [i ; i+1 ] is equivlent to de ning di erent ranges for  in [0; ] (in Figure 1, the angle AOB). It can be seen that these subintervals in  also have the above properties for both the cases taken together. In [] the algorithm to compute j 's is presented. In the following section, the earlier results are reviewed and then we present an improved algorithm in the next section.

2.1 The Algorithm

P4 C

1. For every j , there exists a permutation j that sorts W 8 t 2 (j ; j+1 ): 2. For t = j both j?1 and j sort Z . 3. The permutations j?1 and j can be obtained from each other by exchange of a pair of adjacent elements. 4. If k1 and k2 are the numbers of subintervals for case (i) and case (ii) then, k1 + k2 = n(n ? 1)=2

P2

O

X A

B

Figure 1: For a permutation  = a1 a2 : : : an , de ne rev() as the permutation an an?1 : : : a1 denoting the reverse ordering with respect to . Clearly, if an array is sorted in increasing order by , then rev() sorts it in decreasing order and it sorts ?X in increasing order. Thus we have essentially two distinct cases of linear combinations, namely, case (i) W = X + tY ; t  0 case (ii) W = ?tX + Y ; t  0: For any of these two cases it is shown here that the range of t, [0; 1) can be subdivided into subintervals [0; 1 ]; [1 ; 2 ]; : : : ; [k?1 ; 1) having the following properties.

For a permutation , let i represent the permutation obtained by interchanging the ith consecutive symbols of . That is, if  = a 1 a2 : : : a n i = a1 a2 : : : ai?1 ai+1 ai ai+2 : : : an : Let x be the permutation that sorts X in increasing order, i.e., Xx (i)  Xx (i+1) for all i.

ALGORITHM 1 : STEP 1 0 = x; j = 0; 0 = 0 STEP 2 : begindo for j ,

STEP 2a : begindo for each i = 1; 2; : : : ; n ? 1 STEP 2b X i = Xj (i+1) ?Xj (i) Yi = Yj (i) ?Yj (i+1) It is to be noted that X , Y are also dependent on the index j , but it is not used in the de nition for convenience.

enddo STEP 3 Find

m such n othat X m = Min X i : X i > 0 and Y i > 0 Y m Y i

2.2 Improvement of Algorithm 1

STEP 4 Set j+1 = XY mm ; if m is found in STEP 3 ; else stop. STEP 5 Set j+1 = jm . STEP 6 Update j ? j + 1

It is clear from the foregoing discussion that ALGORITHM 1 requires exactly n(n ? 1)=2 steps for both the cases taken together. In each step, the computation is dominated by the determination of the minimum value of X and by the insertion of two additional similar values. enddo Y If we de ne, following the notations in [1], an active line (* if more than one m found, choose the rst one ar- segment as the line segment joining Pi (X(i) ; Y(i) ) and bitrarily) P (X(i+1) ; Y(i+1) ) for a given permutation , then The second parametric case can be handled by the 'si+1in Step 4 is essentially the minimum positive slope of modifying the ALGORITHM 1 suitably. The modi ed these active line segments. Let us de ne Pi Pj ; for i < j , algorithm takes the initial permutation as y and termi- as a valid line segment. There are n(n ? 1)=2 valid line nates at rev(y ) interchanging of roles of X and Y and segments for a given  and it can be seen that at each change of sign will suce to compute . of n(n ? 1)=2 iterations, the algorithm uses the slopeThe above algorithm iteratively computes  values for information of one (without repetition) of these valid line case(i). Equivalently, if one chooses to compute  values segments with respect to  . Theorem 2 and Proposition between the range (0 ; 180) then both the cases can be 1 guarantee that the active0 line segment with minimum handled simultaneously. The following theorems show (or maximum ) slope at any stage is also minimum (or that these values as computed above are in increasing maximum) slope among the set of valid line segments. order. The proofs of the theorems are omitted here. Thus we sort the slope information of valid line segments respect to 0 and use these 's (or, equivalently 's) Theorem 1 The j 's as computed in STEP 4 of the with sequentially instead of computing these values in Step 2above algorithm satisfy Step 4. Thus, contrary to the proposal in [1], it is not necessary to maintain a S-tree and carry out insertion i  j ; for i > j: and deletion at every iteration. The initial information Lemma 1 j and j+1 arrange the array Z = X + about 0 and the sorted list of valid line segments at Step j+1 Y in the same order. 1 would suce to run the algorithm in O(n2 ). Thus, the ?  initial sorting of slopes of valid segments seems to be Lemma 2 If 8i with Yj (i) ? Yj (i+1) > 0; and l the dominating factor i.e., O(n2 log n) in the implemenis the index such that, tation. Thus the parameters that are to be computed     during the iterations of Algorithm 1 can be obtained iniXj l ? X j l Xj i ? Xj i j = = Min Y ; tially and the algorithm need not be sequential. Once Yj l ? Yj l j i ? Yj i the j 's and the associated permutation j 's are computed, actual sorting of W is accomplished by searching then " the ordered list of j 's which de ne the interval that con  # Xj i + j Yj i ? Xj i + j Yj i tains t. This requires O(log k) for a binary search with k Min Yj i ? Yj i values of . Since k  (n)(2n?1) in O(log n) time sorting of any Z can be carried out. We give below the complete "   # algorithm which takes care of the presorting step as well Xj l + j Yj l ? Xj l + j Yj l = ^ as the actual sorting for each t. = Yj l ? Yj l ALGORITHM 2: To sort X + tY for any value of t ^ and 1 = j+2 ? j+1 : STEP 1 Start with L(0) and the sorted sequence as 0 and 0 = 0 Theorem 2 j sorts Z = X + tY; for j  t  j+1 STEP 2a: begindo for each i = 1; 2; : : :; n ? 1, Theorem 3 ALGORITHM 1 terminates after attainSTEP 2b: begindo for each k > i, ing y , the permutation that sorts Y in increasing order. X ik = X (i+k) ? X (i) proof The algorithm terminates at a permutation  Yik = Y (i) ? Y (i+k) 1. either when there is no i such that Y(i) ? Y(i+1) > 0;. enddo enddo 2. or, when there is no i , such that X(i+1) ?X(i) > 0. STEP 2c: Compute ik as When the rst condition is achieved y is attained and   the second condition implies rev(x ). It is easy to see X ik  = ik that the second condition cannot be achieved earlier than Y ik the rst one. Thus ALGORITHM 1 generates j and j such that where ik is de ned for values of i; k such that Z = X + tY is sorted by j for j  t  j+1 . X ik  0 and Y ik > 0) +1

+2

+1 ( +1)

+1 ( )

+1 ( )

+1 ( +1)

+1 ( +1)

+1

+1 ( +1)

+1

+1 ( )

+1 ( +1)

+1 ( )

+1 ( +1)

+1

+1 ( +1)

+1 ( )

+1 ( )

+1 ( +1)

+1

+1 ( )

+1 ( )

+1 ( +1)

+1 ( )

+1 ( +1)

+1

+1 ( )

1

0

0

0

0

STEP 2d: Sort them in the increasing order. Let

1 ; 2 ; : : : ; r be the ordered sequence of ik 's in increasing order. For every  there is a pair of indices i and k such that   X ik : =  Y ik

We shall denote by ik the transposition which interchanges the position of i and k in a given arrangement. STEP 3 For the given value of t, nd j such that j < t  j+1 . STEP 4 The sorted sequence of X + tY is obtained by computing the permutations,

enddo

ij kj ij? kj? : : : i k 0 : 1

1

3 THE 3D INSTANCE

1 1

The observation made above- the presorting need not be iterative- becomes a very powerfull tool in extending the concept to 3-dimension. In this section, we extend the algorithm for the case m = 3. As m takes on higher values the problem ceases to have any practical importance. For m = 3, the three arrays are X; Y and Z and we assume that the elements of X; Y and Z are positive and distinct. We can parametrize the linear combinations as follows. Case 1 W = X + t1Y + t2Z Case 2 W = X + t1Y ? t2Z Case 3 W = ?t1X + Y + t2Z Case 3 W = ?t1X + Y ? t2Z Case 4 W = ?X ? t1Y ? t2Z Case 5 W = ?X ? t1Y + t2Z Case 6 W = t1 X ? Y ? t2Z Case 7 W = t1 X ? Y + t2Z These 8 cases can be reduced to four distinct cases. Case 1 W = X + t1Y + t2Z Case 2 W = X + t1Y ? t2Z Case 3 W = ?t1X + Y + t2Z Case 4 W = ?t1X + Y ? t2Z The intuitive idea of solving the problem is to make use of the above principle to nd the rectangular ranges of two parameters t1 and t2 . We rst carry out the presorting step for X and Z and determine di erent ranges of t2 as [0; 1 ]; [1 ; 2 ]; : : : ; [k?1 ; 1). This step is essentially sort the j values as described in Algorithm 2. For a xed value of t2 , the problem can be visualized as yet another case of 2-dimensional problem. For example, W = X + t1 Y + t2 Z can be written as W = V + t1 Y , where V = X + t2 Z , for any xed t2 . Thus for a given t2 , we can make use of Algorithm 2 to determine the ranges

of t1 . However, it is not possible to compute ranges of t1 for all values of t2 . On the other hand, we try to nd the ranges of t1 so that the sequence of j does not change. Once we determine the ranges of t1 having distinct order of this sequence, we compute the actual magnitude of j for each critical values of t1 . These magnitudes give the ranges of t2 for a given value of t1 . Thus, the ranges of t1 and t2 are no longer intervals but are a sort of parallelograms in 2-dimension. Initialization:  0 = 0; j = 0. STEP 1: Compute the sorted sequence of X = X + j Y as (j )0 STEP 2a: begindo for each i; 1  i < n and for each k appearing after i in the permutation (j )0 ; X ik = Xk ? Xi Y ik = Yi ? Yk Zik = Zi ? Zk enddo STEP 2b: Compute ik as   X ik ik =  Z ik Though ik 's are de ned for values of i; k such that Z ik 6= 0, when Z ik = 0, corresponding  is considred to be a very large positive number if the numerator is positive and a very large negative number, otherwise. STEPj 2c:j Sort them in the increasing order. Let 1 ; 2 ; : : : ; jr be the ordered sequence of ik 's in increasing order. For every j there is a pair of indices i and k such that    ik j = X Y : ik j We shall denote by ik the transposition which

interchanges the position of i and k in a given arrangement. STEP 3: begindo for each0 1  i; i0  n ?0 1 and k appearing after i and k appearing after i in (j )0 STEP 4: " X ik ? Xi0 k0 #  Z ik j +1  = Min Yik YZii00kk00 Zik ? Zi0 k0 enddo STEP 5: Set j ? j +1, Go to step 1 if we nd in Step 3 at least one entry with positive denominator; else Continue.. STEP 6: Given a pair t1 and t2 , nd the value j such that  j  t1 <  j+1 and for this j nd the k auch that t2 lies between lines joining jk ; jk+1 and jk+1 ; jk+1 +1 . STEP 7: The sorting order of X + t1Y + t2Z is obtained by composition of transpositions 's associated with jm ; 1  m  k with 0j .

4 CORRECTNESS OF THE ALGORITHM

From Algorithm 3, it is clear that we partition the values of t1 to n(n ? 1)=2 intervals and for each interval, t2 varies in a rectangular range. Thus, the output of the algorithm is a tree-structure representing the ranges of t1 and ranges of t2 for each such range. We also store the associated permutation  for each interval such that  sorts X + t1 Y + t2 Z . Thus after pre-processing, we have the sets of ij and regions of t1 , t2 values in which ij sorts W . In order to get the sorted order for a particular t1 , t2 , we traverse through this structure to rst locate the range for t1 and for the given range of t1 to locate the range for t2 , and hence to get the  computed during the pre-processing step. The correctness of the algorithm is based on the observation that within the interval [ j ;  j+1 ] the  does not change and for a xed value of t1 in this range, the 's give the required range where the sorting order remains constant. The proofs are omitted here due to space limitation.

4.1 Complexity Analysis

As discussed earlier, for m = 2, Algorithm 1 (or, equivalently) takes O(n2 logn) time for preprocessing and sorting is accomplished by a binary search with time O(logn). Algorithm 3 makes use of the Algorithm 2, for m = 3, n(n-1)/2 times and also sorts all 's. Hence, Algoritm 3 requires O(n4 logn); and the sorting is accomplished by binary search for t1 in O(log n) and for t2 also in O(logn ). The performance of the algorithm is better only when the pre-processing is done once for very large number of values of ts.

4.2 Experiments

The algorithm is implemented in C on an IBM Pentium 300. The set of points are generated randomly. The experiment was carried out for a very large number values of ts. It is observed that in the algorithm performs much better than the worst case analysis reported above.

5 CONCLUSION

We address a new sorting problem and provide an ecient algorithm for the same. To best of our knowledge, there has not been any earlier attempt in this direction though the problem has practical signi cance. We believe that for m=2 the complexity derived above are optimal. There are possibilities of improving the implementation of the algorithm for m=3. Another important contribution of the present work is that the pre-sorting need not be sequential and as a result the whole sorting process can be accomplished in parallel. For higher values of m, the algorithm can be generalised but has little practical signi cance. ACKNOWLEDGEMENT: Sudhakars work was supported by J N Centre for Advanced Scienti c Research.

REFERENCES

1. B.K Bhattacharya : Usefulness Of Angle Sweep Over Line Sweep. In S.Biswas and K.V.Nori (Eds ), Foundations Of Software Technology And Theoretical Computer Science Berlin : Springer-Verlag (1991) 2. A. K. Pujari : Ecient Algorithm For To Sort Linear Combinations Of Arrays. In FSTTCS Proceedings Springer-Verlag (1993 )

Suggest Documents