Mark Allen Weiss 数据结构与算法分析 课后习题答案10.pdf
《Mark Allen Weiss 数据结构与算法分析 课后习题答案10.pdf》由会员分享,可在线阅读,更多相关《Mark Allen Weiss 数据结构与算法分析 课后习题答案10.pdf(9页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter10:AlgorithmDesignTechniques10.1First,we show that if N?evenly divides P?,then each of?j?(i?1)P?+1through?jiP?must beplaced as the i?th?job on some processor.Suppose otherwise.Then in the supposedoptimal ordering,we must be able to find some jobs?jx?and?jy?such that?jx?is the t?th?jobon some
2、processor and?jy?is the t?+1th?job on some processor but tx?ty?.Let?jz?be thejob immediately following?jx?.If we swap?jy?and?jz?,it is easy to check that the mean pro-cessing time is unchanged and thus still optimal.But now?jy?follows?jx?,which is impos-sible because we know that the jobs on any pro
3、cessor must be in sorted order from theresults of the one processor case.Let?je?1,?je?2,.,?jeM?be the extra jobs if N?does not evenly divide P?.It is easy to see thatthe processing time for these jobs depends only on how quickly they can be scheduled,and that they must be the last scheduled job on s
4、ome processor.It is easy to see that thefirst M?processors must have jobs?j?(i?1)P?+1through?jiP?+M?;we leave the details to thereader.10.3,4789015spnl3:6210.4One method is to generate code that can be evaluated by a stack machine.The two opera-tions are Push?(the one node tree corresponding to)a sy
5、mbol onto a stack and Combine,?which pops two trees off the stack,merges them,and pushes the result back on.For theexample in the text,the stack instructions are Push(s),Push(nl),Combine,Push(t),Com-bine,Push(a),Combine,Push(e),Combine,Push(i),Push(sp),Combine,Combine.By encoding a Combine?with a 0
6、and a Push?with a 1 followed by the symbol,the totalextra space is 2N?1 bits if all the symbols are of equal length.Generating the stackmachine code can be done with a simple recursive procedure and is left to the reader.10.6Maintain two queues,Q?1and Q?2.Q?1will store single-node trees in sorted or
7、der,and Q?2will store multinode trees in sorted order.Place the initial single-node trees on Q?1,enqueueing the smallest weight tree first.Initially,Q?2is empty.Examine the first twoentries of each of Q?1and Q?2,and dequeue the two smallest.(This requires an easilyimplemented extension to the ADT.)M
8、erge the tree and place the result at the end of Q?2.Continue this step until Q?1is empty and only one tree is left in Q?2.-54-10.9To implement first fit,we keep track of bins bi?,which have more room than any of thelower numbered bins.A theoretically easy way to do this is to maintain a splay treeo
9、rdered by empty space.To insert w?,we find the smallest of these bins,which has atleast w?empty space;after w?is added to the bin,if the resulting amount of empty space isless than the inorder predecessor in the tree,the entry can be removed;otherwise,aDecreaseKey?is performed.To implement best fit,
10、we need to keep track of the amount of empty space in each bin.As before,a splay tree can keep track of this.To insert an item of size w?,perform aninsert of w?.If there is a bin that can fit the item exactly,the insert will detect it and splayit to the root;the item can be added and the root delete
11、d.Otherwise,the insert has placedw?at the root(which eventually needs to be removed).We find the minimum element M?in the right subtree,which brings M?to the right subtrees root,attach the left subtree toM?,and delete w?.We then perform an easily implemented DecreaseKey?on M?to reflectthe fact that
12、the bin is less empty.10.10Next fit:12 bins(.42,.25,.27),(.07,.72),(.86,.09),(.44,.50),(.68),(.73),(.31),(.78,.17),(.79),(.37),(.73,.23),(.30).First fit:10 bins(.42,.25,.27),(.07,.72,.09),(.86),(.44,.50),(.68,.31),(.73,.17),(.78),(.79),(.37,.23,.30),(.73).Best fit:10 bins(.42,.25,.27),(.07,.72,.09),
13、(.86),(.44,.50),(.68,.31),(.73,.23),(.78,.17),(.79),(.37,.30),(.73).First fit decreasing:10 bins(.86,.09),(.79,.17),(.78,.07),(.73,.27),(.73,.25),(.72,.23),(.68,.31),(.50,.44),(.42,.37),(.30).Best fit decreasing:10 bins(.86,.09),(.79,.17),(.78),(.73,.27),(.73,.25),(.72,.23),(.68,.31),(.50,.44),(.42,
14、.37,.07),(.30).Note that use of 10 bins is optimal.10.12We prove the second case,leaving the first and third(which give the same results asTheorem 10.6)to the reader.Observe thatlog p?N?=log p?(b?m?)=m?p?log p?b?Working this through,Equation(10.9)becomes T?(N?)=T?(b?m?)=a?m?i?=0m?ab?k?_?i?i?p?log p?
15、b?If a?=b?k?,thenT?(N?)=a?m?log p?b?i?=0mi?p?=O?(a?m?m?p?+1log p?b?)Since m?=log N?/log b?,and a?m?=N?k?,and b?is a constant,we obtainT?(N?)=O?(N?k?log p?+1N?)10.13The easiest way to prove this is by an induction argument.-55-10.14Divide the unit square into N?1 square grids each with side 1/?N?1.Si
16、nce there are N?points,some grid must contain two points.Thus the shortest distance is conservativelygiven by at most?2/(N?1).10.15The results of the previous exercise imply that the width of the strip is O?(1/?N?).Because the width of the strip is O?(1/?N?),and thus covers only O?(1/?N?)of the area
17、 ofthe square,we expect a similar fraction of the points to fall in the strip.Thus onlyO?(N?/?N?)points are expected in the strip;this is O?(?N?).10.17The recurrenceworks out toT?(N?)=T?(2N?/3)+T?(N?/3)+O?(N?)This is not linear,because the sum is not less than one.The running time is O?(N?log N?).10
18、.18The recurrencefor median-of-median-of-sevenpartitioning isT?(N?)=T?(5N?/7)+T?(N?/7)+O?(N?)If all we are concerned about is the linearity,then median-of-median-of-seven can beused.10.20When computing the median-of-median-of-five,30%of the elements are known to besmaller than the pivot,and 30%are k
19、nown to be larger.Thus these elements do not needto be involved in the partitioning phase.(Extra work would need to be done to imple-ment this,but since the whole algorithm isnt practical anyway,we can ignore any extrawork that doesnt involve element comparisons.)The original paper 9 describes theex
20、act constants in the worst-case bound,with and without this extra effort.10.21We derive the values of s?and,following the style in the original paper 17.Let Rt?,X?be the rank of element t?in some sample X?.If a sample S?of elements is chosen ran-domly from S?,and?|?S?|?=s?,?|?S?|?=N?,then weve alrea
21、dy seen thatE?(Rt?,S?)=s?+1N?+1_ _Rt?,S?where E?means expected value.For instance,if t?is the third largest in a sample of 5 ele-ments,then in a group of 19 elements it is expected to be the tenth largest.We can alsocalculate the variance:V?(Rt?,S?)=?(s?+1)2(s?+2)(Rt?,S?)(s?Rt?,S?+1)(N?+1)(N?s?)_ _=
22、O?(N?/?s?)We choose v?1and v?2so thatE?(Rv?1,S?)+2dV?(Rv?1,S?)k?E?(Rv?2,S?)2dV?(Rv?2,S?)where d?indicates how many variances we allow.(The larger d?is,the less likely the ele-ment we are looking for will not be in S?.)The probability that k?is not between v?1and v?2is2derf?(x?)dx?=O?(e?d?2/d?)If d?=
23、log1/2N?,then this probability is(1/N?),specifically O?(1/(N?log N?).This meansthat the expected work in this case is O?(log1N?)because O?(N?)work is performed withvery small probability.-56-These mean and variance equations implyRv?1,S?k(N?+1)(s?+1)_ d?s?andRv?2,S?k(N?+1)(s?+1)_+d?s?This gives equa
24、tion(A):=d?s?=?s?log 1/2N?(A)If we first pivot around v?2,the cost is N?comparisons.If we now partition elements in S?that are less than v?2around v?1,the cost is Rv?2,S?,which has expected value k?+s?+1N?+1_ _.Thus the total cost of partitioning is N?+k?+s?+1N?+1_ _.The cost of the selections to fi
25、nd v?1and v?2in the sample S?is O?(s?).Thus the total expected number of comparisons isN?+k?+O?(s?)+O?(N?/s?)The low order term is minimized whens?=N?/s?(B)Combining Equations(A)and(B),we see thats?2=N?=?s?N?log 1/2N?(C)s?3/2=N?log 1/2N?(D)s?=N?2/3log 1/3N?(E)=N?1/3log 2/3N?(F)10.22First,we calculat
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Mark Allen Weiss 数据结构与算法分析 课后习题答案10 数据结构 算法 分析 课后 习题 答案 10
限制150内