Priority Queues-Datastructues and Algorithms-Solutions, Exercises of Data Structures and Algorithms

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: Priority, Queues, Heaps, Element, Inserted, Height, Node, Binary, Tree, Inductive, Hypothesis

Typology: Exercises

2011/2012

Uploaded on 07/16/2012

sangodkar
sangodkar 🇵🇰

5

(2)

12 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures & Algorithm Analysis in C
(second edition)
MarkAllenWeiss
FloridaInternationalUniversity
SolutionsManual
By:
Muhammad Uzair
05-E-147
U.E.T. Lahore
Pakistan
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Priority Queues-Datastructues and Algorithms-Solutions and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Data Structures & Algorithm Analysis in C

(second edition)

MarkAllenWeiss

FloridaInternationalUniversity

SolutionsManual

By:

Muhammad Uzair

05-E-

U.E.T. Lahore

Pakistan

[email protected]

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.

Chapter 6: Priority Queues (Heaps)

6.1 Yes. When an element is inserted, we compare it to the current minimum and change the minimum if the new element is smaller. DeleteMin O operations are expensive in this scheme.

6.3 The result of three DeleteMins, O starting with both of the heaps in Exercise 6.2, is as fol- lows:

6.5 These are simple modifications to the code presented in the text and meant as programming exercises.

6.6 225. To see this, start with i O=1 and position at the root. Follow the path toward the last node, doubling i O when taking a left child, and doubling i O and adding one when taking a right child.

6.7 (a) We show that H O( N O), which is the sum of the heights of nodes in a complete binary tree of N O nodes, is N O − b O( N O), where b O( N O) is the number of ones in the binary representation of N O. Observe that for N O = 0 and N O = 1, the claim is true. Assume that it is true for values of k O up to and including N O−1. Suppose the left and right subtrees have L O and R O nodes, respectively. Since the root has height OIlog N OK, we have H O( N O) = OIlog N OK + H O( L O) + H O( R O) = OIlog N OK + L O − b O( L O) + R O − b O( R O) = N O − 1 + (OIlog N OK − b O( L O) − b O( R O)) The second line follows from the inductive hypothesis, and the third follows because L O + R O = N O − 1. Now the last node in the tree is in either the left subtree or the right sub- tree. If it is in the left subtree, then the right subtree is a perfect tree, and b O( R O) = OIlog N OK − 1. Further, the binary representation of N O and L O are identical, with the exception that the leading 10 in N O becomes 1 in L O. (For instance, if N O = 37 = 10 0101, L O = 1 0101.) It is clear that the second digit of N O must be zero if the last node is in the left sub- tree. Thus in this case, b O( L O) = b O( N O), and H O( N O) = N O − b O( N O)

If the last node is in the right subtree, then b O( L O) = OIlog N OK. The binary representation of R O is identical to N O, except that the leading 1 is not present. (For instance, if N O = 27 = 1 01011, L O = 01011.) Thus b O( R O) = b O( N O) − 1, and again H O( N O) = N O − b O( N O)

(b) Run a single-elimination tournament among eight elements. This requires seven com- parisons and generates ordering information indicated by the binomial tree shown here.

a

c b

d

e

g f

h

The eighth comparison is between b O and c O. If c O is less than b O, then b O is made a child of c O. Otherwise, both c O and d O are made children of b O. (c) A recursive strategy is used. Assume that N O = 2 k O. A binomial tree is built for the N O elements as in part (b). The largest subtree of the root is then recursively converted into a binary heap of 2 k O−^1 elements. The last element in the heap (which is the only one on an extra level) is then inserted into the binomial queue consisting of the remaining binomial trees, thus forming another binomial tree of 2 k O−^1 elements. At that point, the root has a sub- tree that is a heap of 2 k O−^1 − 1 elements and another subtree that is a binomial tree of 2 k O−^1 elements. Recursively convert that subtree into a heap; now the whole structure is a binary heap. The running time for N O = 2 k O^ satisfies T O( N O) = 2 T O( N O / 2) + log N O. The base case is T O(8) = 8.

≤ 1 + log ( k O / 2) P j O= 1

k O− 1 p P j O, k O

≤ 1 + log ( k O / 2)

≤ log k O

completing the proof.

It can also be shown that asymptotically, E O( Dk O) ∼∼ log ( k O−1) − 0..

6.9 (a) Perform a preorder traversal of the heap.

(b) Works for leftist and skew heaps. The running time is O O( Kd O) for d O-heaps.

6.11 Simulations show that the linear time algorithm is the faster, not only on worst-case inputs, but also on random data.

6.12 (a) If the heap is organized as a (min) heap, then starting at the hole at the root, find a path down to a leaf by taking the minimum child. The requires roughly log N O comparisons. To find the correct place where to move the hole, perform a binary search on the log N O ele- ments. This takes O O(log log N O) comparisons. (b) Find a path of minimum children, stopping after log N O − log log N O levels. At this point, it is easy to determine if the hole should be placed above or below the stopping point. If it goes below, then continue finding the path, but perform the binary search on only the last log log N O elements on the path, for a total of log N O + log log log N O comparisons. Other- wise, perform a binary search on the first log N O − log log N O elements. The binary search takes at most log log N O comparisons, and the path finding took only log N O − log log N O, so the total in this case is log N O. So the worst case is the first case. (c) The bound can be improved to log N O + log *N O + O O(1), where log *N O is the inverse Ack- erman function (see Chapter 8). This bound can be found in reference [16].

6.13 The parent is at position OI( i O + d O − 2) /d OK. The children are in positions ( i O − 1) d O + 2, ..., id O + 1.

6.14 (a) O O(( M O + dN O)log d O N O).

(b) O O(( M O + N O)log N O). (c) O O( M O + N O^2 ). (d) d O= max (2, M O / N O). (See the related discussion at the end of Section 11.4.)

6.18 This theorem is true, and the proof is very much along the same lines as Exercise 4.17.

6.19 If elements are inserted in decreasing order, a leftist heap consisting of a chain of left chil- dren is formed. This is the best because the right path length is minimized.

6.20 (a) If a DecreaseKey O is performed on a node that is very deep (very left), the time to per- colate up would be prohibitive. Thus the obvious solution doesn’t work. However, we can still do the operation efficiently by a combination of Delete O and Insert O. To Delete O an arbi- trary node x O in the heap, replace x O by the Merge O of its left and right subheaps. This might create an imbalance for nodes on the path from x O’s parent to the root that would need to be fixed by a child swap. However, it is easy to show that at most log N O nodes can be affected, preserving the time bound. This is discussed in Chapter 11.

6.21 Lazy deletion in leftist heaps is discussed in the paper by Cheriton and Tarjan [9]. The gen- eral idea is that if the root is marked deleted, then a preorder traversal of the heap is formed, and the frontier of marked nodes is removed, leaving a collection of heaps. These can be merged two at a time by placing all the heaps on a queue, removing two, merging them, and placing the result at the end of the queue, terminating when only one heap remains.

6.22 (a) The standard way to do this is to divide the work into passes. A new pass begins when the first element reappears in a heap that is dequeued. The first pass takes roughly

thus had ( (^) dk^ ) nodes at depth d O. The attached tree had ( (^) d O^ k −^ 1 ) nodes at depth d O−1, which are now at depth d O. Adding these two terms and using a well-known formula estab- lishes the theorem.

6.30 This is established in Chapter 11.

6.31 The algorithm is to do nothing special − merely Insert O them. This is proved in Chapter 11.

6.35 Don’t keep the key values in the heap, but keep only the difference between the value of the key in a node and the value of the key in its parent.

6.36 O O( N O + k Olog N O) is a better bound than O O( N Olog k O). The first bound is O O( N O) if k O = O O( N O / log N O). The second bound is more than this as soon as k O grows faster than a constant. For the other values Ω( N O / log N O) = k O = ο( N O), the first bound is better. When k O = Θ( N O), the bounds are identical.