an optimal algorithm for finding all the jumps of a monotone step-function

10
JOURNAL OF ALGORJTHMS 6,265214 (1985) An Optimal Algorithm for Finding All the Jumps of a Monotone Step-Function REFAELHASSINAND NIMROD MEGIDDO* Stutistics Department. Tel Aviv University, Tel Aviv, Isruel69978 Received July 26,1983 The idea of binary search is generalized as follows. Given f: (0, 1, , N} + {O,...,K) such that f(O)=O, f(N)=K, and f(i)<f(j) for i<j, all the “jumps” of f. i.e.. all is such that f(i) > f(i - 1) together with the difference f(i) - f(i - 1) are recognized within K [log,( N/K)1 + I( N - l)2-t10s2(‘v~K)1 ] j-evaluations. This is proved to be the exact bound in the non-trivial case when K Q N. An optimal strategy is as follows: The first query will be at i = 2”. where m = [log, (N/K)]. An adversary will then respond either f(i) = 0 or f(i) = l as explained in the paper. o 1985 Academic PES. IIIC. If f: {O,l,. . .) N} + {O,l} is monotone and f(0) = 0, f(N) = 1, then f has one “jump” which can be recognized by a binary search within [log, N 1 f-evaluations. In this paper we generalize this situation to the case of a monotone nondecreasing step-function, i.e., f: (0, 1, . . . , N } + (0, 1,. . ., K}, where f(0) = 0, f(N) = K, and f(i) Gf(j) for i -cj. Obvi- ously, by performing K binary searches one may recognize f(i) for all i, so that K[log,N 1 f-evaluations should suffice. Also, trivially, if K z N then N - 1 f-evaluations are sufficient and may also be necessary in the worst- case. We prove in this paper that the exact upper bound on the number of f-evaluations required for the recognition of all the jumps is K [log( N/K )I + I( N - 1)2-[lca(N/K)1 1, where log X = Max(0, log, X) (and hence our bound is equal to N - 1 if K > N/2). An optimal strategy may be described as follows. The first query will be at i = 2", where m = [log( N/K)]. Let the response be f(i) = K, (0 G K, G K). We now proceed, recursively, with *Current address: IBM Research Laboratory, San Jose, Calif. 95193. The work of this author was supported in part by the National Science Foundation Grants ECS-8121741 and ECS-8218181. 265 Ol%-6774/85 $3.00 Copyright Q 1985 by Academic Press, Inc. All rights of reproduction in any form reserved.

Upload: refael-hassin

Post on 21-Jun-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An optimal algorithm for finding all the jumps of a monotone step-function

JOURNAL OF ALGORJTHMS 6,265214 (1985)

An Optimal Algorithm for Finding All the Jumps of a Monotone Step-Function

REFAELHASSINAND NIMROD MEGIDDO*

Stutistics Department. Tel Aviv University, Tel Aviv, Isruel69978

Received July 26,1983

The idea of binary search is generalized as follows. Given f: (0, 1, , N} +

{O,...,K) such that f(O)=O, f(N)=K, and f(i)<f(j) for i<j, all the “jumps” of f. i.e.. all is such that f(i) > f(i - 1) together with the difference f(i) - f(i - 1) are recognized within K [log,( N/K)1 + I( N - l)2-t10s2(‘v~K)1 ] j-evaluations. This is proved to be the exact bound in the non-trivial case when

K Q N. An optimal strategy is as follows: The first query will be at i = 2”. where m = [log, (N/K)]. An adversary will then respond either f(i) = 0 or f(i) = l as explained in the paper. o 1985 Academic PES. IIIC.

If f: {O,l,. . .) N} + {O,l} is monotone and f(0) = 0, f(N) = 1, then f has one “jump” which can be recognized by a binary search within [log, N 1 f-evaluations. In this paper we generalize this situation to the case of a monotone nondecreasing step-function, i.e., f: (0, 1, . . . , N } + (0, 1,. . ., K}, where f(0) = 0, f(N) = K, and f(i) G f(j) for i -cj. Obvi- ously, by performing K binary searches one may recognize f(i) for all i, so that K[log,N 1 f-evaluations should suffice. Also, trivially, if K z N then N - 1 f-evaluations are sufficient and may also be necessary in the worst- case.

We prove in this paper that the exact upper bound on the number of f-evaluations required for the recognition of all the jumps is K [log( N/K )I + I( N - 1)2-[lca(N/K)1 1, where log X = Max(0, log, X) (and hence our bound is equal to N - 1 if K > N/2). An optimal strategy may be described as follows. The first query will be at i = 2", where m = [log( N/K)]. Let the response be f(i) = K, (0 G K, G K). We now proceed, recursively, with

*Current address: IBM Research Laboratory, San Jose, Calif. 95193. The work of this author was supported in part by the National Science Foundation Grants ECS-8121741 and ECS-8218181.

265 Ol%-6774/85 $3.00

Copyright Q 1985 by Academic Press, Inc. All rights of reproduction in any form reserved.

Page 2: An optimal algorithm for finding all the jumps of a monotone step-function

266 HASSIN AND MEGIDDO

the two resulting problems, namely, finding all the jumps of f over the sets ((41,. . . , i} and {i,...,N}.

One typical application of our problem is the following: Suppose that we need to recognize the exact boundaries of blocks of words whose first letters are identical. Such a problem arises when we wish to find a given vector in a preprocessed set of vectors (see [l-4]).

We now turn to a description of the problem in the form of a two-person zero-sum game. For the fundamental concepts of game theory see Owen [5].

A Game Representation

The following game is obviously equivalent to our problem of identifying all the jumps of a monotone step-function. Given a sequence of N boxes and a set of K identical balls, an adversary distributes the balls among the boxes and we have to discover how many balls are contained in each box. In other words, an adversary chooses N integers n,, . . . , nN (ni z 0, Cn i = K) and we may ask for any number Ni (1 < N1 Q N - 1) what is the total number of balls contained within the first Ni boxes. Our goal is to minimize the number of queries while finding all the numbers ni.

We denote by (N, K) the game which starts with N boxes and K balls. The different games are recursively related as follows. The first query splits the sequence (1, . . . , N) into two disjoint sequences of Ni and N, boxes (Ni + N2 = N). The adversary responds by splitting the set of K balls into two sets of K, and K, elements (K, + K, = K). Thus, after the first query and the adversary’s response our game actually reduces to the (independent) playing of two games: (N,, K,) and (N,, K2). Suppose that at the end of the play we have to pay to the adversary an amount equal to the number of queries. Let V(N, K) denote the value of the resulting game. It follows that V(N,O) = 0 and for K 2 1, V(N, K) = 1 + MinN1+N,=NMaxK,+K,=K { V( N,, K,) + V( N2, K2)}. It is also obvious that for K 2 N, V( N, K) = N - 1. The main result of this paper is

THEOREM. For K 2 1,

(1) V(N, K) = K[log(N/K)j + [(N - 1)2-[“‘s(N’K)j] (log X = Max(0, log, X)).

The proof will be established as follows. Let us denote the right-hand side of (1) by F( N, K ). In what follows, Lemma 1 is merely technical. Lemma 2 demonstrates the optimal strategy of the adversary, that is, provides a response scheme that guarantees the value of the game. In other words, Lemma 2 proves that F( N, K) is a lower bound on the value of the game. Lemma 3 describes a strategy of queries which terminates the search within F(N, K) queries against any adversary. In other words, Lemma 3 shows that F( N, K) is also an upper bound.

Page 3: An optimal algorithm for finding all the jumps of a monotone step-function

JUMPS OF A MONOTONE STEP-FUNCTION 267

To simplify notation we henceforth denote

(2) m = /log( N/K)] and define

LEMMA 1. Every pair (N, K) (1 Q K < N) defines a unique triple of integers (a,p,r) such that O<adK, a+/?=K,O<r<2” andN= a2” + p2m+i + r. We can express F( N, K) in terms of a, /3, and r by F(N, K) = (m + l)(a + p) + j3 - 6(r).

Proof. It follows that there exists a unique pair of integers (fi, r) such that 0 Q p < K, 0 < r < 2”, and N = (K + /3)2” + r. Let a = K - p. It follows that a > 0 and N = a2”’ + j32m+1 + r. This implies

F( N, K) = Km + I( N - 1)2-“‘1

= Km + [(a2” + /32m+1 + r - 1)2-“]

= Km + a + 2p - S(r)

= (m + l)(a + /3) + j3 - 6(r).

We are now ready to describe a strategy for the adversary which guaran- tees at least I;( N, K) queries. Notice that a strategy for the adversary is well defined if we specify for every Ni (1 Q Ni 6 N - 1) a number K, (0 Q K, G K) which is the adversary’s response to our first query (in the game (N, K )) as to the number of balls contained in the first Ni boxes.

The strategy and the proof of what it guarantees are contained in the following lemma.

LEMMA 2. For every N, K, and Nl such that 1 < Ni d N - 1, there exists a number K, such that 0 < K, < K for which

F(N,, K,) + F(N,, &) + 1 a F(N, K)

where N2 = N - Nl and K, = K - K,.

Proof. We distinguish different cases for the definition of an optimal response K,.

Case 1. Nl < a2m. Let N1 = i2” + rl, where i < a and 0 Q rl < 2”.

Subcuse 1.1. r 2 r, or /3 > 0. In this case, the adversary chooses K, = i. If i = 0 then F(N,, K,) = 0. Otherwise (if i > l),

Page 4: An optimal algorithm for finding all the jumps of a monotone step-function

268 HASSIN AND MEGIDDO

so that

F(N,, K,) = im + I

i2” + rl - 1 2”

I = i(m + 1) - 6(r,).

In both cases, F( N,, K,) 2 i(m + 1) - 6(r,). By the assumptions of this subcase,

[Ioga] = [log (a - i)2” + p2m+1 + r - rl a+/3-i I

=m

and

F(N,, K2) = (a + p - i)m + I

(a - i)2” + /?2m+’ + r - rl - 1 m

[r-:‘j I

=(m+l)(a+p-i)+j3+ ‘,

Thus,

> (m + l)(cu + p - i) + p - 1.

w% K,) + w,, K2) + 1

> i(m + 1) - S(r,) +(m + l)(a + p - i) + /3

2 (m + l)(a + j3) + B - 6(r)

= F(N, K) (by Lemma 1).

Subcase 1.2. j3 = 0 and r < rl -C r + 2”-‘. Again, the adversary chooses K, = i and F( N,, K,) > i(m + 1) - 6(r,). If i = K then F(N,, K2) = 0; otherwise (if i -C a),

so that

F(N,, K2) = (a - i)(m - 1) + I

(a - i)2” + r - rl - 1

= (a - i)(m - 1) + ~(CY - i) _‘;Y’ I

In both cases, F(N,, K,) > (a - i)(m + 1) - 1. Thus,

F(% K,) + F(&, K2) + 1

> i(m + 1) - S(rl) +(a - i)(m + 1)

= (WI + l)a - 6(r,) 2 (m + l)a - S(r) = F(N, K).

Page 5: An optimal algorithm for finding all the jumps of a monotone step-function

JUMPS OF A MONOTONE STEP-FUNCTION 269

Subcase 1.3. @ = 0 and rr 2 r + 2”-‘. Recall that rr < 2”. In this case the adversary chooses K, = i + 1. (The definition of i implies i < a so that K = cx + j3 2 i + 1 and K, = i + 1 is feasible.) Here

so that

F(N,, K,) = (i + l)(m - 1) + i2mlm:1- ’ ] I

r1 - 1 = (i + l)(rn - 1) + 2i + -

I J 2m-1 .

NotethatifS(r)=Othenr>Osothat [(rl-1)/2”-‘I =landhencein any case I( r - 1)/2”-‘1 > 1 - 6(r). Note that in the present subcase ZVt = i2” + rl < o2”’ + r = N so that i < (Y. If i + 1 = K then F(N,, K2) = 0; otherwise (i + 1 c (IL),

(a - i)2” + r - rl [lot+]= kc% a-i-l J=m so that

F(N,, K2) = (a - i - 1)m + I

(a - i)2” + r - r1 - 1 2” I

=(ar-i-l)m+a-i-1

thus,

Wl, K,) + fv*, &I + 1 > (i + l)(m - 1) + 2i + 1 - 6(r) +(a - i - l)(m + 1) + 1

= a(m + 1) -6(r)

= F(N, K).

Case 2. Nr > (u2”‘. Let Nt = (~2” + i2”‘+’ + rl, where 0 < i Q /l and 0 < r1 < 2m+1.

Subcase 2.1. rl 6 r or J? = i. Here, the adversary chooses K, = (Y + i. It follows that

. .

Page 6: An optimal algorithm for finding all the jumps of a monotone step-function

270 HASSIN AND MEGIDDO

so that if either (Y > 1 or rl < 2” then [log(N,/K,)] = m and

F(N,, K,) = (a + i)m + I a2” + i2”+’ + rl - 1

2” I rl - 1

= (a + i)(m + 1) + i + - I I 2” .

If CY = 1 and rl >, 2” then [log(N,/K,)] = m + 1 so that

F(N,, K,) = (a + i)(m + 1) + I ~2~ + i2”+’ + r, - 1 p+l

J

>, (a + i)(m = 1) + i,

If i = j3 then F(N,, K2) = 0; otherwise (if i < /3),

so that

F(N,, K,) = (p - i)(m + 1) + I (p - i)2”+’ +r-r,-1

2 m+l I

= (/3 - i)(m + 1) = p - i - S(r - rl).

In both cases F( N2, K2) z (p - i)( m + 2) - 6( r - rl). Thus,

WI, K,) + fw,, K,) + 1

2 (a + i)(m + 1) + i - 6(r,) +(/3 - i)(m + 2)

+S(r,) - 6(r) - 1 + 1

= (a + /?)(m + 1) + p - 6(r)

= F(N, K).

Subcase 2.2. r < rl < r + 2” and B > i. Again, the adversary chooses K, = a + i and F(N,, K,) > (a + i)(m + 1) + i - S(rI). Here,

so that

F(N,, K2) = (p - i)m + I

(/3 - i)2m+’ + r - r, - 1 m

J

=(@-i)m+2(P--i)+ ‘-I’- I2 I

-1 ,

Page 7: An optimal algorithm for finding all the jumps of a monotone step-function

JUMPS OF A MONOTONE STEP-FUNCTION 271

Since rl > 0, S(r,) = 0 so that

> (a + i)(m + 1) + i +(/3 - i)m + 2(/? - i) + 1’ -2ms ’ I+ 1

=(a+fi)(m+1)+/?+ [r-;m-‘]+l

2 (a + B)(m + 1) + B - S(r)

= F(N, K)

Subcuse 2.3. rI > r + 2” and /3 > i. Here the adversary chooses K, = a + i + 1. If K, = /3 - i - 1 = 0 then F(iV,, K2) = 0. Otherwise,

so that

F(&, K,) = (j3 - i - l)(m + 1) + I

(j? - i)2m+1 +r-r,-1 2m+l

I

= (j3 - i - l)( m + 1) +(B - i - 1).

Also,

= log I

(a + i + 1)2” + i2” + rI - 2” a+i+l I

= m,

so that

F(N,, K,) = (a + i + 1)m + I

a2m + i2”+’ + rl - 1 2” I

= (a + i + 1)m +(a + 2i + 1)

= (a + i + l)(m + 1) + i

Page 8: An optimal algorithm for finding all the jumps of a monotone step-function

272 HASSIN AND MEGIDDO

and

1 + F(N,, K,) + F(N,, K,) = 1 +(a + i + l)(m + 1) + i

+(p - i - l)(m + 1) + p - i - 1

= (a + /3)(m + 1) + p

2 (a + S)(m + 1) + B - 6(r)

= F(N, K).

This completes the proof that the adversary can force at least F( N, K) queries. We shall now prove that F( N, K) queries always suffice. Essen- tially, an optimal query at the state of N cells and K balls is N1 = 2” (m = bdN,‘K)~)-

LEMMA 3. For every N and K 2 2, for all possible responses K, (0 < K, 6 K ), F(2’“, K,) + F( N - 2”, K - K,) + 1 Q F( N, K ).

Proof (a) We first prove the lemma for K, = 0.

Case (al). N - 2” > K2”. In this case [log( N - 2”)/K ] = m and

F(2”,0) + F( N - 2”, K) + 1

= 0 + Km + I( N - 2” - 1)/2”] + 1

= Km + I( N - 1)/2”] = F( N, K).

Case (a2). N - 2” < K2”. Since N 2 K2” it follows that N - 2” > (K - 1)2” < K2”-‘. Hence, in this case, [log( N - 2”‘)/K] = m - 1. Since we assume N - 2” < K2” then K > N/2” - 1 and thus K + 1 - (N - 1)/2” > 0. Therefore,

F(2”,0) + F(N - 2”, K) + 7

= K(m - 1) + N ,zri- ’ I J

+ 1

= [(Km +(N - 1)/2”) -(K + 1 -(N - 1)/2”)]

G 1Km +(N - 1)/2m] = F(N, K).

(b) Second, we prove the lemma for K, = 1. First, F(2”, 1) = m. Also, since m = [log( N/K)], it follows that 2” 6 N/K < 2”+l, so that

(K - 1)2” < N - 2” < (K - l)2m+’ + 2”.

Page 9: An optimal algorithm for finding all the jumps of a monotone step-function

JUMPS OF A MONOTONE STEP-FUNCTION 273

Case (bl). N - 2” < (K - 1)2”+‘. In this case ilog( N - 2”)/( K - l)] = m so that

=m+(K-l)m+ I

= Km +/(N-1)/2"] = F(N,K).

Case (b2). N - 2” > (K - 1)2m”‘. In this case [log( N - 2”)/( K - l)] = m + 1 and

F(2"', 1) + F( N - 2”, K - 1) + 1

= m +(K - l)(m + 1) +[(N - 2” - 1)/2”+‘j + 1

= Km + K+[(N- 2"- 1)/2”+‘]

=Km+ I 2"+'K+N- 2"- 1

2 m+l I

<Km+ (N+2m)+N-2m-1 .

I 2mtl I

= Km +[(2N- 1)/2”+‘]

= Km +[(2N - 2)/2"+']

=Km +[(N- 1)/2”‘] = F(N,K),

where the inequality follows from our assumption that K2"+l < N + 2". (c) We now complete the proof assuming that K, > 2. Let K, = 2kl +

rl, where 0 < r1 < 2kl. We note that for K, z 1,

I I IogF = m - k, + 6(r,)-1, 1

so that

F(2",K,) = K,(m - k, + 6(r,)- l)+ I

2"- 1 2" - k, + S(r,)-1 I

= K,(m - k, - 1) + 2kl+1 - 1.

Let K, = K - K, and m2 = [log( N - 2")/K,]. Then

a = l+ F(2",K,)+F(N- 2"',K,)

= K,(m - k, - l)+ 2kl+1+ K,m, + I .

Page 10: An optimal algorithm for finding all the jumps of a monotone step-function

274 HASSIN AND MEGIDDO

Since for K, 2 2, K,(k, + 1) z 2kl(k, + 1) 2 2kl+ ‘, it follows that

a 6 K,m + K,m, + I N-2”-1 21112 I

= Km + K,(m, - m> + I

N-2”-1 2m2 I

Q Km + K2(2Qwm - 1) + I

N-2”-1 I 2”’ . It follows from the definition of m2 that 2”‘~ Q (N - 2”)/K, and thus

K, 6 (N - 2”)/2”*. Therefore

a<Km+ Eg . (yv - 1) + 1 N -2’-“: - l]

=Km+-$- N - 2” --1+

I N-2”-1

2m2 2m2

Since

it follows that a < Km + I( N - 1)/2m] = F( N, K). This completes the proof that F( N, K) queries suffice, so that V( N, K) = F( N, K ).

REFERENCES

1. J. L. BENTLEY AND J. B. SAXE, Algorithms on vector sets, SIGACT NDVS 11 (1979). 36-39. 2. R. HASSIN AND N. MEGIDDO, “Searching a Presorted Set of Vectors,” Department of

Statistics, Tel Aviv University, June 1980. 3. D. S. HIRSCHBERG, On the complexity of searching a set of vectors, SIAM J. Comput. 9

(1980). 126-129. 4. S. RAO KOSARAJU, On a multidimensional search problem, SIGACT News 11 (1979). 5. G. OWEN, “Game Theory,” Saunders, Philadelphia, 1968.