• No results found

2.5 More advanced algorithms

2.5.3 GreedyFuture

Who controls the past controls the future.

— GEORGEORWELL, 1984

We describe an offline BST algorithm called GreedyFuture, introduced by Lucas [68] in 1988 and described independently by Munro [76] in 2000. We define GreedyFuture in the second offline model. GreedyFuture satisfies the search-path-only restriction, i.e. after each access it only re-arranges the search path. GreedyFuture is an offline algorithm, in the sense that its re-arrangement of the search path depends on accesses in the future (hence the name “future”). Informally, it re-arranges the search path in such a way, that the sooner an element is accessed in the future, the closer it gets to the root (hence the name “greedy”). The initial tree in GreedyFuture can be arbitrary, although we will see that there is a canonical initial tree that plays an important role. Our description is loosely based on Demaine et al. [31].

We only specify the operation when accessing a key xi, and denote by T the BST before the access. We assume that the sequence of future accesses xi +1, . . . , xm is known.

GreedyFuture(T, xi)

1. Access(T ,xi) and let P denote the search path.

2. Let p = (p1, . . . , p|P |) be the sorted keys of P , let p0= −∞, and p|P |+1= +∞.

3. Let tj= min©t : t > i and pj −1< xt< pj +1ª, for all j = 1,...,|P|, or tj= +∞ if the set is empty.

4. Letτ = ¡t1, . . . , t|P |¢ and let Q =treap(p,τ). 5. Replace P by Q in T .

GreedyFuture accesses a key xiin the usual way, following the search path from the root to xi(step 1). Then, for each key pjin the search path we define the next access time tj, which is the index of the next access that is on pj, or in the open interval between the predecessor and successor of pj in the search path. We set this value to +∞ if there is no access in the future that falls in the interval in question (steps 2 and 3). We then replace the search path by a treap built of the search path, using the next access times as priorities (steps 4 and 5). Recall the definition of the treap that guarantees that the root is the node pj with smallest value tj, i.e. the one which is accessed first in the future. The same property is maintained recursively in the subtrees of the root.

We observe the following: if the next access xi +1is in the current search path (say pj), then it is guaranteed to become the root. This is because its next access time is tj= i + 1, and for all other keys pk, we have tk> i + 1 (since pjis not contained in any of the open inter- vals around other keys on the search path). If the next access xi +1is in one of the subtrees

2.5. More advanced algorithms 35

hanging from the search path, then we have exactly two keys pjand pj +1in the search path such that pj< xi +1< pj +1. In this case, tj= tj +1= i + 1, and all other next access times are greater than i + 1. Therefore, one of pjand pj +1has to become the root, and the other one its child, according to the tie-breaking decision of the treap. (In the original description of GreedyFuture, Lucas suggests breaking the tie according to whether the left subtree of pj or the right subtree of pj +1is accessed earlier in the future.) The subtrees are built recur- sively in the same way. Keys on the search path that are not accessed in the future (those with next access time +∞) are kept at the bottom of the constructed BST Qi(with ties bro- ken arbitrarily by the treap construction). Figure2.4illustrates the execution of GreedyFuture.

Figure 2.4: GreedyFuture execution. (left) Tree before accessing 8. (right) Tree after accessing 8, with future access sequence (9, 4, 6, 10, 11, 2, 6). Greedy- Future replaces the search path to 8 withtreap¡(4,6,8,10),(2,3,1,1)¢. The first sequence consists of the sorted keys in the search path, the second sequence consists of the future access times of the keys in the search path. Observe that 8 is not accessed in the future, but 9 is accessed at time 1, and it has 8 as predecessor. Similarly, although 10 is accessed only at time 4, since it is the

successor of 9, it is assigned priority 1.

The initial tree T0of GreedyFuture can be arbitrary. Since GreedyFuture is an offline

algorithm, we may as well construct T0depending on the input X . Perhaps the most natural

choice of initial tree is the one in which keys are already ordered by their first access time. More precisely, let ti = min {t : xt= i }, for all i ∈ [n], again assuming tj = +∞ if j is never accessed. Then, letτ = (t1, . . . , tn) and choose the initial tree T0=treap(τ). The choice of this

initial tree is natural, because it simulates the normal behavior of the algorithm. If at time zero GreedyFuture would “touch” every node, the resulting tree would be exactly the tree T0

described above. We will refer to this particular choice of the initial tree as canonical.

GreedyFuture is of interest for two main reasons. First, because it was proposed as a theoretical construction that could plausibly achieve a cost close to the offline optimum. Second, in a surprising development, Demaine et al. [31] showed in 2009 that GreedyFuture can be simulated by an online algorithm with asymptotically the same cost. (As we discuss in §2.7, this online algorithm conforms to our lenient online, but not to the strict online model.) Thus, besides Splay, GreedyFuture and its online variant discussed in §2.7are the most promising candidates for dynamic optimality. In the past few years it was shown that GreedyFuture matches essentially all bounds known to be matched by Splay, and also several that are not known to be matched by Splay. (To our knowledge the first such bounds were the pattern-avoiding bounds discussed in §4. Another recent example is the lazy finger bound [54].) Furthermore, proofs for GreedyFuture (and its online variant) tend to be simpler than those for Splay, especially in the geometric model described in §2.7. In §5we revisit GreedyFuture and give a number of new interpretations for it.

We mention some known results about the behavior of GreedyFuture. In 2011, Fox [42] showed that an access lemma similar to Theorem2.15holds for GreedyFuture, from which the results of the following theorem follow. (Observe that GreedyFuture does not have the access-to-root property. Nevertheless, the properties can be proven in the geometric view of GreedyFuture, described in §2.7.) The balance condition was also independently shown by Goyal and Gupta [50].

Theorem 2.18([42]). GreedyFuture satisfies the balance, static optimality, static finger, work-

ing set, and sequential access conditions.

In 2016, Iacono and Langerman showed the following.

Theorem 2.19 ([54]). GreedyFuture satisfies the lazy finger condition.

Similarly as for Splay, we have no non-trivial results about the approximation ratio of GreedyFuture.

Problem 20. Is there some c = o(logn), such that the cost of GreedyFuture for X is at most

c ·OPT(X )?

Problem 21. Is there some c = o(logn), such that the cost of GreedyFuture for X is at most

c ·OPTstr,root(X )? Even more specifically, can we bound the cost of GreedyFuture as c times the cost of Splay?

The performance of GreedyFuture for traversal sequences is studied in §4in a more general context and we present several results in this direction. Let us nevertheless state here an open question that is not answered by our work.

Problem 22. Is the cost of GreedyFuture O(n) for every traversal sequence of length n with every initial tree?

Finally, we mention again that GreedyFuture can be simulated by an online algorithm with a constant factor overhead. We discuss this in §2.7. It is somewhat unsatisfactory that this online algorithm does not conform to the strict model. Therefore we ask the question.

Problem 23. Is there a strict online algorithm

A

, and some c = o(logn), such that the cost of

A

for every (sufficiently long) input sequence X is at most c times the cost of GreedyFuture for X ? In particular, is Splay such an algorithm?