Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Daa

678 views

Published on

Published in: Technology
  • Be the first to comment

Daa

  1. 1. Algorithm Correctness & Analysis
  2. 2. Summary <ul><li>Confidence in algorithms from testing and correctness proof </li></ul><ul><li>Correctness of recursive algorithms proved directly by induction </li></ul><ul><li>Examples: Fibonacci numbers, maximum, multiplication </li></ul>
  3. 3. Correctness <ul><li>How do we know that an algorithm works? </li></ul><ul><li>Logical method for checking correctness </li></ul><ul><ul><li>Testing </li></ul></ul><ul><ul><li>Correctness proof </li></ul></ul><ul><li>Testing vs. Correctness Proofs </li></ul><ul><li>Testing: try the algorithm on sample inputs </li></ul><ul><li>Correctness Proof: Prove mathematically; testing may not found obscure bugs </li></ul><ul><ul><li>Using testing alone can be dangerous </li></ul></ul>
  4. 4. Correctness of Recursive algorithms <ul><li>To prove correctness of recursive algorithm: </li></ul><ul><ul><li>Prove it by induction on the size of the problem being solved </li></ul></ul><ul><ul><li>Base of recursion is base of induction </li></ul></ul><ul><ul><li>Need to prove the recursive calls are given sub-problems, i.e., no infinite recursion </li></ul></ul><ul><ul><li>Inductive step: assume that recursive calls work correctly, and use this assumption to prove that the current call works correctly </li></ul></ul>
  5. 5. Recursive Fibonacci Numbers <ul><li>Fibonacci numbers: F 0 = 0, F 1 =1, and for all n ≥ 2, </li></ul><ul><ul><li>F n = F n-2 + F n-1 </li></ul></ul><ul><ul><li>function fib(n) </li></ul></ul><ul><ul><li>Comment return F n </li></ul></ul><ul><ul><li>If n ≤ 1 then return ( n ) </li></ul></ul><ul><ul><li>else return (fib( n-1 ) + fib ( n-2 )) </li></ul></ul>
  6. 6. Recursive Fibonacci Numbers Claim: For all n ≥ 0 , fib( n ) return F n Base: For n =0 , fib( n ) returns 0 as claimed. For n = 1 , fib( n ) returns 1 as claimed. Induction: Suppose that n ≥ 2 and for all 0 ≤ m < n, fib ( m ) returns F m . Required to prove fib( n ) returns F n What does fib( n ) returns? fib( n-1 ) + fib( n-2 ) = F n-1 + F n-2 (by Ind. Hyp.) = F n
  7. 7. Recursive Maximum
  8. 8. Recursive maximum
  9. 9. Recursive Multiplication
  10. 10. Recursive Multiplication
  11. 11. Recursive Multiplication
  12. 12. Analysis
  13. 13. Summary
  14. 14. <ul><li>Analyze the resource usage of an algorithm to within a constant multiple. </li></ul><ul><li>Why ? Because other constant multiples creep in when translating </li></ul><ul><li>from an algorithm to executable code: </li></ul><ul><li>Programmer ability </li></ul><ul><li>Programmer effectiveness </li></ul><ul><li>Compiler </li></ul><ul><li>Computer hardware </li></ul><ul><li>Recall: Measure resource usage as a function of input size </li></ul>Constant Multiples
  15. 15. Big oh
  16. 16. <ul><li>Most big- O s can be proved by induction. </li></ul><ul><li>First Example: log n = O(n). </li></ul><ul><li>Claim : For all n > 1, log n < n . The proof is by induction on n . </li></ul><ul><li>The claim is trivially for n= 1,since 0<1. Now suppose n > 1 and </li></ul><ul><li>log n < n . Then, </li></ul><ul><li>log (n+1) </li></ul><ul><li>< log 2n </li></ul><ul><li>= log n+1 </li></ul><ul><li>< n+1 (by ind. hyp.) </li></ul>Example
  17. 17. Note that we need Second example
  18. 18. Big Omega
  19. 19. Big Theta
  20. 20. True or false
  21. 21. Adding Big Ohs
  22. 22. Continue…
  23. 23. Multiplying Big Ohs
  24. 24. Types of Analysis
  25. 25. Example…
  26. 26. Time Complexity
  27. 27. Multiplication
  28. 28. Bubble sort
  29. 29. Analysis Trick
  30. 30. Example
  31. 31. Lies, Damn Lies, and Big-Os
  32. 32. Algorithms and Problems
  33. 33. Algorithms Analysis 2
  34. 34. The Heap
  35. 35. Contd…
  36. 36. But we have lost the tree structure To Delete the Minimum
  37. 37. But we have violated the structure condition Contd…
  38. 38. Contd…
  39. 39. Contd…
  40. 40. What Does it work?
  41. 41. Contd…
  42. 42. Contd…
  43. 43. To Insert a New Element
  44. 44. Contd…
  45. 45. Contd…
  46. 46. Contd…
  47. 47. Why Does it Work
  48. 48. Contd…
  49. 49. Implementing a Heap
  50. 50. Analysis of Priority Queue Operation
  51. 51. Analysis of l (n)
  52. 52. Contd…
  53. 53. Example
  54. 54. Heapsort
  55. 55. Building a Heap Top Down
  56. 56. Contd…
  57. 57. Contd…
  58. 58. Building a Heap Bottom Up
  59. 59. Continue…
  60. 60. Continue…
  61. 61. <ul><li>summary </li></ul><ul><li>Analysis of recursive algorithms: </li></ul><ul><li>Recurrence relations </li></ul><ul><li>How to derive them </li></ul><ul><li>How to solve them </li></ul>Algorithms Course Notes Algorithm Analysis 3
  62. 62. Deriving Recurrence Relations
  63. 63. Continue…
  64. 64. Example
  65. 65. Continue…
  66. 66. Analysis of Multiplication
  67. 67. Solving Recurrence Relations
  68. 68. The Multiplication Example
  69. 69. Repeated Substitution
  70. 70. Warning
  71. 71. Reality Check
  72. 72. Merge Sorting
  73. 73. Continue…
  74. 74. Continue…
  75. 75. A General Theorem
  76. 76. Proof Sketch
  77. 77. Continue…
  78. 78. Geometric Progressions
  79. 79. Back to the Proof
  80. 80. Continue…
  81. 81. Messy Details
  82. 82. Continue…
  83. 83. Example

×