Docsity

Amortized Analysis of Data Structures: Dynamic Tables, Scapegoat Trees, and Splay Trees, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Chapter VIII
Special Data Structures
VIII.1 Amortized Analysis
Often a data structure is used in an algorithm and accordingly one is not interested in the
worst-case time needed to perform a certain operation; it is sufficient if the performance is
good when all the operations are considered. For the purpose of analysis it is then conve-
nient to assign an “average” cost (running time) to each operation, over all the operations
performed by the algorithm. We speak of amortized cost of an operation. We can think
of this as “charging” extra to inexpensive operations and storing this charge as “credit”
that can be used later to “pay” for expensive operations, which can then be charged a low
“amortized” cost. One way to perform this amortized analysis is through the use of a po-
tential function, which simply keeps track of the credit accumulated in the data structure.
More precisely, we want a potential function Φ that associates a potential Φ(D) to a data
structure D, so that for the i-th operation that transforms Diinto Di+1, the actual cost ci
of the operation and the amortized cost ˆciare related by
ˆci=ci+K(Φ(Di+1)−Φ(Di)),
where K > 0 is an appropriate constant that relates potential and cost units. Adding over
all the operations, we get
m
X
i=1
ˆci=
m
X
i=1
ci+K
m
X
i=1
(Φ(Di+1)−Φ(Di)) =
m
X
i=1
ci+K(Φ(Dm+1)−Φ(D1)).
If we have Φ(Dm+1)−Φ(D1)≥0, which is achieved, for example, when Φ(D1) = 0 and
Φ(Di)≥0, then m
X
i=1
ˆci≥
m
X
i=1
ci.
So the total amortized cost Pm
i=1 ˆciis an upper bound for the actual cost Pm
i=1 ci.
VIII.2 Dynamic Tables
We consider the following situation: we want to maintain a table under insertions and
deletions so that the occupancy – ratio of number of items stored to actual size – is larger
than a fixed fraction and, of course, at most 1. We assume that changing the size of the
VIII.1
pf3
pf4
pf5

Partial preview of the text

Download Amortized Analysis of Data Structures: Dynamic Tables, Scapegoat Trees, and Splay Trees and more Study notes Algorithms and Programming in PDF only on Docsity!

Chapter VIII

Special Data Structures

VIII.1 Amortized Analysis

Often a data structure is used in an algorithm and accordingly one is not interested in the worst-case time needed to perform a certain operation; it is sufficient if the performance is good when all the operations are considered. For the purpose of analysis it is then conve- nient to assign an “average” cost (running time) to each operation, over all the operations performed by the algorithm. We speak of amortized cost of an operation. We can think of this as “charging” extra to inexpensive operations and storing this charge as “credit” that can be used later to “pay” for expensive operations, which can then be charged a low “amortized” cost. One way to perform this amortized analysis is through the use of a po- tential function, which simply keeps track of the credit accumulated in the data structure. More precisely, we want a potential function Φ that associates a potential Φ(D) to a data structure D, so that for the i-th operation that transforms Di into Di+1, the actual cost ci of the operation and the amortized cost ˆci are related by

ˆci = ci + K(Φ(Di+1) − Φ(Di)),

where K > 0 is an appropriate constant that relates potential and cost units. Adding over all the operations, we get

∑^ m

i=

ˆci =

∑^ m

i=

ci + K

∑^ m

i=

(Φ(Di+1) − Φ(Di)) =

∑^ m

i=

ci + K(Φ(Dm+1) − Φ(D 1 )).

If we have Φ(Dm+1) − Φ(D 1 ) ≥ 0, which is achieved, for example, when Φ(D 1 ) = 0 and Φ(Di) ≥ 0, then ∑m

i=

c ˆi ≥

∑^ m

i=

ci.

So the total amortized cost

∑m i=1 ˆci^ is an upper bound for the actual cost^

∑m i=1 ci.

VIII.2 Dynamic Tables

We consider the following situation: we want to maintain a table under insertions and deletions so that the occupancy – ratio of number of items stored to actual size – is larger than a fixed fraction and, of course, at most 1. We assume that changing the size of the

table implies a complete rebuilding which has a cost that is linear in the size of the new table. We want to show that insertions and deletions can be supported in amortized time O(1), despite the fact that rebuilding the table is expensive. Let size(T ) and n(T ) denote the size and the number of elements stored in T.

The insert and delete operations are implemented as follows:

Insert(x): If n(T ) < size(T ) then store x in T , otherwise allocate a new table of twice the size (or 1 if size was 0), and store all old elements and x in the new table.

Delete(x): If n(T ) > size(T )/4 then remove x from T , otherwise allocate a new table of half the size and store all old elements except x in the new table.

The factor 1/4 is used as occupancy thereshold in a deletion (rather than 1/2) to avoid the possibility that a bad sequence of insertions and deletions force almost consecutive doublings and halvings of the table size, which could not result in an amortized constant time.

For the amortized analysis, we use the potantial function:

Φ(T ) =

∣n(T^ )^ −^

size(T ) 2

Let’s see that the relation ˆc = c+K∆Φ holds for both insertion and deletion with ˆc = O(1):

Insertion: If the table is not rebuilt then certainly the relation holds (note that ∆Φ is either +1 or −1 depending on whether n(T ) > size(T )/2 or not). If the table is rebuilt then the potential changes from size(T )/2 to 1; so since the rebuilding takes time O(size(T )), the relation O(1) = O(size(T )) + K(1 − size(T )) holds. Deletion: Again, if the table is not rebuilt then the relation holds. Otherwise, the potential changes from size(T )/4 to 1 and again the rebuilding takes time O(size(T )), and the relation holds.

To summarize, we may say that the amortized cost of either inserting or deleting in the dynamic table is O(1).

VIII.3 Scapegoat Trees

A scapegoat tree T is a binary search tree that handles deletions and insertions lazily: deletions and rebalancing after insertions are delayed. Let size(T ) be the number of nodes in T , height(T ) be its height, and n(T ) be the number of keys stored in T. Also let D(T ) be a counter that keeps the number of deleted keys. So n(T ) = size(T ) − D(T ) ≤ size(T ). Insertions and deletions enforce:

height(T ) ≤ α · log(size(T )) and size(T ) ≤ 2 n(T )

so that searching in the tree takes time O(log n(T )). For a node v, let depth(v) denote the depth of v in T , and size(v) and height(v) denote the size (number of nodes) and height of the subtree of T rooted at v. Also let α > 1 be a constant (its choice will become clear).

In the following description, rebuilding a tree means constructing a tree with the same keys and with the best possible balance (smallest height). This can be done in linear time. The operations are supported as follows:

We also know that size(v) = size(left(v)) + size(right(v)) + 1. If α > 1 / log 2 then

I(v) = size(left(v)) − size(right(v)) = 2size(left(v)) − size(v) + 1 ≥ (2/e^1 /α^ − 1)size(v) + 1 = Ω(size(v)).

Since each insertion whose search path goes through v increments I(v) by at most 1, we can “build up” an amount of credit I(v) by charging 1 unit of credit to each insertion path through v. The built-up credit can then be used for reconstruction when v is identified as a scapegoat.

Why is it sufficient just to rebuild the subtree rooted at the scapegoat v? After rebuilding this subtree, its depth decreases at least by one, therefore height(T ) decreases (recall we noted that the key x inserted was the only one at the deepest level) and this compensates for the increase that made height(T ) > α log(size(T )) and triggered the rebuild. Since height(T ) ≤ α log(size(T )) hold before the insertion and size(T ) has increased by 1, then the invariant holds again after the update.

When rebuilding, do we keep the nodes marked deleted? We may do either way. If we keep them, the rebuilding cost is paid by the unbalance potential, while the delete potential is still “stored.”

VIII.4 Splay Trees

A splay tree is a binary tree that achieves O(log n) amortized access times by the use of a restructuring heuristic every time that a node is accessed. Unlike other balanced trees, no explicit information is stored in a node (like subtree size). The heuristic is to “splay” the accessed node x up to the root via rotations. Interestingly, the natural approach of rotating always around the parent of x until x is brought to the root does not result in an efficient access time. Rather, the rotations are performed in pairs, which we call double rotations, except possibly the last one which is a single rotation.

A B

C

x

p

A

B C

p

x

Figure VIII.1: Single rotation

Let p = parent(x) and g = parent(parent(x)). There are two types of double rotations depending on whether x and p are both left or both right children – the zig-zig case – or one is left and the other right child – the zig-zag case. In the ziz-zig case, we first rotate around g and then rotate around p, in the zig-zag case, we first rotate around p and then

around g (this is the “natural” sequence). The figures below illustrate the left-left and right-left cases, other cases are symmetric to these.

x

g p

A B

C

D x

A B C D

p g A g B

C D

p

x

Figure VIII.2: Zig-zig case: first rotate around g and then around p.

p

x A B C

D

g g

A B

C

D p

x

A B C D

g

x p

Figure VIII.3: Zig-zag: first rotate around p and then around g.

To summarize, splaying a node x consists in applying to x double rotations until it is made the root of the tree, or it becomes a child of the root and a single rotation makes it the root.

Operations

The search, insert and delete operations are implemented as follows:

Search(x): search for x, then splay x or, if not found, its predecessor or successor (whichever is found).

Insert(x): insert x as usual, then splay it.

Delete(x): search for x, then splay it and remove it leaving left and right subtrees T and T ′; if T is not empty then find and splay the maximum key y in it and make T ′^ the right child of y.

The cost of each of these operations is dominated by the time of splaying. Thus, it suffices to analize the cost of splaying a node.

Analysis

We now verify that the amortized cost of splaying is O(log n). In fact, something stronger is true: if w(x) is a weight or access frequency of key x, then its access time is O(log(W/w(x))) where W =

x w(x). This is achieved without a explicit knowledge of these weights!

The cost of a splaying is proportional to the number of rotations performed, so let us account the number of rotations. The amortized number of rotations is equal to the actual number plus the change of potential. Since the actual number of rotations is depth(x), then the amortized number is at most

depth(x, Ti) + 3(rf (x) − ri(x)) − depth(x, Ti) + 1 = 3(rf (x) − ri(x)) + 1.

Using twf (x) = W (since x is the root of Tf ) and twi(x) ≥ w(x) (since the subtree rooted at x includes x), the amortized number of permutations in splaying x is bounded by

3(log W − log w(x)) + 1 = 3 log(W/w(x)) + 1 = O(log(W/w(x)).

From the discussion above, each of the operations – search, insert and delete – have amor- tized running time O(log(W/w(x))) (actually O(log(W/w(min(pred(x), succ(x))))) for an unsuccessful search).

Single rotations do not work. We point point out that performing single rotations up the tree does not result in efficient amortized access time. Consider the tree on the left in the figure below. It can be created by splay tree operations that insert x, p, g, h, i, j and k in order. Each of these insert operations take O(1) time. Note how unbalanced is the resulting tree. Now search for these keys in the same order. The figures on the center and right illustrate the result of splaying x and p. If the sequence has n keys, then the cost of the i-th search is proportional to (n − i) and the total cost is Θ(n^2 ). So the amortized cost is Θ(n). This illustrates that single rotations do not suffice.

x

g p

h

i

j

k

g p

h

i

j

k

x

g

h

i

j

x k

p

Figure VIII.4: Single rotations up the tree do not work.

References

A. Anderson. General balanced trees. J. Algorithms 30 (1999) 1–28. I. Galperin and R.L. Rivest. Scapegoat trees. Proc. SODA 1993, 165–174. D. D. Sleator and R. E. Tarjan. Self-adjusting binary search trees. J. ACM 32 (1985) 652–686.

R. E. Tarjan. Data Structures and Network Algorithms. SIAM 1983.