书签 分享 收藏 举报 版权申诉 / 39
上传文档赚钱

类型计算机算法导论-第9章课件.ppt

  • 上传人(卖家):三亚风情
  • 文档编号:3224337
  • 上传时间:2022-08-07
  • 格式:PPT
  • 页数:39
  • 大小:1.03MB
  • 【下载声明】
    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

    10、k122121221121222)(2)(1211112/12/The recurrence we started withSubstitute T(n)cn for T(k)Expand arithmetic seriesMultiply it out 21 8/6/2022What happened here?Subtract c/2What happened here?What happened here?What happened here?Randomized SelectionAssume T(n)cn for sufficiently large c:The recurrence

    11、 so farMultiply it out Rearrange the arithmeticWhat we set out to prove enough)big is c if(2424241221)(cnnccncnnccncnnccnccnnncncnT 22 8/6/2022Summary of randomized order-statistic selection Works fast:linear expected time.Excellent algorithm in practice.But,the worst case is very bad:(n2).23 8/6/20

    12、229.3 Selection in worst-case linear time 24 8/6/2022Worst-Case Linear-Time SelectionWhat follows is a worst-case linear time algorithm,really of theoretical interest onlyBasic idea:Generate a good partitioning elementCall this element x 25 8/6/2022Worst-Case Linear-Time SelectionThe algorithm in wo

    13、rds:1.Divide n elements into groups of 52.Find median of each group(How?How long?)3.Use Select()recursively to find median x of the n/5 medians4.Partition the n elements around x.Let k=rank(x)5.if(i=k)then return xif(i k)use Select()recursively to find(i-k)th smallest element in last partition 26 8/

    14、6/2022Choosing the pivot 27 8/6/2022Choosing the pivot 28 8/6/2022Choosing the pivot 29 8/6/2022Choosing the pivot 30 8/6/2022Analysis 31 8/6/2022Analysis(Assume all elements are distinct.)32 8/6/2022Analysis(Assume all elements are distinct.)33 8/6/2022Worst-Case Linear-Time SelectionHow many of th

    15、e 5-element medians are x?At least 1/2 of the medians=n/5/2=n/10How many elements are x?At least 3 n/10 elementsFor large n,3 n/10 n/4(How large?)So at least n/4 elements xSimilarly:at least n/4 elements x 34 8/6/2022Worst-Case Linear-Time Selection 35 8/6/2022Worst-Case Linear-Time SelectionThus af

    16、ter partitioning around x,step 5 will call Select()on at most 3n/4 elementsThe recurrence is therefore:enough big is if20)(2019)(435435435)(ccnncncnncnncncnnnTnTnnTnTnT?n/5 n/5Substitute T(n)=cnCombine fractions Express in desired formWhat we set out to prove 36 8/6/2022Linear-Time Median SelectionG

    17、iven a“black box”O(n)median algorithm,what can we do?ith order statistic:Find median xPartition input around xif(i (n+1)/2)recursively find ith element of first halfelse find(i-(n+1)/2)th element in second halfT(n)=T(n/2)+O(n)=O(n)Can you think of an application to sorting?37 8/6/2022Worst-Case Line

    18、ar-Time SelectionIntuitively:Work at each level is a constant fraction(19/20)smallerGeometric progression!38 8/6/2022Linear-Time Median SelectionWorst-case O(n lg n)quicksortFind median x and partition around itRecursively quicksort two halvesT(n)=2T(n/2)+O(n)=O(n lg n)39 8/6/2022The EndExercises9.1-1;9.2-4;9.3-8,9

    展开阅读全文
    提示  163文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
    关于本文
    本文标题:计算机算法导论-第9章课件.ppt
    链接地址:https://www.163wenku.com/p-3224337.html

    Copyright@ 2017-2037 Www.163WenKu.Com  网站版权所有  |  资源地图   
    IPC备案号:蜀ICP备2021032737号  | 川公网安备 51099002000191号


    侵权投诉QQ:3464097650  资料上传QQ:3464097650
       


    【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。

    163文库