You are on page 1of 1

Algorithms

Exercise Sheet 1

Hilary 2018

1. Recall the k-bit binary counter from the first lecture.


(a) Explain how to implement Decrement. Show that whatever the initial value of the
counter there is a sequence of Increments and Decrements for which the average cost per
operation is Ω(k).
(b) Explain how to implement an operation Reset that resets the counter to zero. Your
implementation should be such that starting from a zero counter Increment and Reset
both have O(1) amortised cost.

2. Show how to implement a queue with two stacks so that the amortised cost of each Enqueue
and Dequeue operation is O(1).

3. Assuming link-by-rank and path compression, draw the data structure that results from
executing the following program:

1 for i ← 1 to 16
2 do Make-Set(xi )
3 for i ← 1 to 15 by 2
4 do Union(xi , xi+1 )
5 for i ← 1 to 13 by 4
6 do Union(xi , xi+2 )
7 Union(x1 , x5 )
8 Union(x11 , x13 )
9 Union(x1 , x10 )
10 Find-Set(x2 )
11 Find-Set(x9 )

65536 )
4. If n = 65536(65536 , what is log∗ n?

5. Suppose that we implement disjoint-set forests with link-by-rank, but not path compression.
Show that a sequence of m Union and Find-Set operations, starting from n singletons can
take time Ω(m log n).

You might also like