Mark Allen Weiss 数据结构与算法分析 课后习题答案6.pdf
《Mark Allen Weiss 数据结构与算法分析 课后习题答案6.pdf》由会员分享,可在线阅读,更多相关《Mark Allen Weiss 数据结构与算法分析 课后习题答案6.pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter6:PriorityQueues(Heaps)6.1Yes.When an element is inserted,we compare it to the current minimum and change theminimum if the new element is smaller.DeleteMin?operations are expensive in thisscheme.6.21326754151412910111381321264815149751113106.3The result of three DeleteMins,?starting with bot
2、h of the heaps in Exercise 6.2,is as fol-lows:4651371081514129114651271081514913116.46.5 These are simple modifications to the code presented in the text and meant as programmingexercises.6.6225.To see this,start with i?=1 and position at the root.Follow the path toward the lastnode,doubling i?when
3、taking a left child,and doubling i?and adding one when taking aright child.-29-6.7(a)We show that H?(N?),which is the sum of the heights of nodes in a complete binary treeof N?nodes,is N?b?(N?),where b?(N?)is the number of ones in the binary representation ofN?.Observe that for N?=0 and N?=1,the cla
4、im is true.Assume that it is true for values ofk?up to and including N?1.Suppose the left and right subtrees have L?and R?nodes,respectively.Since the root has height?log N?,we haveH?(N?)=?log N?+H?(L?)+H?(R?)=?log N?+L?b?(L?)+R?b?(R?)=N?1+(?log N?b?(L?)b?(R?)The second line follows from the inducti
5、ve hypothesis,and the third follows becauseL?+R?=N?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,andb?(R?)=?log N?1.Further,the binary representation of N?and L?are identical,with theexception
6、 that the leading 10 in N?becomes 1 in L?.(For instance,if N?=37=100101,L?=10101.)It is clear that the second digit of N?must be zero if the last node is in the left sub-tree.Thus in this case,b?(L?)=b?(N?),andH?(N?)=N?b?(N?)If the last node is in the right subtree,then b?(L?)=?log N?.The binary rep
7、resentation of R?is identical to N?,except that the leading 1 is not present.(For instance,if N?=27=101011,L?=01011.)Thus b?(R?)=b?(N?)1,and againH?(N?)=N?b?(N?)(b)Run a single-elimination tournament among eight elements.This requires seven com-parisons and generates ordering information indicated b
8、y the binomial tree shown here.abcdefghThe eighth comparison is between b?and c?.If c?is less than b?,then b?is made a child of c?.Otherwise,both c?and d?are made children of b?.(c)A recursive strategy is used.Assume that N?=2k?.A binomial tree is built for the N?elements as in part(b).The largest s
9、ubtree of the root is then recursively converted into abinary heap of 2k?1elements.The last element in the heap(which is the only one on anextra level)is then inserted into the binomial queue consisting of the remaining binomialtrees,thus forming another binomial tree of 2k?1elements.At that point,t
10、he root has a sub-tree that is a heap of 2k?1 1 elements and another subtree that is a binomial tree of 2k?1elements.Recursively convert that subtree into a heap;now the whole structure is a binaryheap.The running time for N?=2k?satisfies T?(N?)=2T?(N?/2)+log N?.The base case isT?(8)=8.-30-6.8Let D?
11、1,D?2,.,Dk?be random variables representing the depth of the smallest,second smal-lest,and k?th?smallest elements,respectively.We are interested in calculating E?(Dk?).Inwhat follows,we assume that the heap size N?is one less than a power of two(that is,thebottom level is completely filled)but suffi
12、ciently large so that terms bounded by O?(1/N?)are negligible.Without loss of generality,we may assume that the k?th?smallest element isin the left subheap of the root.Let p?j?,k?be the probability that this element is the?j?th?smal-lest element in the subheap.Lemma:For k?1,E?(Dk?)=?j?=1k?1p?j?,k?(E
13、?(D?j?)+1).Proof:An element that is at depth d?in the left subheap is at depth d?+1 in the entiresubheap.Since E?(D?j?+1)=E?(D?j?)+1,the theorem follows.Since by assumption,the bottom level of the heap is full,each of second,third,.,k?1th?smallest elements are in the left subheap with probability of
14、 0.5.(Technically,the probabil-ity should be 12 1/(N?1)of being in the right subheap and 12+1/(N?1)of being in theleft,since we have already placed the k?th?smallest in the right.Recall that we have assumedthat terms of size O?(1/N?)can be ignored.)Thusp?j?,k?=pk?j?,k?=2k?21_ _(?j?1k?2)Theorem:E?(Dk
15、?)log k?.Proof:The proof is by induction.The theorem clearly holds for k?=1 and k?=2.We thenshow that it holds for arbitrary k?2 on the assumption that it holds for all smaller k?.Now,by the inductive hypothesis,for any 1?j?k?1,E?(D?j?)+E?(Dk?j?)log?j?+log k?j?Since?f?(x?)=log x?is convex for x?0,lo
16、g?j?+log k?j?2log(k?/2)ThusE?(D?j?)+E?(Dk?j?)log(k?/2)+log(k?/2)Furthermore,since p?j?,k?=pk?j?,k?,p?j?,k?E?(D?j?)+pk?j?,k?E?(Dk?j?)p?j?,k?log(k?/2)+pk?j?,k?log(k?/2)From the lemma,E?(Dk?)=?j?=1k?1p?j?,k?(E?(D?j?)+1)=1+?j?=1k?1p?j?,k?E?(D?j?)ThusE?(Dk?)1+?j?=1k?1p?j?,k?log(k?/2)-31-1+log(k?/2)?j?=1k
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Mark Allen Weiss 数据结构与算法分析 课后习题答案6 数据结构 算法 分析 课后 习题 答案
限制150内