Practice Set 4
Practice Set 4
Instructions
• This problem set is completely optional. There is no need to submit the solutions.
• Solutions are available in “solutions practice set 4.pdf”. You are strongly encouraged to first try
to solve the problems by yourselves and then check the solutions.
• Post on Discord/Canvas if you will face any problem while solving the questions.
Question 1: You are asked to maintain a list L while supporting the following two operations:
• ReplaceSum(L): Compute the sum of all the integers in L. Then remove all these integers, and
finally just store the computed sum in L (cost is length of the list L).
(a) Use the accounting method to show that the amortized cost of both Insert and ReplaceSum
operation is O(1).
(b) Use the potential method to show that the amortized cost of both Insert and ReplaceSum oper-
ation is O(1).
Question 2: Let us consider the following Online List Problem. In this problem we are given a doubly
linked list L containing n items (you may consider them to be distinct), and a sequence of m search
operations s1 , s2 , · · · , sm with the following restrictions:
• Each search operation st will look like: Given an item et you have to correctly say whether et is in
the list L or not.
• The sequence of search operations will arrive in online fashion, i.e., at time t you will get a request
for the operation st and you have to answer correctly. (One important thing to note, you cannot
see future search queries.)
• You can access the linked list only through the HEAD pointer. So if at time1 t you are asked to
search an item et which is the third element in the list at that moment, then you have to pay 3
unit cost to find that element.
• During answering a search query you may update the list. However at any point you can only swap
two neighboring elements in the list, and each such swap will cost you one unit. (So during any
search operation if you will perform k swaps, you will have to pay additional k unit cost.)
Now keeping all these restrictions in mind, ideally we would like to design an algorithm that achieves
minimum total cost.
Ooh, that must be a very difficult task! So to make your life simpler, we are not asking you to design
an algorithm with the minimum cost. Instead we ask you to consider the following simple heuristic
1 Do not get confused between time and cost. Here we use the term time to identify which operation you are going to
serve at that particular moment, whereas the cost captures the complexity of each operation.
1
algorithm: Whenever we search an item in the list, if we find it we bring that in front of the list (by
performing a sequence of swaps).
You may wonder since this is an extremely simple heuristic strategy, its total cost will no way be close
to the minimum. However, we have a different opinion. Suppose if all the m search operations were given
in advance (instead of arriving in online fashion) then the minimum cost that one (optimum algorithm)
could achieve is Copt . We claim that the total cost of the above heuristic algorithm is just at most 4Copt .
If you answer the following series of questions, you will also be able to prove this claim.
(a) During the search operation st suppose you are asked to search for the item et . Let r and r∗ be the
rank of et in the list maintained by the heuristic algorithm (denoted by L) and the optimum algorithm
(denoted by L∗ ) respectively. Further suppose the optimum algorithm performs t∗ swaps during the
search operation st . What is the difference between the cost to perform st by the heuristic and the
optimum algorithm?
(b) Let us define the distance between two lists L and L0 containing exactly the same items (but in
different order) as number of pairs (x, y) such that relative ordering of x and y in L and L0 are different.
For example, if L = 10, 20, 30, 40 and L0 = 10, 30, 40, 20, then the distance between them is 2 (since
(20, 30), (20, 40) are the pairs with different relative ordering in L and L0 ). Show that φ(t), defined as
the twice the distance between L and L∗ at time t, is a valid potential function.
(c) Show that during performing st by the heuristic algorithm, each swap changes the distance between
L and L∗ by at most one.
(d) Next prove that the change in potential from t-th time to (t+1)-th time is at most 4r∗ +2t∗ −2r−2.
(e) Now complete the amortize analysis using the above potential function to show that the total cost
of the above mentioned heuristic algorithm is at most 4Copt .
Question 3: In the Set Union problem we have n elements, that each are initially in n singleton sets,
and we want to support the following operations:
• Union(A, B): Merge the two sets A and B into one new set C = A ∪ B destroying the old sets.
• SameSet(x, y): Return true, if x and y are in the same set, and false otherwise.
We implement it in the following way. Initially, give each set a distinct color. When merging two sets,
recolor the smaller (in size) one with the color of the larger one (break ties arbitrarily). Note, to recolor a
set you have to recolor all the elements in that set. To answer SameSet queries, check if the two elements
have the same color. (Assume that you can check the color an element in O(1) time, and to recolor an
element you also need O(1) time. Further assume that you can know the size of a set in O(1) time.)
Use Aggregate method to show that the amortized cost is O(log n) for Union. That means, show that
any sequence of m union operations takes O(m log n) time. (Note, we start with n singleton sets.)
Question 4: Suppose Alice insists Bob to maintain a dynamic table (that supports both insertion and
deletion) in such a way its size must always be a Fibonacci number. She insists on the following variant
of the rebuilding strategy. Let Fk denote the k-th Fibonacci number. Suppose the current table size is
Fk .
After an insertion, if the number of items in the table is Fk−1 , allocate a new table of size Fk+1 , move
everything into the new table, and then free the old table. After a deletion, if the number of items in the
table is Fk−3 , we allocate a new hash table of size Fk−1 , move everything into the new table, and then
free the old table.
Use either Potential method or Accounting method to show that for any sequence of insertions and
deletions, the amortized cost per operation is still O(1). (If you use Potential method clearly state your
potential function. If you use Accounting method clearly state your charging scheme.)