Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Algorithm Analysis. 2 Question Suppose you have 2 programs that will sort a list of student records and allow you to search for student information.

Similar presentations


Presentation on theme: "1 Algorithm Analysis. 2 Question Suppose you have 2 programs that will sort a list of student records and allow you to search for student information."— Presentation transcript:

1 1 Algorithm Analysis

2 2 Question Suppose you have 2 programs that will sort a list of student records and allow you to search for student information. Question: How do you judge which is a better program? Answer: By running programs or Algorithm Analysis

3 3 Is a methodology for estimating the resource (time and space) consumption of an algorithm. Allows us to compare the relative costs of two or more algorithms for solving the same problem.

4 4 How to Measure “Betterness” Critical resources in computer Time Memory space For most algorithms, running time depends on size n of data. Notation: T(n) Time T is a function of data size n.

5 5 Two approaches Two approaches to obtaining running time: Measuring under standard benchmark conditions (running programs) Estimating the algorithms performance (analyzing algorithms)

6 6 Estimation Assumptions Estimation of running time is based on: Size of the input Number of basic operations The time to complete a basic operation does not depend on the value of its operands.

7 7 Example: largest() // Return position of largest value // in array int largest(int array[], int n) { int posBig = 0; // 1 for (int i = 1; i < n; i++) if (array[posBig] < array[i]){ // 2 posBig = i; // 3 } return posBig; }

8 8 Example: largest() Step 1: T(n) = c 1 Step 2: It takes a fixed amount of time to do one comparison T(n) = c 2 n Step 3: T(n) = c 3 n, at most Total time: T(n) = c 1 + c 2 n+ c 3 n ≈ c 1 + (c 2 + c 3 )n ≈ cn

9 9 Example: Assignment int a = array[0]; The running time is T(n) = c This is called constant running time.

10 10 Example: total() sum = 0; for (i = 1; i <= n; i++) for (j = 1; j < n; j++) sum++; } What is the running time for this code? Analysis: The basic operation is sum++ The cost of a sum operation can be bundled into some constant time c. Inner loop takes c 1 n; outer loop takes c 2 n. The total running time is T(n) = c 1 n * c 2 n = (c 1 + c 2 )n = c 3 n 2

11 11 Growth Rate of Algorithm —Big-O Natation Growth Rate for an algorithm is the rate at which the cost of the algorithm grows as the data size n grows. Constant: T(n) = c  O(1) Linear Growth: T(n) = n  O(n) Quadratic Growth: T(n) = n 2  O(n 2 ) Exponential Growth: T(n) = 2 n  O(2 n ) Assumptions Growth rates are estimates. They have meaning when n is large.

12 Growth Rate Graph 12

13 13 Growth Rate Graph n T(n) c Log n

14 14 Characteristics of Growth Rates Growth Rates Constant— T(n) = c Independent of n Linear— T(n) = cn Constant slope Logarithmic— T(n) = c log n Slope flattens out quickly Quadratic— T(n) = cn 2 Increasing slope Cubic— T(n) = cn 3 Exponential— T(n) = c2 n

15 Growth Rate—Array Operations 15 bool search(int list[], int n, int key) found = false Loop (for i = 0 to i < n) If (list[i] == key) Then found = true End If End loop return found End // search

16 Growth Rate—Array Operations 16 Linear Search Insert at Back Insert at Front Remove from Back Remove from Font

17 Linear Search 17 bool search(int list[], int n, int key) found = false // c 1 Loop (for i = 0 to i < n) If (list[i] == key) Then // nc 2 found = true (at most) End If End loop return found // c 3 End // search T(n) = c 1 + nc 2 + c 3 = c + nc 2 ≈ nc 2 ∴ O(n)

18 Insert at Back 18 void insertAtBack(int list[], int &n, item) If (!isFull) Then list[n] = item n++ End If End // insertAtBack

19 Insert at Back 19 void insertAtBack(int list[], int &n, item) If (!isFull) Then list[n] = item // c1 n++ // c2 End If End // insertAtBack T(n) = c 1 + c 2 = c ∴ O(1)

20 Insert At Front 20 void insertAtFront(int list[], int &n, int item) If (!isFull) Then // shift to right Loop (for i = n downto 1) list[i] = list[i – 1] End loop // assign list[0] = item count++ End If End // insertAtFront

21 Insert At Front—Growth Rate? 21 void insertAtFront(int list[], int &n, int item) If (!isFull) Then Loop (for i = n downto 1) list[i] = list[i – 1] // c 1 n (at most) End loop list[0] = item // c 2 count++ // c 3 End If End // insertAtFront T(n) = c 1 n + c 2 + c 3 = nc 1 + c ≈ nc 1 ∴ O(n)

22 Remove From Back 22 int removeFromBack(list[], int &n) result = NIL If (!isEmpty) Then result = list[n – 1] n = n - 1 End If return result End // removeFromBack

23 Remove From Front 23 int removeFromFront(list[], int &n) result = NIL If (!isEmpty) Then result = list[0] // shift left Loop (for i = 0 to n – 2) list[i] = list[i + 1] End Loop n = n - 1 End If return result End // removeFromFront

24 Growth Rate —Linked-List Operations 24 Linear Search Insert at Back Insert at Front Remove from Back Remove from Font

25 Linear Search 25 bool linearSearch(int key){ bool found = false; c 1 if (head != NULL){ node* temp = head; c 2 while (temp != NULL){ if (temp=>getData() != key) found = true; else temp = temp->getNext(); c 3 n } } return found; } T(n) = c 1 + c 2 + c 3 n ≈ cn ∴ Growth Rate: O(n)

26 insertAtBack() 26 void insertAtBack(int item){ node *newP = new node(item, NULL); node *temp = head; c 1 if (head == NULL) head = newP; else while (temp->getNext() != NULL){ temp = temp->getNext(); c 2 n } temp->setNext(newP); c 3 count++; c 4 } T(n) = c 1 + c 2 n + c 3 + c 4 ≈ c + c 2 n ∴ G.R. = O(n)

27 insertAtFront() 27 void insertAtFront(int item){ node *newP = new node(item, NULL); c 1 newP->setNext(head); c 2 head = newP; Next(newP); c 3 count++; c 4 } T(n) = c 1 + c 2 + c 3 + c 3 = c ∴ G.R. = O(1)

28 removeFromBack() 28 void removeFromBack(){ node *temp = head; c 1 node *tail = NULL; c 2 while (temp != NULL){ trail = temp; nc 3 temp = temp->getNext(); nc 4 } count++; c 5 } T(n) = c 1 + c 2 + nc 3 + nc 4 + c 5 = c 6 + n(c 3 + c 4 ) ≈ cn ∴ G.R. = O(n)

29 removeFromFront() 29 void removeFromBack(){ node *newP = new node(item, NULL); c 1 newP->setNext(head); c 2 head = newP; Next(newP); c 3 count++; c 4 } T(n) = c 1 + c 2 + c 3 + c 3 = c ∴ G.R. = O(1)

30 30 Best, Worst, Average Cases For an algorithm with a given growth rate, we consider Best case Worst case Average case For example: Sequential search for K in an array of n integers Best case: The first item of the array equals K Worst case: The last position of the array equals K Average case: Match at n/2

31 31 Which Analysis to Use The Worst Case. Useful in many real-time applications. Advantage: Predictability You know for certain that the algorithm must perform at least that well. Disadvantage: Might not be a representative measure of the behavior of the algorithm on inputs of size n.

32 32 Which Analysis to Use? The average case. Often we prefer to know the average-case running time. The average case reveals the typical behavior of the algorithm on inputs of size n. Average case estimation is not always possible. For the sequential search example, it assumes that the key value K is equally likely to appear in any position of the array. This assumption is not always correct.

33 33 Which Analysis to Use The Best Case Normally, we are not interested in the best case, because: It is too optimistic Not a fair characterization of the algorithms’ running time Useful in some rare cases, where the best case has high probability of occurring.

34 34 The moral of the story If we know enough about the distribution of our input we prefer the average-case analysis. If we do not know the distribution, then we must resort to worst-case analysis. For real-time applications, the worst- case analysis is the preferred method.

35 Your Turn (Stack w/array) 35 Given Stack Class: … private: elemType data[MAX]; int top; }

36 Stack (w/array) 36 What is the growth rate (big O) of the push operation? void push(elemType item){ if (!isFull()){ top++; data[top] = item; } }

37 Stack (w/array) 37 What is the growth rate (big O) of the pop operation? elemType pop (){ elemType result = NIL; if (!isEmpty()){ result = data[top]; top--; } }

38 Stack (w/array) 38 What is the growth rate (big O) of the clear operation? void clear(){ top = -1; }

39 Stack (w/linked list) 39 Given: Stack class with linked list … private: Node *top; }

40 Stack (w/linked list) 40 What is the growth rate (big O) of the push operation? void push(elemType item){ Node *temp = new Node(item, NULL); top->setNext(temp); top = temp; }

41 Stack (w/linked list) 41 What is the growth rate (big O) of the pop operation? elemType pop () { elemType result = NIL; Node *temp = top; if (!isEmpty()){ result = top-getNext(); delete temp; } return result; }

42 Stack (w/linked list) 42 What is the growth rate (big O) of the clear operation? Void clear () { Node *temp = top; while (top != NULL) { top = top->getNext(); delete temp; temp = top; } }

43 Growth Rate of Binary Search (w/array) 43 Comparisons Number left 0 n 1 n(1/2) 2 n(1/2) 2 3 n(1/2) 3 4 n(1/2) 4 … k-1 n(1/2) k-1 k n(1/2) k Solution n(1 k ) n n(1/2) k = = ---- = 1 (2 k ) (2 k ) 2 k = n log(2 k ) = log(n) k log(2) = log(n) k = log(n)/log(2) k = c 1 log(n) T(n) = c 1 c 2 log(n)  O(log n) Solution n(1 k ) n n(1/2) k = = ---- = 1 (2 k ) (2 k ) 2 k = n log(2 k ) = log(n) k log(2) = log(n) k = log(n)/log(2) k = c 1 log(n) T(n) = c 1 c 2 log(n)  O(log n)

44 Analysis of Bubble Sort void bubbleSort(int a[], int count) pass  count – 1 last  pass – 1 Loop (for i from 1 to pass) Loop (for j from 0 to last) If (a[j] > a[j + 1]) Then swap a[j] and a[j = 1] End If End Loop last  last – 1 End Loop Suppose: count = 10 maximum pass = 9 pass comparisons

45 Analysis of Bubble Sort Total number of comparisons = ? = ? …………………… = 9x10 (n-1) + (n-2) + (n-3)… …+ (n-3) + (n-2) + (n-1) n + n + n … + n + n + n = n(n-1) Therefore, ? = n(n – 1)/2 = cn 2 – cn ≈ cn 2 Growth Rate: O(n 2 )


Download ppt "1 Algorithm Analysis. 2 Question Suppose you have 2 programs that will sort a list of student records and allow you to search for student information."

Similar presentations


Ads by Google