10-11-01ADA04(算法分析基础-递归与非递归算法的分析方法II)课件.ppt

上传人(卖家):晟晟文业 文档编号:4567476 上传时间:2022-12-20 格式:PPT 页数:22 大小:618.50KB
下载 相关 举报
10-11-01ADA04(算法分析基础-递归与非递归算法的分析方法II)课件.ppt_第1页
第1页 / 共22页
10-11-01ADA04(算法分析基础-递归与非递归算法的分析方法II)课件.ppt_第2页
第2页 / 共22页
10-11-01ADA04(算法分析基础-递归与非递归算法的分析方法II)课件.ppt_第3页
第3页 / 共22页
10-11-01ADA04(算法分析基础-递归与非递归算法的分析方法II)课件.ppt_第4页
第4页 / 共22页
10-11-01ADA04(算法分析基础-递归与非递归算法的分析方法II)课件.ppt_第5页
第5页 / 共22页
点击查看更多>>
资源描述

1、Fundamentals of the Analysis of Algorithm Efficiency(III)Chapter 21、Analysis of non-recursive algorithms(II)2、Analysis of recursive algorithms 2022-12-2032010-2011-01 Design and Analysis of Algorithm SCUECExample 2:Element uniqueness problemInput size:Basic operation:Best,worst,average case:Summatio

2、n:Cworst=nComparisonyes2120(1)1()2nnij in nn2022-12-2042010-2011-01 Design and Analysis of Algorithm SCUECExample 3:Matrix multiplicationInput size:Basic operation:Best,worst,average case:Summation:nMultiplicationno111330001()nnnijknn2022-12-2052010-2011-01 Design and Analysis of Algorithm SCUECExam

3、ple 4:Counting binary digits Input size:Basic operation:Best,worst,average case:Summation:nComparisonnoAbout logn2022-12-2062010-2011-01 Design and Analysis of Algorithm SCUECPlan for Analysis of Recursive AlgorithmsbDecide on a parameter indicating an inputs size.bIdentify the algorithms basic oper

4、ation.bCheck whether the number of times the basic op.is executed may vary on different inputs of the same size.(If it may,the worst,average,and best cases must be investigated separately.)bSet up a recurrence relation with an appropriate initial condition expressing the number of times the basic op

5、.is executed.bSolve the recurrence(or,at the very least,establish its solutions order of growth)by backward substitutions or another method.2022-12-2072010-2011-01 Design and Analysis of Algorithm SCUECRecurrence RelationsbA recurrence relation is a recursive form of an equation,for example:bA recur

6、rence relation can be put into an equivalent closed form without the recursion Generation function Characteristics equation Substitution(1)0()(1)1TT nT n2022-12-2082010-2011-01 Design and Analysis of Algorithm SCUECExample 5:Recursive evaluation of n!Definition:n!=1 2 (n-1)n for n 1 and 0!=1Recursiv

7、e definition of n!:F(n)=F(n-1)n for n 1 and F(0)=1Input size:Basic operation:Best,worst,average case:Recurrence relation:nMultiplication noT(n)=T(n-1)+1,T(0)=02022-12-2092010-2011-01 Design and Analysis of Algorithm SCUECSolving the recurrence for T(n)T(n)=T(n-1)+1,T(0)=0bBegin by looking at a serie

8、s of equations with decreasing values of n:T()T(1)1T(1)T(2)1T(2)T(3)1T(3)T(4)1T(4)T(5)1nnnnnnnnnn2022-12-20 102010-2011-01 Design and Analysis of Algorithm SCUECbThen,we substitute back into the first equation:T()T(1)1T()(T(2)1)1T()(T(3)1)1)1T()(T(4)1)1)1)1T()(T(5)1)1)1)1)1nnnnnnnnnnSolving the recu

9、rrence for T(n)(cont.)2022-12-20 112010-2011-01 Design and Analysis of Algorithm SCUECbWe stop when we get to T(0):bHow many“+1”terms are there?Notice we increase them with each substitution.T()T(1)1T()(T(2)1)1T()(T(0)1)1)1)1nnnnnSolving the recurrence for T(n)(cont.)2022-12-20 122010-2011-01 Design

10、 and Analysis of Algorithm SCUECbWe must have n of the“+1”terms because we did n substitutions:bSo,our closed form of the equation is:11T()T(0)1ninT()nnSolving the recurrence for T(n)(cont.)2022-12-20 132010-2011-01 Design and Analysis of Algorithm SCUECExample 6:The Tower of Hanoi Puzzle123Recurren

11、ce for number of moves:2022-12-20 142010-2011-01 Design and Analysis of Algorithm SCUECSolving recurrence for number of movesM(n)=2M(n-1)+1,M(1)=12022-12-20 152010-2011-01 Design and Analysis of Algorithm SCUECExample 7:Counting#bitsInput size:Basic operation:Best,worst,average case:Recurrence relat

12、ion:nAddition noA(n)=A(n/2)+1 A(1)=0Closed form:2()logA nn 2022-12-20 162010-2011-01 Design and Analysis of Algorithm SCUECExample 8 Fibonacci numbersThe Fibonacci numbers:0,1,1,2,3,5,8,13,21,The Fibonacci recurrence:F(n)=F(n-1)+F(n-2)F(0)=0 F(1)=1General 2nd order homogeneous linear recurrence with

13、 constant coefficients:aX(n)+bX(n-1)+cX(n-2)=02022-12-20 172010-2011-01 Design and Analysis of Algorithm SCUECSolving aX(n)+bX(n-1)+cX(n-2)=0bSet up the characteristic equation(quadratic)ar2+br+c=0bSolve to obtain roots r1 and r2bGeneral solution to the recurrenceif r1 and r2 are two distinct real r

14、oots:X(n)=r1n+r2nif r1=r2=r are two equal real roots:X(n)=rn+nr nbParticular solution can be found by using initial conditions2022-12-20 182010-2011-01 Design and Analysis of Algorithm SCUECApplication to the Fibonacci numbersF(n)=F(n-1)+F(n-2)or F(n)-F(n-1)-F(n-2)=0Characteristic equation:Roots of

15、the characteristic equation:General solution to the recurrence:Particular solution for F(0)=0,F(1)=1:r2 -r-1=01,211 4(1)1522r1515()22nnF n115115()2255nnF n2022-12-20 192010-2011-01 Design and Analysis of Algorithm SCUECComputing Fibonacci numbers1.Definition-based recursive algorithmALGORITHM F(n)/I

16、nput:A nonnegative integer n /Output:The nth Fibonacci number if n 1 return n else return F(n-1)+F(n-2)Input size:Basic operation:Best,worst,average case:Recurrence relation:nAddition noA(n)=A(n-1)+A(n-2)+1 A(0)=0,A(1)=0Closed form:11115115()12255nnA n2022-12-20 202010-2011-01 Design and Analysis of

17、 Algorithm SCUECComputing Fibonacci numbers3.Explicit formula algorithmALGORITHM Fib(n)/Input:A nonnegative integer n /Output:The nth Fibonacci number F0 0;F1 0 for i 2 to n do Fi Fi-1+Fi-2 return F(n)2.Nonrecursive definition-based algorithmInput size:Basic operation:Best,worst,average case:Summation:nAddition211()ninn 115115()2255nnF nNoThe End2022-12-20 222010-2011-01 Design and Analysis of Algorithm SCUECAssignment bPreview:chapter 3bExercises:No 1,3,4 of exercises 2.4 No 6 of exercises 2.5

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公、行业 > 各类PPT课件(模板)
版权提示 | 免责声明

1,本文(10-11-01ADA04(算法分析基础-递归与非递归算法的分析方法II)课件.ppt)为本站会员(晟晟文业)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

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


163文库-Www.163Wenku.Com |网站地图|