




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





In this lecture, we’ll consider data structures for decomposable search problems. A search problem asks us to store a set A of constant-sized items so that certain queries Q(x, A), specified by a constant-sized query object x, can be answered quickly. A search problem is decomposable if, for any disjoint sets A and B, the answer to a query over A ∪ B can be computed in constant time from the answers to queries over the individual sets; that is,
Q(x, A ∪ B) = Q(x, A) Q(x, B)
for some ‘trivial’ commutative binary function . I’ll use ⊥ to denote the answer to any query Q(x, ∅) over the empty set, so that q ⊥ = ⊥ q = q for all q. Examples of decomposable search problems include the following.
First, let’s describe a general transformation that adds the ability to insert new items to a static data structure, originally due to Jon Bentley and his PhD student James Saxe* [1]. Suppose we have a static data structure that can store n items in space S(n), after P (n) preprocessing time, and answer a query in time Q(n). We will construct a new data structure with size S′(n) = O(S(n)), preprocessing time P ′(n) = O(P (n)), query time Q′(n) = O(log n) · Q(n), and amortized insertion time I′(n) = O(log n) · P (n)/n. In the next section, we will see how to achieve this insertion time even in the worst case. Our data structure consists of = blg nc levels L 0 , L 1 ,... , L− 1. Each level Li is either empty or a static data structure storing exactly 2i^ items. Observe that for any value of n, there is a unique set of levels that must be non-empty. To answer a query, we perform a query in each non- empty level and combine the results. (This is where we require the assumption that the queries are decomposable.)
NewQuery(x): q ← ⊥ for i ← 0 to ` − 1 if Li 6 = ∅ q ← q Query(x, Li) return q
The total query time is clearly at most
i=0 Q( i) < `Q(n) = O(log n) · Q(n), as claimed.
Moreover, if Q(n) > nε^ for any ε > 0, the query time is actually O(Q(n)).
The insertion algorithm exactly mirrors the algorithm for incrementing a binary counter, where the presence or absence of each Li plays the role of the ith least significant bit. We find the smallest empty level k; build a new data structure Lk containing the new item and all the items stored in L 0 , L 1 ,... Lk− 1 ; and finally discard all the levels smaller than Lk. See Figure 1 for an example.
Insert(x): Find minimum k such that Lk = ∅ Lk ← {x} ∪ ⋃ i<k Li^ 〈〈takes^ P^ (2k)^ time〉〉 for i ← 0 to k − 1 destroy Li
During the lifetime of the data structure, each item will take part in the construction of lg n different data structures. Thus, if we charge
I′(n) =
∑^ lg^ n
i=
P (2i) 2 i^
= O(log n) P (n) n
for each insertion, the total charge will pay for the cost of building all the static data structures. If P (n) > n1+ε^ for any ε > 0, the amortized insertion time is actually O(P (n)/n).
⇓ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ⇓ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 ⇓ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 ⇓ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 ⇓ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 ⇓ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 ⇓ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 ⇓ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 Figure 1. The 27th through 33rd insertions into a Bentley/Saxe data structure
We can modify this general transformation to achieve the same space, preprocessing, and query time bounds, but now with worst-case insertion time I′(n) = O(log n) · P (n)/n. Obviously we cannot get fast updates in the worst case if we are ever required to build a large data structure all at once. The key idea is to stretch the construction time out over several insertions. As in the amortized structure, we maintain ` = blg nc levels, but now each level i consists of four static data structures, called Oldesti, Older (^) i, Old (^) i, and New (^) i. Each of the ‘old’ data structures is either empty or contains exactly 2i^ items; moreover, if Oldesti is empty then so is Older (^) i, and if Older (^) i is empty then so is Old (^) i. The fourth data structure New (^) i is either empty or a partially
Figure 2. The 27th through 33rd insertions into a Overmars/van Leeuwen data structure
also appears in M. To insert an item, we insert it into M. To delete an item, we insert it into G. Finally, to answer a query, we compute Q(x, M ) Q(x, G). The only problem with this approach is that the two component structures M and G might become much larger than the ideal structure storing M \ G, in which case our query and insertion times become inflated. To avoid this problem, we rebuild our entire data structure from scratch— building a new main structure containing M \ G and a new empty ghost structure—whenever the size of G exceeds half the size of M. Rebuilding requires O(P (n)) time, where n is the number of items in the new structure. After a global rebuild, there must be at least n/2 deletions before the next global rebuild. Thus, the total amortized time for each deletion is O(P (n)/n) plus the cost of insertion, which is O(P (n) log n/n).
There is one minor technical point to consider here. Our earlier amortized analysis of insertions relied on the fact that large local rebuilds are always far apart. Global rebuilding destroys that assumption. In particular, suppose M has 2k^ − 1 elements and G has 2k−^1 − 1 elements, and we perform four operations: insert, delete, delete, insert.
We can achieve the same deletion time in the worst case by performing the global rebuild lazily. Now we maintain three structures: a static structure S, the main structure M , and the ghost structure G. Most of the time, we insert new items into M , delete items by inserting them into G, and evaluate queries by computing Q(x, S) Q(x, M ) Q(x, G). However, when 2|G| > |S| + |M |, we freeze M and G and start building three new structures S′, M ′, and G′. Initially, M ′^ and G′^ are empty. Newly inserted items go into the new main structure M ′; newly deleted items go into the new ghost structure G′. To answer a query, we compute Q(x, S) Q(x, M ) Q(x, M ′) Q(x, G) Q(x, G′). After every deletion (i.e., insertion into G′), we spend 8P (n)/n time building the new static structure S′^ from the set (S ∪ M ) \ G. After n/8 deletions, the new static structure is complete; we destroy the old structures S, M, G, and revert back to our normal state of affairs. The exact constant 8 is unimportant, it only needs to be large enough that S′^ is complete before the start of the next global rebuild.
With lazy global rebuilding the worst-case time for a deletion is O(P (n) log n/n), exactly the same as insertion.
being copied) or the new structures S′^ and S¯′^ (which are not completely constructed), all updates are inserted into the update queue U. S′^ and S¯′^ are incrementally constructed in two phases. In the first phase, we build new data structures containing the elements of S¯. In the second phase, we actually execute the stream of insertions and deletions in U , in both S′^ and S¯′, in the order they were inserted into U. In both phases, we spend O(I(n)) steps on the construction for each insertion, and O(P (n)/n + D(n)) steps for each deletion, where the hidden constants are chosen large enough to guarantee that each global rebuild is complete well before the next rebuild is triggered. In particular, in the second rebuild phase, each time an update is inserted into U , we process and remove at least two updates from U. When the update queue is empty, S′^ and S¯′^ are complete, so we destroy the old structures S and S¯ and revert to ‘normal’ operation.
1.4.2 Adding Insertions to a Weak-Deletion-Only Structure
Now suppose our given data structure only support weak deletions in D(n) time. To add support for insertions, we modify the lazy logarithmic method. As before, our main structure consists of lg n levels, but now each level consists of eight base structures Oldi, Olderi, Oldesti, Oldi, Olderi, Oldesti, Newi, Newi, as well as an deletion queue Di. We also maintain an auxiliary dictionary recording the level(s) containing each item in the overall structure. As the names suggest, each active structure Old∼i is a shadow copy of the corresponding active structure Old∼i. Queries are answered by examining the active old structures. Newi and its shadow copy are incrementally constructed from the shadows Olderi− 1 and Oldesti− 1 and from the deletion queue Di. Deletions are performed directly in the active old structures and in the shadows that are not involved in rebuilds, and are inserted into deletion queues at levels that are being rebuilt. At each insertion, if level i is being rebuilt, we spend O(P (2i)/ 2 i) time on that local rebuilding. Similarly, for each deletion, if the appropriate level i is being rebuilt, we spend O(D(2i)) time on that local rebuilding. The constants in these time bounds are chosen so that each local rebuild finishes well before the next one begins.
Here are the insertion and deletion algorithms in more detail:
Age(i): if Oldesti = ∅ Oldesti ← New (^) i; Oldesti ← New (^) i else if Older (^) i = ∅ Older (^) i ← New (^) i; Older (^) i ← New (^) i else Old (^) i ← New (^) i; Old (^) i ← New (^) i New (^) i ← ∅; New (^) i ← ∅
LazyInsert(x): for i ← ` − 1 down to 1 if Oldesti− 1 6 = ∅ and Older (^) i− 1 6 = ∅ spend O(P (2i)/ 2 i) time building New (^) i and New (^) i from Oldesti− 1 ∪ Older (^) i− 1 if New (^) i and New (^) i are complete destroy Oldesti− 1 , Oldesti− 1 , Older (^) i− 1 , and Older (^) i− 1 Oldesti− 1 ← Old (^) i− 1 ; Old (^) i− 1 ← ∅; Oldesti− 1 ← Old (^) i− 1 ; Old (^) i− 1 ← ∅ else if Di 6 = ∅ spend O(P (2i)/ 2 i) time processing deletions in Di from New (^) i and New (^) i if Di = ∅ Age(i) New 0 ← {x}; New 0 ← {x} Age(0) WeakDelete(x): find level i containing x if x ∈ Oldesti Delete(x, Oldesti) if Olderi 6 = ∅ Add x to Di+ Spend O(D(2i+1)) time building New (^) i+1 and New (^) i+ else Delete(x, Oldesti) else if x ∈ Olderi Delete(x, Olderi) Add x to Di+ Spend O(D(2i+1)) time building New (^) i+1 and New (^) i+ else if x ∈ Oldi Delete(x, Oldi); Delete(x, Oldi)
1.4.3 The Punch Line
Putting both of these constructions together, we obtain the following worst-case bounds:
[1] J. L. Bentley and J. B. Saxe. Decomposable searching problems I: Static-to-dynamic transformation. J. Algorithms 1(4):301–358, 1980. [2] M. H. Overmars and J. van Leeuwen. Worst-case optimal insertion and deletion methods for decom- posable searching problems. Inform. Process. Lett. 12:168–173, 1981.
*Starred authors were PhD students at the time that the work was published.