PDF文库 - 千万精品文档,你想要的都能搜到,下载即用。

数据结构课件_01Algorithms.pdf

A°Curtain 私念9 页 212.812 KB下载文档
数据结构课件_01Algorithms.pdf数据结构课件_01Algorithms.pdf数据结构课件_01Algorithms.pdf数据结构课件_01Algorithms.pdf数据结构课件_01Algorithms.pdf数据结构课件_01Algorithms.pdf
当前文档共9页 2.88
下载后继续阅读

数据结构课件_01Algorithms.pdf

Data Structures Algorithms Teacher : Wang Wei 1. Ellis Horowitz,etc., Fundamentals of Data Structures in C++ 2. 金远平, 数据结构 3. http://inside.mines.edu/~dmehta/ 4. 殷人昆, 数据结构 王伟, 计算机工程系, 东南大学 1 Why need algorithms • To computer science – The concept of an algorithm is fundamental • In developing large-scale computer systems – Algorithms • exist for many common problems • designing efficient algorithms plays a crucial role 王伟, 计算机工程系, 东南大学 2 Algorithm Definition • is a step-by-step procedure – a finite set of instructions to be executed in a certain order to get the desired output – • • if followed, accomplishes a particular task Algorithms are generally created independent of underlying languages 王伟, 计算机工程系, 东南大学 3 1 Characteristics Input   Zero or more quantities are externally supplied Output   At least one quantity is produced Definiteness   Each instructions is clear abs unambiguous Finiteness   If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a finite number of steps Effectiveness   Every instruction must be basic enough to be carried out  王伟, 计算机工程系, 东南大学 4 Algorithm Analysis and Measurement • Performance Criteria • Posteriori testing • Priori estimates – Asymptotic Analysis 王伟, 计算机工程系, 东南大学 5 Important Criteria • • • • • • Correctness Readability Efficiency Robustness Usability Simplicity 王伟, 计算机工程系, 东南大学 6 2 Complexities • Time Complexity of a program – is the amount of computer time it needs to run to completion • Running time or the execution time of operations of data structure must be as small as possible • Space Complexity of a program – is the amount of memory it needs to run to completion • Memory usage of a data structure operation should be as little as possible 王伟, 计算机工程系, 东南大学 7 Performance Measurement for Time Complexity • Posteriori testing – is concerned with obtaining the actual space and time requirements of a program 王伟, 计算机工程系, 东南大学 8 Example : Sequential Search int seqsearch ( int a[ ], int n, int x ) { //在a[0],…,a[n-1]中搜索与给定值 x 相等的元 //素,函数返回其位置,失败返回-1。 int i = 0; while ( i < n && a[i] != x ) i++; if ( i == n ) return -1; return i; } 王伟, 计算机工程系, 东南大学 9 3 Measuring the computing time of a program function time() or clock() • Example: double runTime; double start, stop; time(&start); int k = seqsearch (a, n, x); time(&stop); runTime = stop - start; cout << " RunTime : " << runTime << endl; 王伟, 计算机工程系, 东南大学 10 • These quantities are dependent on the particular compiler and options used as well as on the specific computer on which the program is run 王伟, 计算机工程系, 东南大学 11 Performance analysis for Time Complexity • Priori estimates : – – to predict the growth in run time as the instance characteristics change asymptotic notation • Big “oh” : O 王伟, 计算机工程系, 东南大学 12 4 Asymptotic Notation • f (n) = O(g(n)) – iff (if and only if) there exist positive constants c and n0 such that f(n)≤cg(n) for all n, n≥n0 • g(n) – is an upper bound on the value – should be as small a function of n as one come up 王伟, 计算机工程系, 东南大学 13 Theorem 1.2 • if f(n) = amnm+…+ a1n+a0 , then f(n)=O(nm) – Proof : ∑ ∑ | | ∑ | |, – So, f(n) = O(nm) – When the complexity of an algorithm is actually, say, O(log n), – but we can only show that it is O(n) due to the limitation of our knowledge – it is OK to say so. – This is one benefit of O notation as upper bound. 王伟, 计算机工程系, 东南大学 14 How the various functions grow with n? Ultimate Laptop, 1 year 1 second 1E+60 1E+55 1E+50 2^N 1.2^N N^5 N^3 5N 1E+45 1E+40 1000 MIPS, since Big Bang 1E+35 1E+30 1E+25 1E+20 1000 MIPS, 1 day 1E+15 1E+10 100000 1 1 10 100 1000 王伟, 计算机工程系, 东南大学 15 5 Time complexity • The time taken by a program P t(P) = c + tP(n) • c : constant • tP : function fP (n) • n : the number of the inputs and outputs • T(n) = O(f(n)) 16 王伟, 计算机工程系, 东南大学 Compile time • Run or execution time program step  a syntactically or semantically meaningful segment of a program that has a run time •   Run time is independent of n 王伟, 计算机工程系, 东南大学  17 Determine the number of steps : method 1  Introduce a global variable count with initial value 0 int count=0; float sum (float a[ ], int n) { float s = 0.0; //count++ count++; for (int i = 0; i < n; i++) //count++ : ; { count ++; s += a[i]; //count++ count++; } count ++ //count++: ; count++; return s; //count++ : return } 王伟, 计算机工程系, 东南大学 18 6 Determine the number of steps : method 2   { build a table s/e : steps per execution program the number s/e frequency steps Determine of steps 0 1 0 程序步数计算工作表格 float s = 0.0; 1 1 1 for ( int i=0; i0 n=0/n>0 1+f(n-1) 0/1 0/1+f(n-1) 0 1/1 0/0 2/ 2+f(n-1) 0 1/1 Determine the number of steps0/0 if (n<=0 ) 1 1/1 1/1 return 0; 1 1/0 1/0 程序步数计算工作表格 else return sum(a,n-1)+a[n-1]); } total steps 20 王伟, 计算机工程系, 东南大学 • T(n, m) = T1 (n) + T2 (m) = O(max (f (n), g (m))) x = 0; y = 0; T1 (n) = O(1) for ( int k = 0; k < n; k ++ ) x ++; T2(n) = O(n) for ( int i = 0; i < n; i++ ) for ( int j = 0; j < n; j++ ) y ++; T3(n) = O(n2) T(n) = T1(n)+T2(n)+T3(n) = O( max( 1, n, n2 ) ) = O(n2) 王伟, 计算机工程系, 东南大学 21 7 void bubbleSort (int a[ ], int n ) { //对表 a[ ] 逐趟比较, n 是表当前长度 for (int i = 1; i <= n-1; i++) { //n-1趟 for (int j = n-1; j >= i; j--) //n-i次比较 if (a[j-1] > a[j]) { int tmp = a[j-1]; a[j-1] = a[j]; a[j] = tmp; } //一趟比较 } } 王伟, 计算机工程系, 东南大学 • 22 T (n, m) = T1 (n) * T2 (m) = O(f (n)*g (m)) BubbleSort 外层循环 n-1 趟 内层循环 n-i 次比较 O(f (n)*g (n)) = O(n2) n 1   (n  i)  i 1 n(n  1 ) 2 王伟, 计算机工程系, 东南大学 23 Execution Time Cases three cases • Worst Case – This is the scenario where a particular data structure operation takes maximum time it can take. – If an operation's worst case time is ƒ(n) then this operation will not take more than ƒ(n) time where ƒ(n) represents function of n • Average Case – This is the scenario depicting the average execution time of an operation of a data structure. – If an operation takes ƒ(n) time in execution, then m operations will take mƒ(n) time • Best Case – This is the scenario depicting the least possible execution time of an operation of a data structure. – If an operation takes ƒ(n) time in execution, then the actual operation may take time as the random number which would be maximum as ƒ(n) 王伟, 计算机工程系, 东南大学 24 8 Space complexity • The space requirement of program P S(P) = c + SP(n) • c : constant • SP : function fP (n) • n : the number of the inputs and outputs • S(n) = O(f(n)) 25 王伟, 计算机工程系, 东南大学 • • Fixed part : is independent of the number of the inputs and outputs – Space for the code – Constant – Simple variables – Fixed-size component variables Variable part : is dependent on the particular instance – component variables – Referenced variables – Recursion stack space 王伟, 计算机工程系, 东南大学 26 Example //iterative function float Sum (float *a, const int n) { float s=0; for(int i=0;i

相关文章