Algorithm Cost Algorithm Complexity.ppt
《Algorithm Cost Algorithm Complexity.ppt》由会员分享,可在线阅读,更多相关《Algorithm Cost Algorithm Complexity.ppt(42页珍藏版)》请在麦多课文档分享上搜索。
1、Algorithm Cost Algorithm Complexity,Lecture 23,Algorithm Cost,Back to Bunnies,Recall that we calculated Fibonacci Numbers using two different techniquesRecursionIteration,LB,Back to Bunnies,Recursive calculation of Fibonacci Numbers: Fib(1) = 1 Fib(2) = 1 Fib(N) = Fib(N-1) + Fib(N-2) So: Fib(3) = Fi
2、b(2) + Fib(1)= 1 + 1= 2,LB,Tree Recursion?,f(n),f(n-1),f(n-2),f(n-2),f(n-3),f(n-4),f(n-3),f(n-3),f(n-4),f(n-4),f(n-5),f(n-4),f(n-5),f(n-5),f(n-6),LB,Tree Recursion Example,f(6),f(5),f(4),f(4),f(3),f(2),f(3),f(3),f(2),f(2),f(1),f(2),f(1),f(2),f(1),LB,Recursively,public static int fibR(int n) if(n = 1
3、 | n =2)return 1;elsereturn fibR(n-1) + fibR(n-2); ,LB,Iteratively,public static int fibI(int n)int oldest = 1;int old = 1;int fib = 1;while(n- 2) fib = old + oldest;oldest = old;old = fib;return fib;,LB,Slight Modifications,LB,public static int fibR(int n) if(n = 1 | n =2)return 1;elsereturn fibR(n
4、-1) + fibR(n-2); ,public static int fibI(int n)int oldest = 1;int old = 1;int fib = 1;while(n- 2) fib = old + oldest;oldest = old;old = fib;return fib;,Add Counters,Demo,LB,Conclusion,Algorithm choice or design can make a big difference!,LB,Correctness is Not Enough,It isnt sufficient that our algor
5、ithms perform the required tasks.We want them to do so efficiently, making the best use of Space Time,Time and Space,Time Instructions take time. How fast does the algorithm perform? What affects its runtime?Space Data structures take space. What kind of data structures can be used? How does the cho
6、ice of data structure affect the runtime?,Time vs. Space,Very often, we can trade space for time:For example: maintain a collection of students with SSN information. Use an array of a billion elements and have immediate access (better time) Use an array of 35 elements and have to search (better spac
7、e),The Right Balance,The best solution uses a reasonable mix of space and time.Select effective data structures to represent your data model.Utilize efficient methods on these data structures.,Questions?,Algorithm Complexity,Scenarios,Ive got two algorithms that accomplish the same task Which is bet
8、ter?Given an algorithm, can I determine how long it will take to run? Input is unknown Dont want to trace all possible paths of executionFor different input, can I determine how an algorithms runtime changes?,Measuring the Growth of Work,While it is possible to measure the work done by an algorithm
9、for a given set of input, we need a way to: Measure the rate of growth of an algorithm based upon the size of the input Compare algorithms to determine which is better for the situation,Introducing Big O,Will allow us to evaluate algorithms. Has precise mathematical definition We will use simplified
10、 version in CS 1311Caution for the real world: Only tells part of the story!Used in a sense to put algorithms into families,LB,Why Use Big-O Notation,Used when we only know the asymptotic upper bound. If you are not guaranteed certain input, then it is a valid upper bound that even the worst-case in
11、put will be below. May often be determined by inspection of an algorithm. Thus we dont have to do a proof!,Size of Input,In analyzing rate of growth based upon size of input, well use a variable For each factor in the size, use a new variable N is most commonExamples: A linked list of N elements A 2
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ALGORITHMCOSTALGORITHMCOMPLEXITYPPT
