analysis of algorithms
play

Analysis of Algorithms Amr Magdy Analyzing Algorithms Algorithm - PowerPoint PPT Presentation

CS141: Intermediate Data Structures and Algorithms Analysis of Algorithms Amr Magdy Analyzing Algorithms Algorithm Correctness 1. Termination a. Produces the correct output for all possible input. b. Algorithm Performance 2. Either


  1. CS141: Intermediate Data Structures and Algorithms Analysis of Algorithms Amr Magdy

  2. Analyzing Algorithms Algorithm Correctness 1. Termination a. Produces the correct output for all possible input. b. Algorithm Performance 2. Either runtime analysis, a. or storage (memory) space analysis b. or both c. 2

  3. Algorithm Correctness Sorting problem Input: an array A of n numbers Output: the same array in ascending sorted order (smallest number in A[1] and largest in A[n]) 3

  4. Algorithm Correctness Sorting problem Input: an array A of n numbers Output: the same array in ascending sorted order (smallest number in A[1] and largest in A[n]) Insertion Sort 4

  5. Algorithm Correctness How does insertion sort work? 5

  6. Algorithm Correctness 5 2 4 6 1 3 6

  7. Algorithm Correctness 5 2 4 6 1 3 7

  8. Algorithm Correctness 5 2 4 6 1 3 8

  9. Algorithm Correctness 5 2 4 6 1 3 9

  10. Algorithm Correctness 5 2 4 6 1 3 10

  11. Algorithm Correctness 5 2 4 6 1 3 11

  12. Algorithm Correctness 5 2 4 6 1 3 12

  13. Algorithm Correctness Is insertion sort a correct algorithm? 13

  14. Algorithm Correctness Is insertion sort a correct algorithm? Does it halt? Does it produce correct output for all possible input? 14

  15. Algorithm Correctness Is insertion sort a correct algorithm? Does it halt? Yes Two deterministically bounded loops, no infinite loops involved Does it produce correct output for all possible input? 15

  16. Algorithm Correctness Is insertion sort a correct algorithm? Does it halt? Yes Two deterministically bounded loops, no infinite loops involved Does it produce correct output for all possible input? 16

  17. Algorithm Correctness Is insertion sort a correct algorithm? Does it halt? Yes Does it produce correct output for all possible input? Will check through loop invariants for insertion sort For other algorithms, we can use any systematic logic/steps to show that, either loop invariants or other methods 17

  18. Algorithm Correctness Is insertion sort a correct algorithm? Loop invariant: It is a property that is true before and after each loop iteration. 18

  19. Algorithm Correctness Is insertion sort a correct algorithm? Loop invariant: It is a property that is true before and after each loop iteration. Insertion sort loop invariant (ISLI): The first (j-1) array elements A[1..j-1] are: (a) the original (j-1) elements, and (b) sorted. 19

  20. Algorithm Correctness Is insertion sort a correct algorithm? If ISLI correct, then insertion sort is correct How? Halts and produces the correct output after (n-1) iterations 20

  21. Algorithm Correctness Is insertion sort a correct algorithm? If ISLI correct, then insertion sort is correct How? Halts and produces the correct output after (n-1) iterations Loop invariant (LI) correctness 1. Initialization: LI is true prior to the 1 st iteration. 2. Maintenance: If LI true before the iteration, it remains true before the next iteration 3. Termination: After the loop terminates, the output is correct. 21

  22. Participation Exercise 22

  23. Algorithm Correctness ISLI: The first (j-1) array elements A[1..j-1] are: (a) the original (j-1) elements, and (b) sorted. 1. Initialization: Prior to the 1 st iteration, j=2, the first (2-1)=1 elements is sorted. 2. Maintenance: The (j-1) th iteration inserts the j th element in a sorted order, so after the iteration, the first (j-1) elements remains the same and sorted. 3. Termination: The loop terminates after (n-1) iterations, j=n+1, so the first n elements are sorted, then the output is correct. 23

  24. Algorithm Correctness ISLI: The first (j-1) array elements A[1..j-1] are: (a) the original (j-1) elements, and (b) sorted. 1. Initialization: Prior to the 1 st iteration, j=2, the first (2-1)=1 elements is sorted. 2. Maintenance: The (j-1) th iteration inserts the j th element in a sorted order, so after the iteration, the first (j-1) elements remains the same and sorted. 3. Termination: The loop terminates after (n-1) iterations, j=n+1, so the first n elements are sorted, then the output is correct. 24

  25. Analyzing Algorithms Algorithm Correctness 1. Termination a. Produces the correct output for all possible input. b. Algorithm Performance 2. Either runtime analysis, a. or storage (memory) space analysis b. or both c. 25

  26. Algorithms Performance Analysis Which criteria should be taken into account? Running time Memory footprint Disk IO Network bandwidth Power consumption Lines of codes … 26

  27. Algorithms Performance Analysis Which criteria should be taken into account? Running time Memory footprint Disk IO Network bandwidth Power consumption Lines of codes … 27

  28. Average Case vs. Worst Case 28

  29. Insertion Sort Best Case 29

  30. Insertion Sort Best Case Input array is sorted 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 30

  31. Insertion Sort Best Case Input array is sorted 1 2 3 4 5 6 ……………………………………….………c1 1 2 3 4 5 6 ……………………………………….………c2 ……….0 ……………………………………….………c3 1 2 3 4 5 6 (n-1) ……………………….c4 1 2 3 4 5 6 do not execute ………. 0 1 ……………………………………….…c5 1 2 3 4 5 6 1 2 3 4 5 6 31

  32. Insertion Sort Best Case Input array is sorted 1 2 3 4 5 6 ……………………………………….………c1 1 2 3 4 5 6 ……………………………………….………c2 ……….0 ……………………………………….………c3 1 2 3 4 5 6 (n-1) ……………………….c4 1 2 3 4 5 6 do not execute ………. 0 1 ……………………………………….…c5 1 2 3 4 5 6 T(n) = (n-1)*(c1+c2+0+c3+1*(c4+0)+c5) 1 2 3 4 5 6 T(n) = cn-c, const c=c1+c2+c3+c4+c5 32

  33. Insertion Sort Worst Case 33

  34. Insertion Sort Worst Case Input array is reversed 6 5 4 3 2 1 5 6 4 3 2 1 4 5 6 3 2 1 3 4 5 6 2 1 2 3 4 5 6 1 1 2 3 4 5 6 34

  35. Insertion Sort Worst Case Input array is reversed 6 5 4 3 2 1 ……………………………………….………c1 5 6 4 3 2 1 ……………………………………….………c2 ……….0 ……………………………………….………c3 4 5 6 3 2 1 (n-1) ……………………….c4 ……….…………………....c5 3 4 5 6 2 1 i ……….………………….……....c6 ……………………………………….…c7 2 3 4 5 6 1 1 2 3 4 5 6 35

  36. Insertion Sort Worst Case Input array is reversed 6 5 4 3 2 1 ……………………………………….………c1 5 6 4 3 2 1 ……………………………………….………c2 ……….0 ……………………………………….………c3 4 5 6 3 2 1 (n-1) ……………………….c4 ……….…………………....c5 3 4 5 6 2 1 i ……….………………….……....c6 ……………………………………….…c7 2 3 4 5 6 1 T(n) = (n-1)*(c1+c2+0+c3+i*(c4+c5+c6)+c7) T(n) = (n- 1)*(c1+c2+0+c3+c7) + ∑ i*(c4+c5+c6), for all 1 <= i < n 1 2 3 4 5 6 T(n) = (cn- c) + ∑ i*d, c & d are constants ∑ i *d = 1*d+2*d+3*d+….+(n - 1)*d= d *(1+2+3+…(n -1))= d*n(n-1)/2 c’s & d are consts T(n) = (cn-c) + dn 2 /2-dn/2 = d*n 2 +c11*n+c12, 36

  37. Insertion Sort Average Case Average = (Best + Worst)/2 T(n) = cn 2 +dn+e, c, d, e are consts 37

  38. Which case we consider? 38

  39. Which case we consider? The worst case 39

  40. Which case we consider? The worst case Why? 40

  41. Which case we consider? The worst case Why? It gives guarantees on the upper bound performance 41

  42. Growth of Functions It is hard to compute the actual running time for more complex algorithms The cost of the worst-case is a good measure The growth of the cost function is what interests us (when input size is large) We are more concerned with comparing two cost functions, i.e., two algorithms. 42

  43. Growth of Functions 43

  44. O-notation 44

  45. Ω -notation 45

  46. Θ -notation 46

  47. o-notation 47

  48. ω -notation 48

  49. Comparing Two Functions 𝑔 𝑜 𝑚𝑗𝑛 𝑜→∞ 𝑕 𝑜 0: f(n) = o(g(n)) f(n) = Θ (g(n)) c > 0: f(n) = ω(g(n)) ∞ : 49

  50. Analogy to Real Numbers 50

  51. Simple Rules We can omit constants We can omit lower order terms Θ ( 𝑏𝑜 2 + 𝑐𝑜 + 𝑑 ) becomes Θ ( 𝑜 2 ), a, b, c are constants Θ ( 𝑑 1) and Θ ( 𝑑 2) become Θ (1 ), c’s are constants Θ (log 𝑙 1 𝑜 ) and Θ (log 𝑙 2 𝑜 ) become Θ (log 𝑜 ), k’s are constants Θ( log( 𝑜 𝑙 )) becomes Θ (log 𝑜 ), k is constant 51

  52. Popular Classes of Functions 52

  53. Insertion Sort Worst Case (Revisit) Input array is reversed 6 5 4 3 2 1 5 6 4 3 2 1 4 5 6 3 2 1 (n-1) 3 4 5 6 2 1 max n 2 3 4 5 6 1 T(n) = (n-1)*n = O(n 2 ) 1 2 3 4 5 6 53

  54. Comparing two algorithms T1(n) = 2n+1000000 T2(n) = 200n + 1000 Which is better? Why? In terms of order of growth? 54

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend