Loop Invariants and Binary Search Techniques

loop invariants and binary search l.w
1 / 52
Embed
Share

Learn about loop invariants, binary search, iterative algorithms, assertions, and proofs of correctness. Understand the importance of maintaining loop invariants and establishing safe locations during computations.

  • Loop Invariants
  • Binary Search
  • Iterative Algorithms
  • Assertions
  • Proofs

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Loop Invariants and Binary Search Chapter 4.4, 5.1

  2. Outline Iterative Algorithms, Assertions and Proofs of Correctness Binary Search: A Case Study

  3. Outline Iterative Algorithms, Assertions and Proofs of Correctness Binary Search: A Case Study

  4. Assertions An assertion is a statement about the state of the data at a specified point in your algorithm. An assertion is not a task for the algorithm to perform. You may think of it as a comment that is added for the benefit of the reader.

  5. Loop Invariants Binary search can be implemented as an iterative algorithm (it could also be done recursively). Loop Invariant: An assertion about the current state useful for designing, analyzing and proving the correctness of iterative algorithms.

  6. Other Examples of Assertions Preconditions: Any assumptions that must be true about the input instance. Postconditions: The statement of what must be true when the algorithm/program returns. Exit condition: The statement of what must be true to exit a loop.

  7. Iterative Algorithms Take one step at a time towards the final destination loop (done) take step end loop

  8. Establishing Loop Invariant From the Pre-Conditions on the input instance we must establish the loop invariant.

  9. Maintain Loop Invariant Suppose that We start in a safe location (pre-condition) If we are in a safe location, we always step to another safe location (loop invariant) Can we be assured that the computation will always be in a safe location? By what principle?

  10. Maintain Loop Invariant By Induction the computation will always be in a safe location. S (0) , ( ) i S i + , ( ) i S i S i ( 1)

  11. Ending The Algorithm Define Exit Condition Exit Termination: With sufficient progress, 0 km Exit the exit condition will be met. When we exit, we know exit condition is true loop invariant is true from these we must establish Exit the post conditions.

  12. Definition of Correctness <PreCond> & <code> <PostCond> If the input meets the preconditions, then the output must meet the postconditions. If the input does not meet the preconditions, then nothing is required.

  13. Outline Iterative Algorithms, Assertions and Proofs of Correctness Binary Search: A Case Study

  14. Define Problem: Binary Search PreConditions Key 25 Sorted List 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 PostConditions Find key in list (if there). 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

  15. Define Loop Invariant Maintain a sublist. If the key is contained in the original list, then the key is contained in the sublist. key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

  16. Define Step Cut sublist in half. Determine which half the key would be in. Keep that half. mid key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key mid, then key is in left half. If key > mid, then key is in right half.

  17. Define Step It is faster not to check if the middle element is the key. Simply continue. key 43 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key mid, then key is in left half. If key > mid, then key is in right half.

  18. Make Progress The size of the list becomes smaller. 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 79 km 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 75 km

  19. Exit Condition key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 0 km If the key is contained in the original list, If element = key, return associated entry. Otherwise return false. then the key is contained in the sublist. Sublist contains one element. Exit

  20. Running Time The sublist is of size n, n/2, n/4, n/8, ,1 Each step O(1) time. Total =O(log n) key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key mid, then key is in left half. If key > mid, then key is in right half.

  21. Running Time Binary search can interact poorly with the memory hierarchy (i.e. caching), because of its random-access nature. It is common to abandon binary searching for linear searching as soon as the size of the remaining span falls below a small value such as 8 or 16 or even more in recent computers.

  22. BinarySea rch(A[1..n], k y e ) <precondition>: A[1..n] is sorted in non-decreasing order <postcondition>: If is in A[1..n], algorithm returns 1, whil e key p q mid = key its location p q q n = = p loop-invariant>: If + is in A[1..n], then key is in A[p.. q] 2 A m if key [ id ] q mid = els e p mi d 1 = + end end if key A p [ ] p = return( ) lse e return("Key n end ot in list")

  23. Simple, right? Although the concept is simple, binary search is notoriously easy to get wrong. Why is this?

  24. Boundary Conditions The basic idea behind binary search is easy to grasp. It is then easy to write pseudocode that works for a typical case. Unfortunately, it is equally easy to write pseudocode that fails on the boundary conditions.

  25. Boundary Conditions if key A mid [ ] if key A mid [ ] q mid q mid = = else else or p mid 1 p mid 1 = + = + end end What condition will break the loop invariant?

  26. Boundary Conditions mid key 36 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 C od e : k ey A[m id] s e le c t rig h t ha lf Bug!!

  27. Boundary Conditions if key A mid < [ mid = ] if key A mid [ ] if key A mid [ ] q 1 q mid q mid = = else else else p mid p mid 1 p mid 1 = = + = + end end end OK OK Not OK!!

  28. Boundary Conditions + + p q p q = = mid mid or 2 2 key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 p q + Select mid = Shouldn t matter, right? 2

  29. Boundary Conditions p q + Select mid = 2 mid key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key mid, then key is in left half. If key > mid, then key is in right half.

  30. Boundary Conditions p q + Select mid = 2 mid key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key > mid, then key is in right half. If key mid, then key is in left half.

  31. Boundary Conditions No progress toward goal: Loops Forever! Another bug! p q + Select mid = 2 mid key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key > mid, then key is in right half. If key mid, then key is in left half.

  32. Boundary Conditions p q p q p q + + + mid mid mid = = = 2 2 A mi mid 2 A mi mid if key A mid q < [ mid = ] if key q [ d ] if key q [ d ] 1 = = else else else p mid p mid 1 p mid 1 = = + = + end end end OK OK Not OK!!

  33. Getting it Right How many possible algorithms? p q + mid r o ? = p q + 2 mid = 2 A mi mid How many correct algorithms? or if key A mid < [ ] ? if key q [ d ] = else Probability of guessing correctly? p mid 1 = + o r q mid 1 = end else p mid = end

  34. Alternative Algorithm: Less Efficient but More Clear BinarySearch(A[1..n],key) <precondition>: A[1..n] is sorted in non-decreasing order <postcondition>: If key is in A[1..n], algorithm returns its location p = 1, q = n while q p <loop-invariant>: If key is in A[1..n], then key is in A[p..q] p +q 2 if key <A[mid] q = mid -1 else if key > A[mid] p = mid +1 else return(mid) end end return("Key not in list") mid = Still (log ), but with slightly larger constant. n

  35. Card Trick A volunteer, please.

  36. Pick a Card Done Thanks to J. Edmonds for this example.

  37. Loop Invariant: The selected card is one of these.

  38. Which column? left

  39. Loop Invariant: The selected card is one of these.

  40. Selected column is placed in the middle

  41. I will rearrange the cards

  42. Relax Loop Invariant: I will remember the same about each column.

  43. Which column? right

  44. Loop Invariant: The selected card is one of these.

  45. Selected column is placed in the middle

  46. I will rearrange the cards

  47. Which column? left

  48. Loop Invariant: The selected card is one of these.

  49. Selected column is placed in the middle

  50. Here is your card. Wow!

More Related Content