Docsity

Static to Dynamic Transformation - Lecture Notes | CS 573, Study notes of Algorithms and Programming

Toggle sidebar
bg1
CS 573 Lecture 1: Static-to-Dynamic Transformations Spring 2006
1 Static-to-Dynamic Transformations (January 17 and 19)
In this lecture, we’ll consider data structures for decomposable search problems. A search problem
asks us to store a set Aof 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 Aand B, the answer to a query over A∪Bcan 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=qfor all q. Examples of decomposable search
problems include the following.
•Items are points in the plane; a query asks for the number of points in a given query rectangle.
Here, = + and ⊥= 0.
•Items are points in the plane; a query asks for the minimum distance from a data point to a
given query point. Here, = min and ⊥=∞.
•Items are points in the plane; a query asks whether any point lies in a given query triangle.
Here, =∨and ⊥=False.
•Items are intervals on the real line; a query asks for the number of intervals that contain a
given query point.
1.1 Insertions Only (Bentley and Saxe* [1])
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 nitems 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 S0(n) = O(S(n)),
preprocessing time P0(n) = O(P(n)), query time Q0(n) = O(log n)·Q(n), and amortized insertion
time I0(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 nclevels L0, L1, . . . , L`−1. Each level Liis either empty
or a static data structure storing exactly 2iitems. 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 Li6=∅
q←qQuery(x,Li)
return q
The total query time is clearly at most P`−1
i=0 Q(2i)< `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)).
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Static to Dynamic Transformation - Lecture Notes | CS 573 and more Study notes Algorithms and Programming in PDF only on Docsity!

1 Static-to-Dynamic Transformations (January 17 and 19)

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.

  • Items are points in the plane; a query asks for the number of points in a given query rectangle. Here,  = + and ⊥ = 0.
  • Items are points in the plane; a query asks for the minimum distance from a data point to a given query point. Here,  = min and ⊥ = ∞.
  • Items are points in the plane; a query asks whether any point lies in a given query triangle. Here,  = ∨ and ⊥ = False.
  • Items are intervals on the real line; a query asks for the number of intervals that contain a given query point.

1.1 Insertions Only (Bentley and Saxe* [1])

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

∑`− 1

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

1.2 Lazy Rebuilding (Overmars* and van Leeuwen [2])

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

A B C D E F G H

I J K L

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

I J K L

M N O P

Q R S T

U

U V

W X

Y

Y

Z

A B C D E F G H

I J K L M N

I J K L

M N O P

Q R S T

U V

U V

W X

Y Z

A B C D E F G H

I J K L M N O

I J K L

M N O P

Q R S T

U V W

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

A B C D E F G H

I J K L M N O P

Q

Q R S T

U V W X

Y

Y Z

A B C D E F G H

I J K L M N O P

Q R

Q R S T

U V W X

Y Z

Y Z

A B C D E F G H

I J K L M N O P

Q R S

Q R S T

U V W X

Y Z 0

Y Z

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.

  • The first insertion causes us to rebuild M completely.
  • The first deletion causes us to rebuild G completely.
  • The second deletion triggers a global rebuild. The new M contains 2k−^1 − 1 items.
  • Finally, the second insertion causes us to rebuild M completely. Another way to state the problem is that a global rebuild can put us into a state where we don’t have enough insertion credits to pay for a local rebuild. To solve this problem, we simply scale the amortized cost of deletions by a constant factor. When a global rebuild is triggered, a fraction of this pays for the global rebuild itself, and the rest of the credit pays for the first local rebuild at each level of the new main structure, since ∑lg n i=0 P^ (2i) =^ O(P^ (n)).

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.

M S G

M’ S’ G’

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.

Oldi

Olderi

Oldesti

Oldi

Olderi

Oldesti

Newi

Newi

Di

Oldi- 1

Olderi- 1

Oldesti- 1

Oldi- 1

Olderi- 1

Oldesti- 1

Newi- 1

Newi- 1

Di- 1

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:

  • The entire structure uses O(S(n)) space and can be built in O(P (n)) time.
  • Queries can be answered in time O(Q(n) log n), or O(Q(n)) if Q(n) > nε^ for any ε > 0.
  • Each insertion takes time O(P (n) log n/n), or O(P (n)/n) if P (n) > n1+ε^ for any ε > 0.
  • Each deletion takes time O(P (n)/n + D(n) log n), or O(P (n)/n + D(n)) if D(n) > nε^ for any ε > 0.

References

[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.