loop invariants
play

Loop invariants <precondition: n>0> int i = 0; while (i - PDF document

4/23/16 Loop invariants as a way of reasoning about the state of your program Loop invariants <precondition: n>0> int i = 0; while (i < n){ i = i+1; We want to prove: } i==n right after the loop <post condition: i==n>


  1. 4/23/16 Loop invariants as a way of reasoning about the state of your program Loop invariants <precondition: n>0> int i = 0; while (i < n){ i = i+1; We want to prove: } i==n right after the loop <post condition: i==n> Loop invariants: Section 5.5 in Rosen The programmer’s wife tells him “Go to the store and pick up a loaf of bread. If they have eggs, get a dozen.” The programmer comes back with 12 loaves of bread . Example: loop index value after loop Loop invariants // precondition: n>=0 n A way to reason about the correctness of a int i = 0; program // i<=n loop invariant while (i < n){ n A loop invariant is a predicate // i < n test passed q that is true directly before the loop executes // AND So we can conclude the obvious: q that is true before and after each repetition of the // i<=n loop invariant loop body i++; i==n right after the loop // i <= n loop invariant q and that is true directly after the loop has } executed // i>=n AND i <= n à i==n i.e., it is kept invariant by the loop. 1

  2. 4/23/16 Loop invariants What does it mean... n Combined with the loop condition, the loop <loop invariant> If we can prove that invariant allows us to reason about the behavior while(test){ the loop invariant holds before the loop and that <test AND of the loop: the loop body keeps the loop invariant true loop invariant> <loop invariant> i.e. <test AND loop invariant> S; <loop invariant> S; while(test){ <loop then we can infer that invariant> <test AND loop invariant> } not test AND loop invariant S; <not test AND holds after the loop terminates <loop invariant> loop invariant> } < not test AND loop invariant > Example: sum of elements in an array Loop Invariant for Selection Sort public void selectionSort (Comparable [] array){ int total (int[] elements){ int min; int sum = 0, i = 0, n = elements.length; for (int i = 0; i < array.length-1; i++) { // sum == sum of elements from 0 to i-1 min = i; while (i < n){ for (int j = i+1; j < array.length; j++){ // sum == sum of elements 0...i-1 if (array[j].compareTo(array[min]) < 0) sum += elements [i]; min = j; i++; } // sum == sum of elements 0...i-1 swap (array, min, i); } } } // i==n (previous example) AND // sum == sum elements 0...i-1 // à sum == sum of elements 0...n-1 Invariant? return sum; } 2

  3. 4/23/16 Loop Invariant for Selection Sort Loop Invariant for Insertion Sort public void selectionSort (Comparable [] array){ public void insertionSort(Comparable[] array) { int min; for (int i = 1; i < array.length; i++) { for (int i = 0; i < array.length-1; i++) { Comparable temp = array[i]; int position = i; min = i; while (position > 0 && for (int j = i+1; j < array.length; j++){ array[position-1].compareTo(temp) > 0) { if (array[j].compareTo(array[min]) < 0) array[position] = array[position–1]; min = j; position--; } } swap (array, min, i); array[position] = temp; } } } } Invariant : array[0] … array[i-1] consists of elements originally in Invariant: array[0] … array[i] are in sorted order this range of indexes, but in sorted order Bubble Sort Bubble Sort public void bubbleSort (Comparable [] array) { public void bubbleSort (Comparable [] array) { for (int position = array.length-1; position>=0; for (int position = array.length-1; position>=0; position--) { position--) { for (int i = 0 ; i < position; i++) { for (int i = 0 ; i < position; i++) { if (array[i].compareTo(array[i+1]) > 0) if (array[i].compareTo(array[i+1]) > 0) swap(array, i, i+1); swap(array, i, i+1); } } } } } } Inner Invariant: array[i] is the largest element in the Invariant? first i elements in the array Outer Invariant: After i iterations the largest i elements are in their correct sorted position 3

  4. 4/23/16 Closed Curve Game Closed Curve Game n There are two players, Red and Blue. The game is n We can express this game as a computer played on a rectangular grid of points: program: 6 . . . . . . . while (more line segments can be drawn) { 5 . . . . . . . 4 . . . . . . . Red draws line segment; 3 . . . . . . . Blue draws line segment; 2 . . . . . . . 1 . . . . . . . } 1 2 3 4 5 6 7 Red draws a red line segment, either horizontal or vertical, connecting Question: Does either Red or Blue have a winning any two adjacent points on the grid that are not yet connected by a line strategy? segment. Blue takes a turn by doing the same thing, except that the line segment drawn is blue. Red's goal is to form a closed curve of red line segments. Blue's goal is to prevent Red from doing so. See http://www.cs.uofs.edu/~mccloske/courses/cmps144/invariants_lec.html Closed Curve Game Closed Curve Game n Answer: Yes! Blue is guaranteed to win the game by responding to n By following this strategy Blue guarantees that Red does each turn by Red in the following manner: not have an “upper right corner” at any step. if (Red drew a horizontal line segment) { n So, the invariant is: let i and j be such that Red's line segment connects (i,j) with (i,j+1) There does not exist on the grid a pair of red line if (i>1) { draw a vertical line segment connecting (i-1,j+1) with (i,j+1) segments that form an upper right corner. } else { draw a line segment anywhere } And in particular, Red has no closed curve! } else // Red drew a vertical line segment let i and j be such that Red's line segment connects (i,j) with (i+1,j) if (j>1) { draw a horizontal line segment connecting (i+1,j-1) with (i+1,j) } else { draw a line segment anywhere } } 4

  5. 4/23/16 Example: Egyptian multiplication Can we show it works? Loop invariants!! // precondition: left >0 AND right >0 A B 19 5 int a=left, b=right, p=0; //p: the product 19 x 5: /2 9 10 *2 // p + (a*b) == left * right loop invariant /2 4 20 *2 while (a!=0){ /2 2 40 *2 // a!=0 and p + (a*b) == left * right /2 1 80 *2 // loop condition and loop invariant throw away all rows with even A: if (odd(a)) p+=b; A B a/=2; 19 5 b*=2; 9 10 // p + (a*b) == left*right 1 80 __________ } add B's 95 // a==0 and p+a*b == left*right à p == left*right --> the product !! Try it on 7 * 8 Try it on 8*7 left right a b p left right a b p 8 7 8 7 0 7 8 7 8 0 4 14 0 3 16 +=b: 8 2 28 0 1 32 +=b: 24 1 56 0 0 64 +=b: 56 0 118 +=b: 56 5

  6. 4/23/16 Relation to binary representation 19*5 Incorporating loop invariants into your code 00101 n An assertion is a statement that says something about the state of your program 10011 n Should be true if there are no mistakes in the program ______ n Can be used to check that a loop invariant holds true 101 5 1010 10 Example: 00000 int p=…,d=…; 000000 int q = p/d; 1010000 80 int r = p%d; _______ assert p == q*d + r; 1011111 95 Example Using assertions // precondition: left >0 AND right >0 n Assertions may slow down execution. For example, if an int a=left, b=right, p=0; //p: the product assertion checks to see if the element to be returned is assert (p + (a*b) == left * right); // loop invariant the smallest element in the list, then the assertion would while (a!=0){ have to do the same amount of work that the method assert (p + (a*b) == left * right); would have to do if (odd(a)) p+=b; n Therefore assertions can be enabled and disabled a/=2; n Assertions are, by default, disabled at run-time b*=2; n Think of assertions as a debugging tool assert(p + (a*b) == left*right); } n Don’t use assertions to flag user errors, because assert (p + (a*b) == left * right); assertions can be turned off // a==0 and p+a*b == left*right à p == left*right 6

  7. 4/23/16 Enabling assertions Summary: Loop Invariant Reasoning n Need to set a compiler flag. //loop invariant true before loop while (b){ n Go to Run -> Run Configurations -> Arguments, // b AND loop invariant and in the box labeled VM arguments, enter S; either -enableassertions or just -ea // loop invariant } // not b AND loop invariant loop invariants as a way of proving correctness of loop- based algorithms 7

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