Analysis of Algorithms Orders of Growth.ppt
《Analysis of Algorithms Orders of Growth.ppt》由会员分享,可在线阅读,更多相关《Analysis of Algorithms Orders of Growth.ppt(60页珍藏版)》请在麦多课文档分享上搜索。
1、1,Analysis of Algorithms & Orders of Growth,Rosen 6th ed., 3.1-3.3,2,Analysis of Algorithms,An algorithm is a finite set of precise instructions for performing a computation or for solving a problem. What is the goal of analysis of algorithms? To compare algorithms mainly in terms of running time bu
2、t also in terms of other factors (e.g., memory requirements, programmers effort etc.) What do we mean by running time analysis? Determine how running time increases as the size of the problem increases.,3,Example: Searching,Problem of searching an ordered list. Given a list L of n elements that are
3、sorted into a definite order (e.g., numeric, alphabetical), And given a particular element x, Determine whether x appears in the list, and if so, return its index (position) in the list.,4,Search alg. #1: Linear Search,procedure linear search (x: integer, a1, a2, , an: distinct integers) i := 1 whil
4、e (i n x ai) i := i + 1 if i n then location := i else location := 0 return location index or 0 if not found,5,Search alg. #2: Binary Search,Basic idea: On each step, look at the middle element of the remaining list to eliminate half of it, and quickly zero in on the desired element.,x,x,x,x,6,Searc
5、h alg. #2: Binary Search,procedure binary search (x:integer, a1, a2, , an: distinct integers) i := 1 left endpoint of search interval j := n right endpoint of search interval while i1 item m := (i+j)/2 midpoint if xam then i := m+1 else j := m end if x = ai then location := i else location := 0 retu
6、rn location,7,Is Binary Search more efficient?,Number of iterations: For a list of n elements, Binary Search can execute at most log2 n times! Linear Search, on the other hand, can execute up to n times !,8,Is Binary Search more efficient?,Number of computations per iteration: Binary search does mor
7、e computations than Linear Search per iteration. Overall: If the number of components is small (say, less than 20), then Linear Search is faster. If the number of components is large, then Binary Search is faster.,9,How do we analyze algorithms?,We need to define a number of objective measures.(1) C
8、ompare execution times? Not good: times are specific to a particular computer !(2) Count the number of statements executed? Not good: number of statements vary with the programming language as well as the style of the individual programmer.,10,Example (# of statements),Algorithm 1 Algorithm 2arr0 =
9、0; for(i=0; iN; i+) arr1 = 0; arri = 0; arr2 = 0;. arrN-1 = 0;,11,How do we analyze algorithms?,(3) Express running time as a function of the input size n (i.e., f(n). To compare two algorithms with running times f(n) and g(n), we need a rough measure of how fast a function grows. Such an analysis i
10、s independent of machine time, programming style, etc.,12,Computing running time,Associate a “cost“ with each statement and find the “total cost“ by finding the total number of times each statement is executed. Express running time in terms of the size of the problem. Algorithm 1 Algorithm 2Cost Cos
11、tarr0 = 0; c1 for(i=0; iN; i+) c2arr1 = 0; c1 arri = 0; c1arr2 = 0; c1.arrN-1 = 0; c1 - -c1+c1+.+c1 = c1 x N (N+1) x c2 + N x c1 = (c2 + c1) x N + c2,13,Computing running time (cont.),Cost sum = 0; c1 for(i=0; iN; i+) c2for(j=0; jN; j+) c2 sum += arrij; c3- c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N
12、x N,14,Comparing Functions Using Rate of Growth,Consider the example of buying elephants and goldfish:Cost: cost_of_elephants + cost_of_goldfishCost cost_of_elephants (approximation) The low order terms in a function are relatively insignificant for large nn4 + 100n2 + 10n + 50 n4i.e., n4 + 100n2 +
13、10n + 50 and n4 have the same rate of growth,15,Rate of Growth Asymptotic Analysis,Using rate of growth as a measure to compare different functions implies comparing them asymptotically. If f(x) is faster growing than g(x), then f(x) always eventually becomes larger than g(x) in the limit (for large
14、 enough values of x).,16,Example,Suppose you are designing a web site to process user data (e.g., financial records). Suppose program A takes fA(n)=30n+8 microseconds to process any n records, while program B takes fB(n)=n2+1 microseconds to process the n records. Which program would you choose, kno
15、wing youll want to support millions of users?,A,17,Visualizing Orders of Growth,On a graph, as you go to the right, a faster growing function eventually becomes larger.,fA(n)=30n+8,Increasing n ,fB(n)=n2+1,Value of function ,18,Big-O Notation,We say fA(n)=30n+8 is order n, or O(n). It is, at most, r
16、oughly proportional to n. fB(n)=n2+1 is order n2, or O(n2). It is, at most, roughly proportional to n2. In general, an O(n2) algorithm will be slower than O(n) algorithm. Warning: an O(n2) function will grow faster than an O(n) function.,19,More Examples ,We say that n4 + 100n2 + 10n + 50 is of the
17、order of n4 or O(n4) We say that 10n3 + 2n2 is O(n3) We say that n3 - n2 is O(n3) We say that 10 is O(1), We say that 1273 is O(1),20,Big-O Visualization,21,Computing running time,Algorithm 1 Algorithm 2Cost Costarr0 = 0; c1 for(i=0; iN; i+) c2arr1 = 0; c1 arri = 0; c1arr2 = 0; c1.arrN-1 = 0; c1 - -
18、c1+c1+.+c1 = c1 x N (N+1) x c2 + N x c1 = (c2 + c1) x N + c2,O(n),22,Computing running time (cont.),Cost sum = 0; c1 for(i=0; iN; i+) c2for(j=0; jN; j+) c2 sum += arrij; c3 -c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N x N,O(n2),23,Running time of various statements,while-loop,for-loop,24,i = 0; while
19、(iN) X=X+Y; / O(1)result = mystery(X); / O(N), just an example.i+; / O(1) The body of the while loop: O(N) Loop is executed: N timesN x O(N) = O(N2),Examples,25,if (ij)for ( i=0; iN; i+ )X = X+i; elseX=0;Max ( O(N), O(1) ) = O (N),O(N),O(1),Examples (cont.d),26,Asymptotic Notation,O notation: asympt
20、otic “less than”: f(n)=O(g(n) implies: f(n) “” g(n) notation: asymptotic “greater than”: f(n)= (g(n) implies: f(n) “” g(n) notation: asymptotic “equality”: f(n)= (g(n) implies: f(n) “=” g(n),27,Definition: O(g), at most order g,Let f,g are functions RR. We say that “f is at most order g”, if: c,k: f
21、(x) cg(x), xk “Beyond some point k, function f is at most a constant c times g (i.e., proportional to g).” “f is at most order g”, or “f is O(g)”, or “f=O(g)” all just mean that fO(g). Sometimes the phrase “at most” is omitted.,28,Big-O Visualization,k,29,Points about the definition,Note that f is O
22、(g) as long as any values of c and k exist that satisfy the definition. But: The particular c, k, values that make the statement true are not unique: Any larger value of c and/or k will also work. You are not required to find the smallest c and k values that work. (Indeed, in some cases, there may b
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ANALYSISOFALGORITHMSORDERSOFGROWTHPPT
