



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is solution manual provided by Muhammad Uzair at University of Engineering and Technology Lahore. It related to Data Structures and Algorithms course. Its main points are: Amortized, Analysis, Insertions, Heap, Order, Violation, Merge, Potential, Splay, Tree
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




By:
Muhammad Uzair
05-E-
U.E.T. Lahore
Pakistan
Preface
Included in this manual are answers to most of the exercises in the textbook Data Structures and Algorithm Analysis in C, second edition, published by Addison-Wesley. These answers reflect the state of the book in the first printing.
Specifically omitted are likely programming assignments and any question whose solu- tion is pointed to by a reference at the end of the chapter. Solutions vary in degree of complete- ness; generally, minor details are left to the reader. For clarity, programs are meant to be pseudo-C rather than completely perfect code.
Errors can be reported to [email protected]. Thanks to Grigori Schwarz and Brian Harvey for pointing out errors in previous incarnations of this manual.
11.1 When the number of trees after the insertions is more than the number before.
11.2 Although each insertion takes roughly log N O, and each DeleteMin O takes 2log N O actual time, our accounting system is charging these particular operations as 2 for the insertion and 3log N O−2 for the DeleteMin. O The total time is still the same; this is an accounting gimmick. If the number of insertions and DeleteMins O are roughly equivalent, then it really is just a gimmick and not very meaningful; the bound has more significance if, for instance, there are N O insertions and O O( N O / log N O) DeleteMins O (in which case, the total time is linear).
11.3 Insert the sequence N O, N O + 1, N O − 1, N O + 2, N O − 2, N O + 3, ..., 1, 2 N O into an initially empty skew heap. The right path has N O nodes, so any operation could take Ω( N O) time.
11.5 We implement DecreaseKey(X, H) as follows: If lowering the value of X O creates a heap order violation, then cut X O from its parent, which creates a new skew heap H O 1 with the new value of X O as a root, and also makes the old skew heap H O smaller. This operation might also increase the potential of H O, but only by at most log N O. We now merge H O and H O 1. The total amortized time of the Merge O is O O(log N O), so the total time of the DecreaseKey O operation is O O(log N O).
11.8 For the zig O− zig O case, the actual cost is 2, and the potential change is R P f OO( X O ) + R P f OO( P O ) + R P f OO( G O ) − Ri O( X O ) − Ri O( P O ) − Ri O( G O ). This gives an amortized time bound of ATzig O− zig O = 2 + R P f OO( X O ) + R P f OO( P O ) + R P f O O( G O ) − Ri O( X O ) − Ri O( P O ) − Ri O( G O ) Since R P f OO( X O ) = Ri O( G O ), this reduces to = 2 + R P f OO( P O ) + R P f OO( G O ) − Ri O( X O ) − Ri O( P O )
Also, R P f OO( X O ) > R P f OO( P O ) and Ri O( X O ) < Ri O( P O ), so ATzig O− zig O < 2 + R P f OO( X O ) + R P f OO( G O ) − 2 Ri O( X O )
Since S (^) i O( X O ) + S P f O O( G O ) < S P f OO( X O ), it follows that Ri O( X O ) + R P f OO( G O ) < 2 R P f OO( X O ) − 2. Thus ATzig O− zig O < 3 R P f O O( X O ) − 3 Ri O( X O )
11.9 (a) Choose W O( i O) = 1 / N O for each item. Then for any access of node X O, R P f OO( X O ) = 0, and Ri O( X O ) ≥ −log N O, so the amortized access for each item is at most 3 log N O + 1, and the net potential drop over the sequence is at most N O log N O, giving a bound of O O( M Olog N O + M O + N Olog N O), as claimed. (b) Assign a weight of qi O /M O to items i O. Then R P f OO( X O ) = 0, Ri O( X O ) ≥ log( qi O /M O), so the amortized cost of accessing item i O is at most 3 log( M O /q (^) i O) + 1, and the theorem follows immediately.
11.10 (a) To merge two splay trees T O 1 and T O 2 , we access each node in the smaller tree and insert it into the larger tree. Each time a node is accessed, it joins a tree that is at least
twice as large; thus a node can be inserted log N O times. This tells us that in any sequence of N O−1 merges, there are at most N Olog N O inserts, giving a time bound of O O( N Olog^2 N O). This presumes that we keep track of the tree sizes. Philosophically, this is ugly since it defeats the purpose of self-adjustment. (b) Port and Moffet [6] suggest the following algorithm: If T O 2 is the smaller tree, insert its root into T O 1. Then recursively merge the left subtrees of T O 1 and T O 2 , and recursively merge their right subtrees. This algorithm is not analyzed; a variant in which the median of T O 2 is splayed to the root first is with a claim of O O( N Olog N O) for the sequence of merges.
11.11 The potential function is c O times the number of insertions since the last rehashing step, where c O is a constant. For an insertion that doesn’t require rehashing, the actual time is 1, and the potential increases by c O, for a cost of 1 + c O. If an insertion causes a table to be rehashed from size S O to 2 S O, then the actual cost is 1 + dS O, where dS O represents the cost of initializing the new table and copying the old table back. A table that is rehashed when it reaches size S O was last rehashed at size S O / 2, so S O / 2 insertions had taken place prior to the rehash, and the initial potential was cS O / 2. The new potential is 0, so the potential change is − cS O / 2, giving an amortized bound of ( d O − c O / 2) S O + 1. We choose c O = 2 d O, and obtain an O O(1) amortized bound in both cases.
11.12 We show that the amortized number of node splits is 1 per insertion. The potential func- tion is the number of three-child nodes in T O. If the actual number of nodes splits for an insertion is s O, then the change in the potential function is at most 1 − s O, because each split converts a three-child node to two two-child nodes, but the parent of the last node split gains a third child (unless it is the root). Thus an insertion costs 1 node split, amor- tized. An N O node tree has N O units of potential that might be converted to actual time, so the total cost is O O( M O + N O). (If we start from an initially empty tree, then the bound is O O( M O).)
11.13 (a) This problem is similar to Exercise 3.22. The first four operations are easy to imple- ment by placing two stacks, SL O and SR O, next to each other (with bottoms touching). We can implement the fifth operation by using two more stacks, ML O and MR O (which hold minimums). If both SL O and S (^) R O never empty, then the operations can be implemented as follows: Push(X,D): push X O onto SL O; if X O is smaller than or equal to the top of ML O, push X O onto ML O as well. Inject(X,D): same operation as Push O, except use S (^) R O and MR O. Pop(D): pop S (^) L O; if the popped item is equal to the top of ML O, then pop ML O as well. Eject(D): same operation as Pop O, except use S (^) R O and MR O. FindMin(D): return the minimum of the top of ML O and MR O. These operations don’t work if either S (^) L O or SR O is empty. If a Pop O or E P ject O is attempted on an empty stack, then we clear ML O and MR O. We then redistribute the elements so that half are in SL O and the rest in S (^) R O, and adjust ML O and MR O to reflect what the state would be. We can then perform the Pop O or E P ject O in the normal fashion. Fig. 11.1 shows a transfor- mation. Define the potential function to be the absolute value of the number of elements in SL O minus the number of elements in S (^) R O. Any operation that doesn’t empty SL O or SR O can