欢迎来到课桌文档! | 帮助中心 课桌文档-建筑工程资料库
课桌文档
全部分类
  • 党建之窗>
  • 感悟体会>
  • 百家争鸣>
  • 教育整顿>
  • 文笔提升>
  • 热门分类>
  • 计划总结>
  • 致辞演讲>
  • 在线阅读>
  • ImageVerifierCode 换一换
    首页 课桌文档 > 资源分类 > PPT文档下载  

    数据结构.ppt

    • 资源ID:229775       资源大小:608KB        全文页数:15页
    • 资源格式: PPT        下载积分:10金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    数据结构.ppt

    CHAPTER 2ALGORITHM ANALYSIS,【Definition】An algorithm is a finite set of instructions that,if followed,accomplishes a particular task.In addition,all algorithms must satisfy the following criteria:(1)Input There are zero or more quantities that are externally supplied.(2)Output At least one quantity is produced.(3)Definiteness Each instruction is clear and unambiguous.(4)Finiteness If we trace out the instructions of an algorithm,then for all cases,the algorithm terminates after finite number of steps.(5)Effectiveness Every instruction must be basic enough to be carried out,in principle,by a person using only pencil and paper.It is not enough that each operation be definite as in(3);it also must be feasible.,1/15,Note:A program is written in some programming language,and does not have to be finite(e.g.an operation system).An algorithm can be described by human languages,flow charts,some programming languages,or pseudo-code.,Example Selection Sort:Sort a set of n 1 integers in increasing order.,From those integers that are currently unsorted,find the smallest and place it next in the sorted list.,Where and how are they stored?,Where?,for(i=0;i n;i+)Examine listi to listn1 and suppose that the smallest integer is at listmin;Interchange listi and listmin;,Sort=Find the smallest integer+Interchange it with listi.,Algorithm in pseudo-code,2/15,1 What to Analyze,Machine&compiler-dependent run times.Time&space complexities:machine&compiler-independent.,Assumptions:instructions are executed sequentially each instruction is simple,and takes exactly one time unit integer size is fixed and we have infinite memory,Typically the following two functions are analyzed:Tavg(N)&Tworst(N)-the average and worst case time complexities,respectively,as functions of input size N.,If there is more than one input,these functions may have more than one argument.,3/15,1 What to Analyze,Example Matrix addition,/*rows+1*/,/*rows(cols+1)*/,/*rows cols*/,T(rows,cols)=2 rows cols+2rows+1,Q:What shall we do if rows cols?,A:Exchange rows and cols.,4/15,ExampleIterative function for summing a list of numbers,/*count=1*/,/*count+*/,/*count+for last execution of for*/,/*count+*/,/*count+*/,Tsum(n)=2n+3,ExampleRecursive function for summing a list of numbers,/*count+*/,/*count+*/,/*count+*/,Trsum(n)=2n+2But it takes more time to compute each step.,1 What to Analyze,5/15,1 What to Analyze,Is it really necessary to count the exact number of steps?,Uhhh.I dont think so.,Why not?,Because it drives me crazy!,So its too complicated sometimes.But does it worth the effort?,Take the iterative and recursive programs for summing a list for example-if you think 2n+2 is less than 2n+3,try a large n and youll be surprised!,I see.Then whats the point of this Tp stuff?,Good question!Lets ask the students.,6/15,2 Asymptotic Notation(,o),The point of counting the steps is to predict the growth in run time as the N change,and thereby compare the time complexities of two programs.So what we really want to know is the asymptotic behavior of Tp.,Suppose Tp1(N)=c1N2+c2N and Tp2(N)=c3N.Which one is faster?,No matter what c1,c2,and c3 are,there will be an n0 such that Tp1(N)Tp2(N)for all N n0.,I see!So as long as I know that Tp1 is about N2 and Tp2 is about N,then for sufficiently large N,P2 will be faster!,7/15,2 Asymptotic Notation,【Definition】T(N)=O(f(N)if there are positive constants c and n0 such that T(N)c f(N)for all N n0.,【Definition】T(N)=(g(N)if there are positive constants c and n0 such that T(N)c g(N)for all N n0.,【Definition】T(N)=(h(N)if and only if T(N)=O(h(N)and T(N)=(h(N).,Note:2N+3=O(N)=O(Nk1)=O(2N)=We shall always take the smallest f(N).2N+N2=(2N)=(N2)=(N)=(1)=We shall always take the largest g(N).,【Definition】T(N)=o(p(N)if T(N)=O(p(N)and T(N)(p(N).,8/15,2 Asymptotic Notation,Rules of Asymptotic Notation,If T1(N)=O(f(N)and T2(N)=O(g(N),then(a)T1(N)+T2(N)=max(O(f(N),O(g(N),(b)T1(N)*T2(N)=O(f(N)*g(N).,If T(N)is a polynomial of degree k,then T(N)=(N k).,logk N=O(N)for any constant k.This tells us that logarithms grow very slowly.,Note:When compare the complexities of two programs asymptotically,make sure that N is sufficiently large.For example,suppose that Tp1(N)=106N and Tp2(N)=N2.Although it seems that(N2)grows faster than(N),but if N 106,P2 is still faster than P1.,9/15,2 Asymptotic Notation,10/15,11/15,2 Asymptotic Notation,n,12/15,Example Matrix addition,/*(rows)*/,/*(rows cols)*/,/*(rows cols)*/,T(rows,cols)=(rows cols),2 Asymptotic Notation,13/15,2 Asymptotic Notation,General Rules,FOR LOOPS:The running time of a for loop is at most the running time of the statements inside the for loop(including tests)times the number of iterations.,NESTED FOR LOOPS:The total running time of a statement inside a group of nested loops is the running time of the statements multiplied by the product of the sizes of all the for loops.,CONSECUTIVE STATEMENTS:These just add(which means that the maximum is the one that counts).,IF/ELSE:For the fragmentif(Condition)S1;else S2;the running time is never more than the running time of the test plus the larger of the running time of S1 and S2.,14/15,2 Asymptotic Notation,RECURSIONS:,Example Fibonacci number:Fib(0)=Fib(1)=1,Fib(n)=Fib(n1)+Fib(n2),long int Fib(int N)if(N=1)return 1;else return Fib(N 1)+Fib(N 2);,/*O(1)*/,/*O(1)*/,/*O(1)*/,/*T(N)*/,/*T(N 1)*/,/*T(N 2)*/,T(N)=T(N 1)+T(N 2)+2,Fib(N),Proof by induction,Q:Why is it so bad?,15/15,

    注意事项

    本文(数据结构.ppt)为本站会员(夺命阿水)主动上传,课桌文档仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知课桌文档(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-1

    经营许可证:宁B2-20210002

    宁公网安备 64010402000986号

    课桌文档
    收起
    展开