Вы находитесь на странице: 1из 8

Sorting Strings and Constructing Digital Search Trees in Parallel

(Extended Abstract)

Joseph F. JEiJEi" Kwan Woo Ryu*it Uzi Vishkint

Dept. of Electrical Eng. Dept. of Computer Eng. Dept. of Electrical Eng.


Inst. for Adv. Comp. Studies Kyungpook Natl U., KOREA Inst. for Adv. Comp. Studies
Inst. for Systems Research Inst. for Adv. Comp. Studies University of Maryland
University of Maryland University of Maryland College Park, MD 20742
College Park, MD 20742 College Park, MD 20742 Tel Aviv University, Israel

Abstract string with respect t o lexicographic ordering. Our so-


W e descrzbe two simple oplimal-work parallel al- lution improves upon the previous best known result t o
gorithms for sorting a list L = ( X I ,X z , . . . , X,) solve this problem.

8
o ,m strings over an arbitrary alphabet C, where
i = l lX;l = n. The first algorithm is a deterministic

algorithm that runs in O( ,$:I,") time and the sec-


1 Introduction
The problem of sorting has been studied extensively
ond is a randomized algorithm that runs zn O(logm) in the literature because of its many important appli-
+
lime. Both algorithms use O(m log m n ) operations. cations and because of its intrinsic theoretical signif-
icance. Given n elements drawn from a linearly or-
Compared t o the best known parallel algorithms for dered set, the sequential time complexity for sorting
sorting strings, our algorithms offer th.e followin,g im-
these n elements is 0 n log n ) on the comparison tree
provements: b
model. In addition, t ere are several parallel sortin
algorithms that run in O(log n ) time using O ( nlog n!
1. The total number of operations used b y our algo-
rithms is optimal while all previous parallel algo- operations (e.g. [2, IO]).
rith.ms use a n,on-optimal numher of operdions. In this paper, we consider the problem of sort-
ing a set of strings containing a total of n symbols
2. W e make no assumption about the alphabet while from an arbitrary alphabet C. More formally, given a
the previous algorithms assume that the alphabet list L = ( X I ,X z , . . . , X,) of m strings over an arbi-
is restricted t o { 1 , 2 , . . . , n O ( l ) } . trary alphabet C, where [Xi1 = n, we are sup-
posed to rearrange L into a fexicographically sorted
3. The computation model assumed b y our algo- list of strings. This problem has been considered be-
rithms is the Common C R C W P R A M unlike fore in the case where the input alphabet is restricted
the known algorithms that assume the Arbitrary to { 1 , 2 , . . .,no(1)} ([1, 13, 161). In particular, Aho
CRCW PRAM. et. al. [l] provide a sequential algorithm that runs
in O ( n ) time; Hagerup and Petersson [13] provide a
4 . Our algorithms use O ( mlog m + n ) space, while parallel algorithm that runs in O( , ~ ~ ~ , " ,time) us-
th.e previous parallel algorith,ms use O(n.l+')
space, where E is a positive consiant. ing O ( nlog log n ) operations on the Arbitrary CRCW
PRAM, and JbJb and Ryu [16] provide an improved
W e also present optimal-work parallel algorith,ms to parallel algorithm that runs in O(1og n ) time using the
construct a digital search tree for a gzven set of strings same number of operations on the same model.
and to search for a string in a sorted list of strings. We The problem of sorting strings can be solved in
use our parallel sorting algorithms to solve the problem O(logm) time using O(n1ogm) operations on the
of determining a minimal starting point of a circular Common CRCW PRAM by directsly applying the par-
allel sorting algorithm of [lo]. Each comparison of the
'Partially supported by the National Science Foundation, algorithm in [lo] is now a comparison between two
Grant No. CCR-9103135, and by the National Science Founda- strings which can be achieved in constant time and
tion Engineering Research Center Program NSFD CD 8803012. linear work on the Common CRCW PRAM [12], and
isupported in part by NONDIRECTED RESEARCH each string participates in at most O(1og m) compar-
FUND, Korea Research Foundation, 1992. isons. However, the total work is not optimal.
IPartially supported by NSF grants CCR-8906949 and CCR- We present in this paper two simple optimal-
9111348. work parallel algorithms to solve our sorting prob-

349
0 1994 IEEE
0-8186-5602-6/94
lem. T h e first is a deterministic algorithm that runs parallel algorithm that runs in O( l & ~ ~ ~time
, ) and a
+
in O( IO " m time using O ( m log m n ) operations. randomized parallel algorithm that runs in O(1og n)
To achieve these bounds, we make use of the digital time are presented; both algorithms use O ( n ) opera-
search tree data structure for representing the sorted tions and do not impose any restriction on the input
strings compactly. We show how to merge such data alphabet.
structures within a sort-by-merge paradigm to allow The rest of the paper is organized as follows. We
effective application of pipelining. Our second algo- review in Section 2 the PRAM model and some re-
rithm is a randomized algorithm that runs in O(log m) lated results, and introduce some basic terminology
time using O ( mlog m + n ) operations. This algorithm that will be used in the remainder of this paper. The
is similar to Reischuk's parallel sorting algorithm [20 . deterministic parallel algorithms for sorting and for
Both algorithms run on the Common CRCW PRA ,
and use only O(m lo m + n ) memory locations. Note
IJ constructing digital search trees are presented in Sec-
tions 3 and 4. T h e randomized parallel sorting algo-
that the time compfexities are functions of m only. rithm is presented in Section 5. Finally, an optimal
Our algorithms improve upon the best known previ- parallel algorithm for finding a minimal starting point
ous parallel algorithms in four ways: of a circular string is presented in Section 6.

1. The total number of operations used by our algo- 2 Preliminaries


rithms is optimal.
2. The input alphabet is arbitrary, unlike the as-
6
A PRAM Parallel Random Access Machine) con-
Pi, . . . ,Pp-r, all
sists of p sync ronous processors, PO,
having access to a large shared memory. In a single
sumptions made in [13, 161 where the alphabet is cycle, each processor may read or write from or into
restricted to { 1 , 2 , . . . , n o ( 1 ) } . a shared memory cell, or perform a local operation.
Each processor Pi, 0 5 i 5 p - 1, is uniquely identified
3. The parallel computation model assumed by our by an index i which can be referred to in the program.
algorithms is weaker. T h e model used by the Let T A ( ~denote ) the .time required by a parallel
algorithms of [13, 161 is the Arbitrary CRCW algorithm A to solve a problem of size n and let W A ( ~ )
PRAM. be the total number of operations used. We say that A

4 . Our algorithms use O(m1ogm + n ) space while


those of [13, 161 use O(n'+') space, where 6 is a
I
is work optimal i.f W A n ) is @ ( S e q ( n ) ) where
is the running time o the best sequential algorithm
for solving the same problem.
, Seq(n)

positive constant. The following known results on the PRAM will be


used throughout the paper. Consider a sequence of n
Our randomized parallel algorithm is based on an integers ( a l ,a ~. .,. , an) each of length O(1ogn). The
optimal algorithm for solving the string search prob- prefix sums of this sequence are the n partial sums
lem. This problem is to find a string Y in a sorted list d e f i n e d b y s i = a l + a z + . . . + a ; , l < i < n . Thenwe
of m strings C = (Xi., Xa,.. . . ,X m ) , when 1x11 = . . .= have the following lemma.
IX,I = = 1. A naive binary search takes O ( f . l o g m ) Lemma 2.1 [11] The prefix sums of a sequence of
time. Kosaraju [18] provides an O(1-) time algo- n integers each of length O(1ogn) can be found in
rithm. We develop in this paper an O(1ogm 1) time + o(,,',"P,;,) time using ~ ( n operations
) on the Com-
sequential algorithm for the case where the input list
is properly preprocessed. We also present a very fast mon CRCW PRAM. 0
parallel algorithm to preprocess the input list using a
linear number of operations. A slight modification of the above prefix sums al-
In addition, we provide an algorithm for con- gorithm can be used to solve the following seg-
structing a digital search tree for a sorted list C = mented prefix sums problem. Given a sequence
A = a l , a z , . . . , a n ) of n integers each of length
( X l , X z , .. . , X m ) , where lXil = n. The dig- O(logn\ and a Boolean array B of length n such that
ital search tree can be deterministically constructed
bl = b, = 1; for each il < i2 such that bi, = bi, = 1
in O( ,lz:,mm) time using ~ ( log +
mm n ) operations. and bj = 0 for all il < j < iz, we wish to compute the
Note that the time complexity does not depend on the prefix sums of the subarray (ai,+l, . . . , ai3) of A .
lengths of the input strings.
We show how to use our parallel sorting algorithms Corollary 2.1 The segmented prefix sums of a se-
to find a minimal starting point (m.s.p. in short) quence of n integers each of length O(1ogn) can be
of a circular string with respect to lexicographic or- found in O(,d,"P,;,) time using O ( n ) operations on
dering. This problem is known to admit a sequen- the Common CRCW P R A M . 0
tial linear time algorithm [8, 211. When the input
alphabet is restricted to { 1,2, . . . , n o ( ' ) } , Iliopoulos The following four lemmas state known results for
and Smyth [14] provide a parallel algorithm that runs finding the first 1 in a Boolean array, solving the all
in O(1og n log log n ) time using O ( n log log n ) opera- nearest one bit problem, merging two sorted lists, and
tions on the Arbitrary CRCW PRAM, and JbJb and sorting a list of n elements respectively.
Ryu [16] independently provide a.n improved algo-
rithm that runs in O(1ogn) time using the same num- Lemma 2.2 [12] Given a Boolean array B[1 : n] of
ber of operations on the same model. A deterministic size n , the index i such that B[i] = 1 and B[j] = 0 ,

350
f o r all 1 5 j 5 i - 1, can be found in 0 1) time using
O(n) operations on the Common C R C PRAM. 0 6 X and Y differ). Sorting the list of input strings im-
plies rearranging the strings in a sorted list with re-
spect to the lexicographic ordering.
Lemma 2.3 [5] Given a Boolean array B 1 : n] = One of our algorithms makes use of a slight varia-
I,
( b l , bz, . . . , b,) of size n , f o r all 1 5 i 5 n , t e nearest
one bit of bi t o its lefi can be found in O(cr(n))time
tion of the digital search tree as defined, for exam-
ple, in [15]. Let C = ( X I ,X 2 , . . . , X m )be a sorted list
using O ( n ) operations, where cr denotes the extremely of distinct strings over the alphabet C. Assume that
slowly increasing uinverse-Ackermann” function. 0 no X i is a prefix of some X j , whenever i # j . We
define the digital search tree T associated with C to
be an ordered rooted tree with m leaves such that
Lemma 2.4 [19, 61 Given two sorted lists ofm and
n elements respectively drawn from a linearly ordered 1. Each edge of T is labeled with a symbol from the
set, the two lists can be merged in O(loglog(m n ) ) + alphabet C, and is directed away from the root.
time using O ( n + m ) operations on the C R E W P R A M .
0 2. No two edges emanating from the same node have
the same symbol as their labels.
Lemma 2.5 [lo] Given a list of n elements drawn 3. All the edges from each node appear in sorted
from a linearly ordered set, the list can be sorted order with respect to their labels.
in O(1ogn) time using O(n1ogn) operations on the
E R E W PRAM. 0 4. Each leaf node U is uniquely identified with a
string X i , in the sense that the concatenation of
the labels on the path from the root to U is X i .
We now introduce the basic terminology needed
for describing our operations on strings. Let C be Notice that condition 3 of our definition does not ap-
an alphabet consisting of an arbitrary set of sym- pear in the usual definition of digital search trees and
bols. Assume that C is equipped with a total order is needed here to describe our deterministic sorting al-
4 that can be evaluated in constant time by a single gorithm. Since the reconstruction of the sorted list of
processor for any given pair of symbols from E. A the input strings from the corresponding digital search
string X = ( a , , a2, .. . ,
denoted by 1x1.
h is a finite tuple of sym-
bols from C, where k is t e length of X which is
Let X [ i ] be the i-th symbol of X I
t,ree can be done quite easily, we can define the sort-
ing problem as follows: Given a list of strings over an
arbitrary alphabet, construct the digital search tree
i.e., X [ i ] = a,, where 1 5 i 5 1x1.For any pair of associated with the corresponding sorted list. We use
indices i and j such that 1 5 i , j 5 k , a consecutive this definition in the next section to obtain the opti-
portion of X , X[i : j ] = ( a i , a i + l , . . . , U , ) , defines a mal parallel algorithm when all the input strings are
substring of X . Note that X i : j ] is the empty of the same length 1 and 1 is small ( 1 5 logm). We
string 6 if i > j , while X [ i :, j \ is a nontrivial sub- will then generalize this algorithm to sort strings of
<
string of X if i j . A substring defined by X[l : i], different lengths.
for some i such that 1 5 i 5 k , is called a pre-
3 A Deterministic Sorting Algorithm
fix of X ,whereas a substring of the form X[j : IC
for some j such that 1 5 j 5 t , is called a SUI:
fix of X. If X = ( a l l . . . , a k ) and Y = ( b l , . . . , b l )
In this section, we present a simple opti-
mal algorithm that lexicographically sorts a list
are strings, the concatenation of X and Y is the L = ( X 1 , X 2 , . . .,X,) of m input strings, where
string X Y = ( ~ 1 , .. . , a k , b l , . . . , bl). Let Xi be the lX,l= n. This algorithm runs in O(l$IEm)
string consisting of X concatenated with itself i times,
time using O(m log m + n ) operations. Note that the
for i 2 0. Then X o = 6,and X’ = X X ’ - ’ , for time complexity depends only on m. During the ex-
i > 0. For a string X = ( 0 1 , a 2 , . . . , a k ) , we consider ecution of our algorithm, digital search trees are con-
at+l = u k + 2 = . . . = #, where # is a symbol not oc-
structed for sorting substrings of the same length.
curring in C (“blank symbol”), and extend the total Without loss of generality, we assume that the input
order 4 to the alphabet C U {#} such that # 4 U , strings are pairwise distinct and no string is a prefix
Vu E c. of another string.
Given a list of strings C = ( X I , X?, . . ., Xm), let Before going further, we introduce a straightfor-
ILL= m be the number of striiigs i n C, and let ward algorithm to sort a list of m strings. Note that
= n be the total number of symbols i n the by Lemma 2.2, any two strings of len ths 11 and 12
ithout loss of generality, w e assume that none
of the strings is the empty string 6 and that the input
+
can be compared in O( 1) time using 0 8 1 1 2 ) opera-
tions. Hence, if we apply Cole’s mergesort algorithm
strings are pairwise distinct since we can add a unique (Lemma 2.5) to solve our string sorting problem, each
marker to the end of each string. Hence, for any two of its O(lo m) iterations can be executed in O(1) time
distinct strings X = ( ~ 1 , .. . , a k hand Y = l b l , . . . , bl) using O(nf operations. Thus, we have the following
of C,there is a position j in whic X and Y iffer. The lemma.
total order on C induces a lexicographic order on
the list of strings as follows. When x = ( a l , . . . , a t ) Lemma 3.1 A list of m input strings whose total
and Y = ( 6 1 , . . ., b,) are any two distinct strings, we n.umber of symbols i s n can be sorted in O(1og m time
say that X 4 Y if and only if ai = b i , for 1 5 i < j , using O ( n log m) operations O R the Common ARC W
and aj 4 bj ( j is the most significant position in which PRAM. 0

351
The total number of operations used by the modified Lemma 3.2 Given t w o digital search trees S and T
Cole's mergesort algorithm is nonoptimal in the sense with m, and mt leaves respectively, Algorithm Merg-
that the problem can be solved in O(m log m+n) oper- ing-Two-Trees merges the t w o trees into a single dig-
ations which is strictly less than O(n logm) whenever ital search tree R with m, = m, +mt leaves in
m = o ( n ) and m is not a constant.
The basic strategy of our optimal algorithm fol-
O ( d , ~ ~time ~ using
~ ~ O(mr
T ) + +
( ( n , n t ) - 12,)) op-
lows the mergesort technique. Given t w o digital search erations, where d = min {depth(S), depth(T)}, and
trees with m, and mt leaves respectively, we show how where depth@) is the length of the longest path f r o m
the root i n tree 2 . 0
to merge these two trees into a digital search tree with
+
m, mt leaves. We begin by describing a relatively
We use our tree merging algorithm to derive the fol-
slow algorithm that merges the two digital search trees
by sweeping through these trees from top to bottom lowing mergesort algorithm. We first consider the case
in a breadth-first search manner. where all the m input strings are of the same length
1 = 2, We then generalize our algorithm.
Algorithm Merging-Two-Trees
Algorithm Merge-Sort
Input: Digital search trees S and T with m, and mt
leaves corresponding to two sorted lists of m, and mt Input: m digital search trees. Each tree is a chain of
strings respectively. length 1 = 2 which represents a string of length 1. For
Output: The merged digital search tree R with m, = simplicity, we assume that m = 2 h , for some positive
+
m, mt leaves. integer h.
Output: The digital search tree for the sorted list of
Let the roots be at level 0. Let s be a vertex of S t.he input strings.
and let t be a vertex of T . The vertices s and 1 are
pairwise ambiguous if the string defined by the path begin
from the root of S to the vertex s is equal to the string for j + 1 to logm do
defined by the path from the root of T to the vert.ex for i +- 1 to 9 do in parallel
1 . In this case, the vertices s and 1 will be merged to Merge the two adjacent trees Tzi-1 and T2i
one vertex r in R. each with 2j-l leaves;
begin Let the resulting tree be Ti;
end
The algorithm works in iterations i 2 1.
By Lemma 3.2, iteration j of this algorithm runs
Input to iteration ( i 2 1 ) : All ambiguous pairs in O(llo]poRo~,,,)time, and hence the total time for
( s l , t l ) , .. . , ( s k , t k ) a t level i - 1, where vertices
S I , ,. . , s k belong to S and vertices t l , . . . , i k belong the a.lgorithm is O(ll:$,",). During iteration j ,
to T . The children of each s, in S are i n sorted order the number of operations necessary to, merge chil-
according to the i-th symbols of the strings leading
to them. Similarly, the children of each t i n T are
drens of 2 pairs of trees each with 23-' leaves is
in sorted order according to the i-t.h symbols of the O ( 2 . 2j-l . 2) = O(m). Thus, the total number of
strings leading to them. operations required for merging the children by this
algorithm is O(m1ogm). Notice that the number of
Iteration i: ambiguous pairs during iteration j is the difference be-
for every input ambiguous pair ( s , t ) do in parallel tween the number of vertices in the trees before the it-
Merge the list of children of s with the list of chil- eration and that in the trees after the iteration. Thus,
dren of i, using a parallel merging algorithm. Identify the total number of ambiguous pairs during the execu-
ambiguous pairs at level i . For each input ambiguous tion of the algorithm is bounded by the total number
pair, form a new vertex r in R. of vertices in the input m trees which is O ( n ) , and
hence the total number of operations for manipulat-
Implementation Remark: We store each tree in a ing the ambiguous pairs is O ( n ) . It follows that the
one dimensional array. The children of each vertex are total number of operations is O ( m log m + n). There-
stored in sorted order in consecutive memory locations fore, we have the following lemma.
so that the merging of the childrens of two vertices
can be done in a linear number of operations. Prior to Lemma 3.3 Given a list of m strrngs each of length
advancing from iteration i- 1 to iteration i, we apply a m = I, Algorilhm Merge-Sort finds the sorted list of
2
prefix sums routine so that all the ambiguous pairs of
level i - 1 are given in consecutive memory locations. the strings in 0 ( 1 , ~ $ ~ , ) time using O(mlogm n )+
operations. 0
end
We can make the algorithm faster by pipelining the
Let n, and nt be the numbers of vertices of the input operations of the two loops. Once the first iteration
digital search trees S and T respectively. Similarly, of the inner loop terminates, we can proceed with the
let n, be the number of vertices of the output digitsal next outer iteration while trying to complete the in-
search tree R. Then, clearly n, + n l > n,, and one ner loop of the previous iteration. Thus, we have the
can show the following lemma. following lemma.

352
Lemma 3.4 Given a list of m strings each of length where 1 5 i 5 r, and such that XI,, exists but
m = I , the sorted list of the input strings can be deter-
11 Xl-1,j does not, and X,,, exists, but X,+l,j does
mined in O(([+log m ) lo]gDpo~m)
lime using O ( mlog m+ not. This step can be performed in O(1og m) time
using U ( n ) operations with the doubling tech-
n) operations. D nique, and enables allocation of processors for ex-
ecuting the following steps.
the time complexity to O( Iztgmm)
We can improve the previous algorithm by reducing
regardless of the [Step 3 ] For each 1 < j 5 si and each substring Xi,j
value I as stated in the next theorem. See [17] for the of Xi, 1 5 i <
m, find the maximum inter-
proof. val ( l ' , ~ ' ) ,where I' 5 i <r', and such that
Xp,h = Xlt+l,h = . . . = X,r,h,.fOr each 1 h < j .
We do this in two substeps. First we use an auxil-
<
Theorem 3.1 Given a list C = ( X I ,XZ,. . . , X,) of
iaryarray A a n d s e t A ( i , j ) := O,ifXi,j = X i - l , j
m strings, where lXil = n, the sorted list o f t h e
:= 1, otherwise; we also set the fol-
input strings can be found in O( l ~ $ ~ m )dime vsing values: A ( 1 , l ) := 1. Now for
+
U ( mlog m n) operations. O each i separately, find the smallest k for which
= 1; we conclude that for every j < k,
Remark: One can get a somewhat simpler optimal = A ( i , j - l ) , and we set B ( i , j ) = 0 for
time sequential algorithm. We can just merge the in- k ; we also conclude for every j 2 k,
put m digital search trees of different lengths two by that A ( i , j ) # A ( i , j - l ) , and we set B ( i , j ) = 1
two by using a sequential algorithm similar to Algo- for all j 2 k. This computation can be done in
r i t h p Merging-Two-Trees. This sequential algorithm O(1) time and O ( n ) operations on the Common
mqrges two digital search trees by processing them in CRCW by Lemma 2.2. The second substep fin-
a breadth-first search or a depth-first search manner. ishes the computation of this step. Let the inter-
val (l', T ' ) of Xi,l be (1, m). Determining I' for the
4 Constructing a Digital Search Tree interval (l', r') of Xij, for j 2 2, is done by finding
Given a lexicographically the largest integer k 5 i for which B ( k ,j - 1) = 1.
sorted list L = (Xi, Xz,. . . , X,) of m input strings,
where IXiJ= n, we show how to construct a dig-
ital searc tree for the list within the same complexity
h
The value r' of l',r') can be also determined in
a similar way. T is step can be done in O ( a ( m ) )
time and O(n operations by Lemma 2.3. Note
bounds as those of the sorting algorithm. We have al- that the two dimensional arrays A and B of this
ready presented an optimal-work parallel algorithm to step are used for ease of explanation and are not
construct a digital search tree when each input string really needed.
is of length logm. In this section, we generalize this
result to an optimal-work deterministic parallel algo- [Step 4 ] We construct in parallel a digital search tree
rithm when the input strings are of different lengths. of depth a t most logm for each interval de-
If the input strings are not sorted, we can sort them termined in Step 3 in the following way. Let
by using the sorting algorithm in the previous section (l',.') and ( l " , ~ " ) be such intervals of sub-
without affecting the overall complexity of the algo- strings Xi,, and X;,,-l, respectively. Clearly,
rithm. Without loss of generality, we assume that the I" 5 1' and r' 5 r". Then, the ( j - 1)st
input strings are pairwise distinct and no string is a substrings {XI(;- 1 , XIt+l,j- I., . . . , Xrt,i-l} form
prefix of another string. The following simple algo- a single path of length logm in the digital search
rithm constructs the digital search tree in O( ~ ,$"), tree corresponding to the interval (l", r"), and the
j-th substrings {XI),,,Xlt+l,j!.. . , X,),j} form a
+
time using O ( mlogm n ) operations. digital search tree corresponding to the interval
( 1 ' - r'). \ I I

Algorithm Digital-Search-Tree
Input: A sorted list L = ( X I ,Xz,. . . , X,) of m input [Step 1
For each interval (l',r') of Xi,j, let the
eaf node corresponding to the substrings
strings, where gLl lXil = n. {Xpi-l, Xp+l,j-l,,. ., X,1,j-1} be the root node
Output: The igrta search tree for the list L . of t e tree corresponding to the substrings
begin {XIf,j,XII+l,j,...,X,/.j}.

[Step 1 ] Partition each string Xi = (u,,1,ui,z,.. . , u i , ~ , ) , end


1 5 i 5 m, into si = [ elsub-
One can show the following theorem concerning the
strings, say, Xi,l = ( a i , l ,ai,2 . ai,logm)r
' 7 above algorithm,
Xi,z = (~i,logm+ir...,ui,21ogm),. . . ! Xi,s, =
('ii(s.--l)logm+l,. . ' i n l a ) ;
. ? hence Ixi,jl = logm, Theorem 4.1 Given a sorted list of m strings whose
for 1 5 j 5 si - 1, and !Xi,,,[5 logm. total length is n , the corresponding digital search
[Step 2 ] For each substring Xi,, of Xi, where 1 5 i 5 m be constructed i n O(ld",Y:,", ) ' a m e using
and 1 5 j 5 s i , find the minimum interval ( l , ~ ) , O ( m l o g m + n ) operations.

353
5 A Randomized Sorting Algorithm Algorithm Binary-Search-Preprocessing correctly con-
In this section, we present a simple randomized al- structs the auziliary array A’[l..m’; l..I] in O(a(m‘))
gorithm that sorts a list C = ( X I ,X Z ,. . . , X,) of m time using a linear number of operations. 0
input strings, where ELl lX,l= n , in O(1ogm) time We now provide a binary search algorithm that opti-
+
using O(m1ogm n ) operations, with high probabil- mally finds a given string in a sorted list of strings.
ity. As in Section 3, we first consider the case where The algorithm starts by comparing the given string Y
all the input strings are of the same length 1 = 2, with the middle string in the sorted list. The first posi-
and later generalize the result. We also assume that tion in which these two strings differ gives a restricted
the input strings are stored in a n array X[l..m; 1 . 4 , range where Y can occur. We use the information
where 1 = 2. stored in A‘ to determine the new search range for Y.
Our randomized algorithm requires an efficient al- The detailed algorithm and the proof of the following
gorithm for the following string search problem. Given lemma are in [17].
a sorted list C’= (Xi , Xi,. . . ,X’,) of m’ strings, each
of length I , and a string Y of tge same length I , we L e m m a 5.2
need an algorithm to search for Y in L’ serially. A Given a sorted list C‘ = (Xi, Xi, . . . ,XL,),a string
naive binary search algorithm will take O(1 . log m‘) Y , and the auziliary array A’[l..m’; 1 . 4 , the position
time. Kosaraju [18] provides an O ( I d m - time ) al- of Y in C‘ can be determined in O(1ogm’ + I ) time
gorithm. We now develop a very simple algorithm that sequentially. 0
+
takes O(1ogm’ I ) time given that the input list C’ The following randomized algorithm uses the above
is properly preprocessed. The following algorithm for
preprocessing the input list is similar to Step 3 of Al- search algorithm to sort a list of m strings, each of
gorithm Digitalsearch-Tree of previous section, and +
length 1, in O(1ogm I ) time using O(m1ogm n ) +
can be done in O(cu(m)) time using O ( n ) operations. operations, with high probability. This algorithm can
be modified t o sort m strings of different lengths in
O(1og m) time using O ( m log m + n) operations as in
A l g o r i t h m Binary-Search-Preprocessing Section 3.
Input: A sorted list L’ = ( X i , Xi,. . . , XL,) of
m’ strings, each of length 1. C’ is stored in array Algorithm RandomizedSorting
X’[l..m’; l..l]. We use two auxiliary Boolean arrays I n p u t : A list C = ( X l , X a , . . .,X,)of m strings,
A[l..m’; 1.4 and E[l..m’; 1 . 4 . where 1x11= . . . = IX,I = = 1. t
O u t p u t : An array A’[l..m’; 1..1]. For each i and j , o u t p u t : The list C in sorted order.
A ’ [ i , j ] contains a pair of integers ( r l r r 2 ) such that begin
rl 5 i 5 rz and (r1 , T Z ) is the maximum interval such
+
that X ’ [ r I , k ] = X ’ [ q 1, k ] = . . . = X’[rz,k], for all [Step 1 ] Pick randomly & strings and sort them de-
IC such that 1 5 k 5 j . terminist ically.
[Step 2 ] Preprocess the sorted list of the $ & strings
begin
1. forall 2 5 i 5 m’ and 1 5 j 5 1 do i n parallel introduced in Step 1 and determine the position
of each of the m input strings in the sorted list.
if X ’ [ i - l , j ]= X’[i,j] then A[i,j] +-0

A[1,1] = 1;
else A [ i ,j ]
+- 1; [Step 3 ] Partition the set of input strings into (e
+
1) sets according to their positions in the sorted
2. forall 1 5 i 5 m’, find the smallest k i
such that A [ i , k ]= 1 in parallel; list. Each such set consists of at most O(log2m)
3. forall 1 5 i 5 m’ and 1 5 j 5 1 do i n parallel strings with high probability.
if j < ki then E [ i , j ] t 0 else B [ i , j ] 1;
4. forall 1 5 i 5 m‘ and 1 5 j 5 1 do i n parallel
+- [Step 4 ] Sort all the
parallel.
(e
+ 1) sets independently in
compute r1 for the interval ( T I , r 2 ) of A ’ [ i ,j]
by finding the largest integer end.
k 5 i for which B[k,j] = 1;
compute r:!for the interval similarly; L e m m a 5.3 Let m’ be the number of strings in any
end s e t after Step 9 ofthe above algorithm. Then Pr{m’ 2
8 log2 m > 5 m-4.
Step 2 of the above algorithm can be done in O( 1) L e m m a 5.4 Given a list of m strings each of length
time using O(m’1) operations by Lemma 2.2. Steps 11- - 1, Algorithm Randomized-Sorting sorts the in-
m
1 and 3 can be also performed within the same com-
plexity bounds. Step 4 can be done in O(cu(m’))time
put strings in O(1og m + 1 ) time using O(m log m n ) +
operations with high probability. 0
and O(m’1) operations by Lemma 2.3. Thus, we have
the following lemma. L e m m a 5.5 Given a list of m strings each of length
1
m
1 = 1, th,e sorted list of th.ese strin,gs can be found in
L e m m a 5.1 Given a sorted 12.52 O(logm) time using O(m1ogm + n ) operations with
C’ = ( X i , Xi,.. . , X L x , ) of m’ strzngs each of length 1, high probability. 0

354
Finally, we generalize this result to the case where the Lemma 6.1 Let C = (cO,cl, . , c n - l ) be a circular
input strings are of different lengths. The proof of the string with a unique m . s . p . If ci+l = C j + l , f o r all
following theorem is in [17]. + + +
1 = 1 , .. . , s, then any of {i 1 , . . . , i s 1) n { j +
+ +
1 , . . .,j s 1) can not be the m . s . p . 0
, = ( X 1 , X 2 , .. . , X m ) of
Theorem 5.1 G i v e n a list C
m strings, where E r l lXil = n , the sorted list of Algorithm Simple-MSP
the input strings can be found in O(1ogm) t i m e us- Input : A non-repeating circular string C =
ing O(m1ogm + n ) operations with high probability. ( c O I c l , ..., c , , - ~ ) . (Assume for convenience that n =
0
2k, for some integer IC > 0.)
Output : The m.s.p. of the input string.
We next consider an application of our sorting algo-
rithms, namely that of determining the minimal start-
ing point of a circular string. begin
1 . Mark all n positions as candidates for the m.s.p.;
6 Finding a Minimal Starting Point of 2. for i = 1 to logn do
for each block of size 2' do in parallel
a Circular String
Compare the two strings of length 2'
c 1 , . . . , c n - l ) be a strin over the ar-
Let C = (CO, starting at the candidate positions,
bitrary alphabet C. The string is cafled circular and eliminate one of them;
if the following n strings are considered equivalent; end
(cj,.. . ., cn-l, CO, . . . , c j - I ) , j = 0 , . . ., n - 1. Let
C ( J O=) [cj,, . . . , c n - l , CO, . . . , cj,-1] be a linear string
of C with j~ as a starting point. Given C ( i ) and C(j) We assign 2' processors to each block of size 2', and
we define C ( i ) 4 C ( j ) i%f [ c i , . . . , c n - l , c9,. . . , ~ i - ~ j compare the two strings of length 2' each starting from
precedes [ c j , . . . , c n - l , CO, . . . , cj-11 in lexicographic its candidate position. If the two strings are differ-
order, and C ( i ) 5 C ( j ) iff C ( i ) + C ( j ) or C ( i ) = ent, we find the smaller string and mark its starting
C ( j ) . C(j0) is minimal if C(j0) 5 C(j) for all position as a further candidate. If the two strings
j = 0 , . . . , n - 1. The index ju is called a minimal are the same, we mark the first of the two candi-
starting point of C (m.s.p. in short). A circular dates as a further candidate by using Lemma 6.1.
string C is defined to be repeating if i t has more This can be done in constant time using O(2') op-
than one m.s.p. erations by Lemma 2.2. Thus, the above algorithm
Given a circular string, the problem of finding a finds the m.s.p. of a non-repeating circular string in
minimal starting point can be solved in linear se- O(1ogn) time and O(nlogn) operations on the Com-
quential time [8, al]. This problem can be also mon CRCW PRAM. Now, we describe the optimal
solved in O(lognlog1o n ) time [14] or O(1ogn) time algorithm.
[16] using O ( n loglogng) operations on the Arbitrary
CRCW PRAM if the input alphabet is restricted to Algorithm Optimal-MSP
{ 1 , 2 , . . . , no(1)}. In this section, we show that the two Input : A non-repeating circular string C =
parallel algorithms of Sections 3 and 5 can be used to ( C O , c1,. . . , cn-l). (Assume for convenience that n =

that runs in O( ,:$z:n)


derive an optimal parallel algorithm for this problem
time deterministically or in
O(1og n ) time with high probability. The correspond-
2', for some integer A > 0, and that n is a multiple of
log n .)
Output : The m.s.p. of the input string.
ing algorithm provides four kinds of improvement over
the algorithms of [14, 161. The first improvement is begin
that the input alphabet is not restricted; the second is
that the new algorithm uses O ( n )operations; the third [Step 1 ] Partition C into & = m blocks, B1,
is that the algorithm runs on a weaker model, namely B2, ..., Bm, where Bi = ( C ( i - l ) l o g n ,
the Common CRCW PRAM; and the fourth is that C(i-l)logn+l,...,Cilogn-l), 1 5 i I m.
the algorithm use O ( n ) space while those of [14, 161
use O(n'+') space, where is a positive constant. [Step:! ] Let $ be a special symbol not in C such that
In this section, we assume that the input circular VQ E C, a < $. Then construct m circular strings,
string is not repeating, since if it is repeating, we can C1, . . ., C, each of length 2 log n + 1, where Ci =
find its smallest repeating prefix in O(1ogn) time and ( B i ,B ( ; + l ) m o d m , $ ) ,1 5 i 5 m. Clearly, for each
O ( n )operations [9, 221. Clearly, the m.s.p. of a small- i , Ci is non-repeating and has only one m.s.p.
est repeating prefix is also an m.s.p. of the original
string. One can easily check that there is only one [Step 3 ] Find the m.s.p. of Ci serially, for each i =
m.s.p. in a non-repeating circular string. We be- 1, . . . , m. We can easily prove that this posi-
gin with a simple O(1ogn) time algorithm t,hat uses tion is better than any other position of {(i -
O(n1ogn) operations. We will use it later to derive l ) l o g n , ( i - l ) l o g n + l , . , . , i l o g n - - 1) in C by
a more efficient algorithm. This simple a.lgorithm is using Lemma 6.1. If the m.s.p. is in B i , mark the
based on the following lemma from [21]. symbol. Otherwise, do not mark any symbol in
Bi .

355
[Step 4 Now, there are a t most m candidates which have [7] P.C.P. Bhatt, K. Diks, T . Hagerup, V.C. Prasad,
een marked in Step 3. Construct a t most m T. Radzik, and S. Saxena, Improved Determin-
strings each of which starts a t a candidate and istic Parallel Integer Sorting, Information and
ends at just before the next candidate. Computation 94, pp. 29-47, 1991.
[Step 5 ] Sort the strings constructed in Step 4 by using [8] K.S. Booth, Lericographically Least Circular Sub-
one of the algorithms developed in Sections 3 and strings, Information Processing Letters 10, pp.
5. 240-242, 1980.
[Step 6 ] Replace each of the strings sorted in Step 5 with [9] D. Breslauer and Z. Galil, A n Optimal
its ranking in the sorted list from Step 5. Then C O(log logn) time Parallel String Matching Algo-
is reduced into a circular string of length at most rithm, SIAM J. Computing 19(6), pp. 1051-1058,
m =Logn L. 1990.
[Step 7 ] Apply Algorithm Simple-MSP to the resulting [lo] R. Cole, Parallel Merge Sort, SIAM J . Computing
string and find the final m.s.p. 17(4), pp. 770-785, 1988.
end [ l l ] R. Cole and U. Vishkin, Faster Optimal Prefix
Sums and List Ranking, Information and Com-
Lemma 6.2 The m.s.p. of the modified circular putation, 81(3), pp. 334-352, 1989.
string afler Step 6 of Algorithm OptimaLMSP main-
tains the same m.s.p as the original one. 0 [12] F.E. Fich, P.L. Ragde, and A. Wigderson, Rela-
tions between Concurrent- Write Models of Paral-
Clearly, all the steps of the above algorithm can be lel Computation, SIAM J. Computing 1, pp. 606-
done in O(1og n ) time using O ( n )operations determin- 627, 1988.
istically except for Step 5. Thus, we have the following
theorem. [13] T . Hagerup and 0. Petersson, Merging and Sod-
ing Strings in Parallel, Mathematical Founda-
Theorem 6.1 Given a circular string of length n , Al- tions of Computer Science 1992, 17th Int’l Symp.,

string in O( ,zz:,,)
gorithm Optimal-MSP finds the m s . ~ . of the input
time deterministically or O(1ogn)
time randomized, using O(n) operations on the Com-
Lecture Notes in Computer Science 629, Springer-
Verlag, pp. 298-306.
1141 C.S. Iliopoulos and W.F. Smyth, Optimal Algo-
mon C R C W PRAM. 0 rithms f o r Computing the Canonical Form of a
Circular String, personal communication, 1992.
[15] J . JhJh, A n Introduction to Parallel Algorithms,
References Addison-Wesley, Reading, MA, 1992.
[l) A.V. Aho, J.E. Hopcroft, and J.D. Ullman, The
Design and Analysis of Computer Algorithms, [16] J . JhJh and K.W. Ryu, A n Eficient Parallel Al-
Addison-Wesley, Reading, MA, pp. 78-84, 1974. gorithm for the Single Function Coarsest Partz-
tion Problem, Proc. 5th ACM Symposium on Par-
[a] M . Ajtai, J . Komlos, and E. Szemeredi, A n allel Algorithms and Architectures, 1993.
O(n1ogn) Sorting Network, Proc. 15th ACM
Symp. on Theory of Computing, pp. 1-9, 1983. [17] J . JdJd, K.W. Ryu, and U. Vishkin, Sorting
Strings and Constructing Digital Search fiees in
[3] R.J. Anderson and G.L. Miller, Optimal Parallel Parallel, CS-TR-3073, UMIACS-TR-93-39, Univ.
Algorithms for List Ranking, 3rd Aegean Work- of Maryland, 1993.
shop in Computing, Lecture Notes in Computer
Science 319, 1988, Springer-Verlag, pp. 81-90, [18] S.R. Kosaraju, On a Multidimensional Search
1988. Problem, Proc. 11th Annual ACM Symp. Theory
of Computing, pp.67-73, 1979.
[4] A. Apostolico, C. Iliopoulos, G.M. Landau, B.
Schieber, and U . Vishkin, Parallel Construction [19] C.P. Kruskal, Searching, Merging, and Sorting in
of a Suffix Tree with Applications, Algorithmica Parallel Compuiation, IEEE Trans. on Comp., C-
3(3), pp. 347-365, 1988. 32(10), pp. 942-946, 1983.
(51 0. Berkman and U . Vishkin, Recursive star- [20] R. Reischuk, Probabilistic Parallel Algorithms
Tree Parallel Data Structure, Proc. 30th Annual for Sorting and Selection, SIAM J . Computing,
Symp. Foundations of Computer Science, pp. 14(2), pp. 396-409, 1985.
196-202, 1989. Also, SIAM J . Computing, 1993,
to appear. [21] Y. Shiloach, Fast Canonization of Circular
Strings, J . Algorithms 2, pp. 107-121, 1981.
[6] A. Borodin and J .E. Hopcroft, Routing, Merging,
and Sorting on Parallel Models of Computation, [22] U . Vishkin, Optimal Parallel Matching in Strings,
Journal of Computer and System Sciences 30, pp. Information and Control 67, pp, 91-113, 1985.
130-145, 1985.

356

Вам также может понравиться