1 / 48

Basic Concepts

Basic Concepts. Chapter 1. Overview: System l ife c ycle. Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm) Refinement and coding Verification Correctness proofs Testing Error removal. Data abstraction and encapsulation.

Télécharger la présentation

Basic Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Concepts Chapter 1

  2. Overview: System life cycle • Requirements • Analysis • Bottom-up • Top-down • Design • Data objects (abstract data type) and operations (algorithm) • Refinement and coding • Verification • Correctness proofs • Testing • Error removal

  3. Data abstraction and encapsulation • Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object from the outside world • Data Abstraction is the separation between the specification of a data object and its implementation • A data type is a collection of objects and a set of operations that act on those objects • An abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects, and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operations

  4. Example 1.1 [Abstract data type NaturalNumber] • ADT NaturalNumber is • objects: an ordered subrange of the integers starting at zero and ending at the maximum integer (MAXINT) on the computer. • functions: • for all x, y ∈ NatureNumber; TRUE, FALSE ∈ Boolean and where +, -, <, and == are the usual integer operations. • Zero ( ):NaturalNumber ::= 0 • IsZero(x):Boolean ::= if (x == 0) IsZero = true; else IsZero = false • Add(x, y):NaturalNumber ::= if ((x+y) <= MAXINT)Add = x+y; else Add = MAXINT • Equal(x,y):Boolean ::= if (x == y) Equal = TRUE; else Equal = FALSE • Successor(x):NaturalNumber ::= if (x == MAXINT)Successor = x;else Successor = x+1 • Subtract(x,y):NaturalNumber ::= if (x<y) Subtract = 0; else Subtract = x-y • end NaturalNumber

  5. Algorithm specification • An algorithm is a finite set of instructions that accomplishes a particular task • Criteria • Input • Output • Definiteness: clear and unambiguous • Finiteness: terminate after a finite number of steps • Effectiveness: instruction is basic enough to be carried out, in principle, by a person using only pencil and paper

  6. Example 1.2 [Selection sort] • From those integers that are currently unsorted, find the smallest and place it next in the sorted list • Smallest? • for ( i=0; i<n; i++) { • examine list[i] to list[n-1] and suppose that smallest integer is list[min] • interchange list[i] & list[min] }

  7. Selection sort void SelectionSort(int*a, constintn) { //Sort the n integers a[0] to a[n-1] into nondecreasingorder. for (inti= 0 ;i< n ; i ++) { intj = i; //find smallest integer in a[i] to a[n-1] for (intk =i + 1 ;k < n; k++) if (a[k] < a[j]) j = k; swap(a[i], a[j]); } }

  8. Example • Input: 20 10 15 6 17 30 (a[0]~a[5]) • Iteration 1 • Scan from a[0] to a[5] • The smallest one is 6 (a[3]) • Swap a[3] and a[0] • 610 15 20 17 30

  9. Reference: J.L. Huang@NCTU

  10. Example 1.3 [Binary search] intBinarySearch(int *a, constintx,constintn) { //Search the sorted array a[0], …, a[n-1]for x Initialize leftand right; while(there are more elements) { Let middlebe the middle element; if (x < a[middle]) set righttomiddle-1; else if (x > a[middle]) set lefttomiddle+1; else returnmiddle; } Not found; }

  11. C++ function for binary search intBinarySearch(int *a, constintx,constint n) {// Search the sorted array a[0], …, a[n-1] for x intleft = 0, right = n-1 ; while (left <= right) { //there are more elements intmiddle = (left + right)/2; if (x < a[middle]) right=middle-1 ; else if (x > a[middle]) left = middle+1 ; else returnmiddle; } // end of while return -1; // not found }

  12. Example • Input: 1 3 7 9 13 20 31 • Search for 7 • Search for 16

  13. Search for 7 Reference: J.L. Huang@NCTU

  14. Search for 16 Reference: J.L. Huang@NCTU

  15. Recursive algorithms • Recursion is usually used to solve a problem in a “divided-and-conquer” manner • Direct recursion • Functions that call themselves • Indirect recursion • Functions that call other functions that invoke calling function again • Eg. C(n,m) = n!/[m!(n-m)!] • C(n,m)=C(n-1,m)+C(n-1,m-1) • Boundary condition for recursion

  16. Example 1: Summation • sum(1, n)=sum(1, n-1)+n • sum(1, 1)=1 • int sum(int n) { if (n==1) return (1); else return(sum(n-1)+n); }

  17. Example 2: Factorial • n!=n×(n-1)! • fact(n)=n×fact(n-1) • 0!=1 • int fact(int n) { if ( n== 0) return (1); else return (n*fact(n-1)); }

  18. Example 3: Multiplication • a×b=a×(b-1)+a • a×1=a • intmult(int a, int b) { if ( b==1) return (a); else return(mult(a,b-1)+a); }

  19. Example 1.4 [Recursive binary search] intBinarySearch(int *a, constintx,constintleft,constintright) { //Search the sorted array a[left], …, a[right]for x if (left <= right) {intmiddle = (left + right)/2 ; if (x < a[middle]) returnBinarySearch(a, x, left, middle-1) ; else if (x > a[middle]) returnBinarySearch(a, x, middle+1, right) ; else return middle ; } // end ofif return -1 ; //not found }

  20. Example 1.5 [Permutation generator] • Permutation of {a, b, c}: (a, b, c), (a, c, b), (b, a, c), (b, c, a), (c, a, b), (c, b, a) • Permutation of {a, b, c, d}: • a + Perm{b, c, d} • b + Perm{a, c, d} • c + Perm{a, b, d} • d + Perm{a, b, c}

  21. Recursive permutation generator void Permutations (char *a, constintk, constintm) { //Generate all permutations of a[k], …, a[m]. if (k = = m) //output permutation { for (inti =0; i <=m; i++)cout <<a[i] << “ “ ; cout<<endl ; } else // a [k : m] has more than one permutation. Generate these recursively. for (i = k ; i<= m;i++) { swap(a[k], a[i]); Permutations(a, k+1, m) ; swap(a[k], a[i]) ; } }

  22. Performance analysis and measurement • Does it do what we want it to do? • Does it work correctly according to the original specifications of the task? • Is there documentation that describes how to use it and how it works? • Are functions created in such a way that they perform logical subfunctions? • Is the code readable?

  23. Complexity • Space complexity • Amount of memory • Time complexity • Amount of computing time

  24. Space complexity • S(P) = c + Sp(instance characteristics) • P: the program • c: constant (instruction, simple variables, constants) • Sp(instance characteristics): depends on characteristics of the instance

  25. Example 1.8 • float Abc(float a, float b, float c) { return a+b+b*c+(a+b-c)/(a+b)+4.0; } • Sp(instance characteristics)=0

  26. Example 1.9 • float Sum (float *a , constintn) { floats = 0; for(inti = 0;i <n;i++) s += a[i] ; returns; } • Ssum(n)=0

  27. Example 1.10 • floatRsum(float *a , constint n) { if (n <= 0) return 0 ; else return (Rsum(a, n-1) + a[n-1]) ; } • 4(n+1)

  28. Time complexity • T(P) = c + tp(instance characteristics) • P: the program • – c: compile time • – tp(instance characteristics): program execution time

  29. Program steps • Comments: 0 • Declarative statements: 0 • Expressions and assignment statements: • Most expressions have a step count of one • Exceptions: contain function calls

  30. Iteration statements • for(<init-stmt>; <expr1>; <expr2>): one, unless the counts attributable to <init-stmt>, <expr1>, or <expr2> are a function of the instance characteristics • while <expr> do : <expr> • do...while <expr> : <expr> • Switch statement • switch(<expr>){ // cost <expr> case cond1: <statement 1> case cond2: <statement2> … default: <statement> } // plus all preceding conditions

  31. If-else statement • if(<expr>) <statement1>; else <statement2>; // corresponding to <expr>, <statement1>, <statement2> • Function invocation: one, unless the invocation involves pass-by-value parameters whose size depends on the instance characteristics • Memory management statements: 1 • Function statements: 0 (function invocation) • Jump statements: • continue, break,goto, return: 1 • return <expr>: <expr>

  32. Methods to compute the step count • Tabular method • Determine the total number of steps contributed by each statement steps per execution (i.e., s/e) × frequency • Add up the contribution of all statements

  33. Example 1.11 • float Sum (float *a, constintn) { floats = 0; count++ ; // count is global for (inti = 0;i <n;i++) { count++ ; // for for s+= a[i] ; count++ ; // for assignment } count++ ; // for last time of for count++ ; // for return returns ; } • 2n+3 steps

  34. Example 1.12 • floatRsum(float *a , constintn) { count++ ; // for ifcondition if (n <= 0) { count++ ; // for return return 0; } else { count++ ; //for return return(Rsum(a, n-1) + a [n - 1]) ; } } • tRsum(n) =2+tRsum(n-1) =2+2+tRsum(n-2) =2x2+tRsum(n-2) … =2n+tRsum(0) =2n+2

  35. Tabular method float Sum (float *a , constintn) { floats = 0; for(inti = 0;i <n;i++) s += a[i] ; returns; }

  36. Tabular method floatRsum(float *a , constint n) { if (n <= 0) return 0 ; else return (Rsum(a, n-1) + a[n-1]) ; }

  37. Time complexity • Cases • Worst case • Best case • Average case • Worst case and average case analysis is much more useful in practice

  38. Time complexity • Difficult to determine the exact step counts • What a step stands for is inexact • e.g. x = y and x = y + z + (x/y) + (x*y*z-x/z) • Exact step count is not useful for comparison • Step count doesn’t tell how much time step takes • Just consider the growth in run time as the instance characteristics change

  39. Big “oh” • f(n)=O(g(n)) if and only if there exist positive constants c and n0 such that f(n)≤cg(n) for all n, nn0 Reference: J.L. Huang@NCTU

  40. Example 1.15 • 3n+2 =O(n) • 3n+2≤4n for all n≥2 • 10n2+4n+2=O(n2) • 10n2+4n+2≤11n2 for all n≥10 • 3n+2 = O(n2) • 3n+2≤n2 for all n≥4

  41. Omega • f(n)=(g(n)) if and only if there exist positive constants c and n0 such that f(n)cg(n) for all n, nn0 Reference: J.L. Huang@NCTU

  42. Example 1.16 • 3n+3=Ω(n) • 3n+3≥3n for all n≥1 • 6*2n+n2=Ω(2n) • 6*2n+n2≥2n for all n≥1 • 3n+3=Ω(1) • 3n+3≥3 for all n≥1

  43. Theta • f(n)=(g(n)) if and only if there exist positive constants c1, c2, and n0 such that c1g(n)≤f(n)≤c2g(n) for all n, nn0 Reference: J.L. Huang@NCTU

  44. Example 1.17 • 3n+2= Θ(n) • 3n≤3n+2≤4n, for all n≥2 • 10n2+4n+2= Θ(n2) • 10n2≤10n2+4n+2≤11n2, for all n≥5

  45. Some rules • If T1(n)=O(f(n)) and T2(n)=O(g(n)), then • T1(n)+T2(n) = max ( O(f(n)), O(g(n)) ) • T1(n)×T2(n) = O( f(n)×g(n) ) • If T(n) is a polynomial of degree k, then T(n)= Θ(nk)

  46. Example: Sum float Sum (float *a , constintn) { floats = 0; for(inti = 0;i <n;i++) s += a[i] ; returns; }

  47. Example: Rsum floatRsum(float *a , constint n) { if (n <= 0) return 0 ; else return (Rsum(a, n-1) + a[n-1]) ; }

  48. Plot of function values

More Related