Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)

Similar presentations


Presentation on theme: "Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)"— Presentation transcript:

1 Chapter 1 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 2

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 3

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 4

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 5

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] } 6

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

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] 6 10 15 20 17 30 8

9 9 Reference: J.L. Huang@NCTU

10 Example 1.3 [Binary search] int BinarySearch (int *a, const int x, const int n) { // Search the sorted array a[0], …, a[n-1] for x Initialize left and right; while ( there are more elements ) { Let middle be the middle element; if (x < a[middle]) set right to middle-1 ; else if (x > a[middle]) set left to middle+1 ; else return middle ; } Not found; } 10

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

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

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

14 Search for 16 14 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 15

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); } 16

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)); } 17

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

19 Example 1.4 [Recursive binary search] int BinarySearch(int *a, const int x, const int left, const int right) { // Search the sorted array a[left], …, a[right] for x if (left <= right) { int middle = (left + right)/2 ; if (x < a[middle]) return BinarySearch(a, x, left, middle-1) ; else if (x > a[middle]) return BinarySearch(a, x, middle+1, right) ; else return middle ; } // end of if return -1 ; // not found } 19

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} 20

21 Recursive permutation generator void Permutations (char *a, const int k, const int m) { // Generate all permutations of a[k], …, a[m]. if (k = = m) // output permutation { for (int i =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]) ; } 21

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? 22

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

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

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

26 Example 1.9 float Sum (float *a, const int n) { float s = 0; for(int i = 0; i <n ; i++) s += a[i] ; return s; } S sum (n)=0 26

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

28 Time complexity T(P) = c + t p (instance characteristics) P: the program – c: compile time – t p (instance characteristics): program execution time 28

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

30 Iteration statements for( ; ; ): one, unless the counts attributable to,, or are a function of the instance characteristics while do : do...while : Switch statement switch( ){// cost case cond1: case cond2: … default: }// plus all preceding conditions 30

31 If-else statement if( ) ; else ; // corresponding to,, 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 : 31

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 32

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

34 Example 1.12 float Rsum (float *a, const int n) { count++ ; // for if condition if (n <= 0) { count++ ; // for return return 0; } else { count++ ; // for return return (Rsum (a, n-1) + a [n - 1]) ; } t Rsum (n) =2+t Rsum (n-1) =2+2+t Rsum (n-2) =2x2+t Rsum (n-2) … =2n+t Rsum (0) =2n+2 34

35 Tabular method 35 lines/efrequency total steps 123456123456 011110011110 1 n+1 n 1 0 1 n+1 n 1 0 Total number of steps 2n + 3 float Sum (float *a, const int n) { float s = 0; for(int i = 0; i <n ; i++) s += a[i] ; return s; }

36 Tabular method float Rsum(float *a, const int n) { if (n <= 0) return 0 ; else return (Rsum (a, n-1) + a[n-1]) ; } 36 lines/e frequency n = 0 n > 0 total steps n = 0 n > 0 1 2(a) 2(b) 3 4 0 1 1+t Rsum (n-1) 0 1110111101 1101111011 0110001100 1 0 1+ t Rsum (n-1) 0 Total number of steps22 + t Rsum (n-1)

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

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 38

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

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

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

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

43 Theta f(n)=  (g(n)) if and only if there exist positive constants c 1, c 2, and n 0 such that c 1 g(n) ≤ f(n) ≤ c 2 g(n) for all n, n  n 0 43 Reference: J.L. Huang@NCTU

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

45 Some rules If T 1 (n)=O(f(n)) and T 2 (n)=O(g(n)), then T 1 (n)+T 2 (n) = max ( O(f(n)), O(g(n)) ) T 1 (n)×T 2 (n) = O( f(n)×g(n) ) If T(n) is a polynomial of degree k, then T(n)= Θ(n k ) 45

46 Example: Sum float Sum (float *a, const int n) { float s = 0; for(int i = 0; i <n ; i++) s += a[i] ; return s; } lines/efrequencytotal steps 123456123456 011110011110 - 1 n+1 n 1 -  (0)  (1)  (n)  (1)  (0) t Sum (n) =  (max 1  i  6 {g(n)}) =  (n) 46

47 Example: Rsum float Rsum(float *a, const int n) { if (n <= 0) return 0 ; else return (Rsum (a, n-1) + a[n-1]) ; } 47 lines/efrequency n = 0 n > 0 total steps n = 0 n > 0 1 2(a) 2(b) 3 4 0 1 2+t Rsum (n-1) 0 - 1 1 0 0 1 - 0  (0) 1  (1) 1  (0) 0  (2+t Rsum (n-1)) 0  (0) t Rsum (n) = 2  (2 + t Rsum (n-1))

48 Plot of function values 48


Download ppt "Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)"

Similar presentations


Ads by Google