计算机算法导论-第9章课件.ppt
- 【下载声明】
1. 本站全部试题类文档,若标题没写含答案,则无答案;标题注明含答案的文档,主观题也可能无答案。请谨慎下单,一旦售出,不予退换。
2. 本站全部PPT文档均不含视频和音频,PPT中出现的音频或视频标识(或文字)仅表示流程,实际无音频或视频文件。请谨慎下单,一旦售出,不予退换。
3. 本页资料《计算机算法导论-第9章课件.ppt》由用户(三亚风情)主动上传,其收益全归该用户。163文库仅提供信息存储空间,仅对该用户上传内容的表现方式做保护处理,对上传内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(点击联系客服),我们立即给予删除!
4. 请根据预览情况,自愿下载本文。本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
5. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007及以上版本和PDF阅读器,压缩文件请下载最新的WinRAR软件解压。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 算法 导论 课件
- 资源描述:
-
1、 1 8/6/2022Introduction to Algorithms9 Medians and Order Statistics 2 8/6/2022Order StatisticsThe ith order statistic in a set of n elements is the ith smallest elementThe minimum is thus the 1st order statistic The maximum is the nth order statisticThe median is the n/2 order statisticIf n is even,
2、there are 2 medians 3 8/6/2022Selection ProblemInput:A set A of n(distinct)numbers and a number i,with 1 i n.Output:The element x A that is larger than exactly i-1 other elements of A.How can we calculate order statistics?What is the running time?4 8/6/20229.1 Minimum and Maximum 5 8/6/2022Minimum a
3、nd MaximumHow many comparisons are needed to find the minimum element in a set?The maximum?MINIMUM(A)1 min A1 2 for i 2 to lengthA 3 do if min Ai 4 then min Ai 5 return min 6 8/6/2022Simultaneous Minimum and MaximumCan we find the minimum and maximum with less than twice the cost?Yes:Walk through el
4、ements by pairsCompare each element in pair to the otherCompare the largest to maximum,smallest to minimumTotal cost:3 comparisons per 2 elements 3 n/2 =O(3n/2)7 8/6/20229.2 Selection in expected linear time 8 8/6/2022The Selection ProblemA more interesting problem is selection:finding the ith small
5、est element of a set We will show:A practical randomized algorithm with O(n)expected running timeA cool algorithm of theoretical interest only with O(n)worst-case running time 9 8/6/2022Randomized SelectionKey idea:use partition()from quicksortBut,only need to examine one subarrayThis savings shows
6、up in running time:O(n)We will again use a slightly different partition q=RandomizedPartition(A,p,r)Aq Aqqpr 10 8/6/2022Randomized SelectionRandomizedSelect(A,p,r,i)if(p=r)then return Ap;q=RandomizedPartition(A,p,r)k=q-p+1;if(i=k)then return Aq;if(i k)then return RandomizedSelect(A,p,q-1,i);else ret
7、urn RandomizedSelect(A,q+1,r,i-k);Aq Aqkqpr 11 8/6/2022Randomized SelectionAnalyzing RandomizedSelect()Worst case:partition always 0:n-1T(n)=T(n-1)+O(n)=?=O(n2)(arithmetic series)No better than sorting!“Best”case:suppose a 9:1 partitionT(n)=T(9n/10)+O(n)=?=O(n)(Master Theorem,case 3)Better than sort
8、ing!What if this had been a 99:1 split?12 8/6/2022Randomized Selection 13 8/6/2022Randomized Selection 14 8/6/2022Calculating expectation 15 8/6/2022Calculating expectation 16 8/6/2022Calculating expectation 17 8/6/2022Calculating expectation 18 8/6/2022Calculating expectation 19 8/6/2022Randomized
9、SelectionLets show that T(n)=O(n)by substitution 12/1021,max1nnknknkTnnknkTnnTWhat happened here?20 8/6/2022What happened here?“Split”the recurrenceWhat happened here?What happened here?What happened here?Randomized SelectionAssume T(n)cn for sufficiently large c:nncncnnnnnncnkkncncknnkTnnTnknknnknn
展开阅读全文