CS Lecture 19: Loop invariants

Similar documents
CS 1110: Introduction to Computing Using Python Loop Invariants

Lecture 24: Loop Invariants

isinstance and While Loops

Lecture 22. While Loops

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

ASYMPTOTIC COMPLEXITY

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

Iteration and For Loops

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

Lecture 11: Iteration and For-Loops

Lecture 20: While Loops (Sections 7.3, 7.4)

ASYMPTOTIC COMPLEXITY

CS 1110: Introduction to Computing Using Python Lists and Sequences

CS 1110: WORKED EXAMPLES REGARDING LOOPS AND INVARIANTS

CS 1110, LAB 13: SEQUENCE ALGORITHMS

CORRECTNESS ISSUES AND LOOP INVARIANTS

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

CS1110. Lecture 22: Prelim 2 Review Session. Announcements. Processed prelim regrade requests: on the front table.

CS Prelim 2 Review Fall 2018

CS1110. Lecture 6: Function calls

CS Prelim 1 Review Fall 2018

Lecture 10: Lists and Sequences

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

Lecture 13. For-Loops

Lecture 19. Operators and Abstraction

CS1110. Lecture 6: Function calls

Lecture 15. For-Loops

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

RECURSION (CONTINUED)

Lecture 19: Subclasses & Inheritance (Chapter 18)

Lecture 13. For-Loops

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

CS 1110 Prelim 1 October 4th, 2012

CS 1110 Final, December 16th, 2013

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Lecture 18: Using Classes Effectively (Chapter 16)

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

Lecture 17: Classes (Chapter 15)

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

CS 1110 Final, December 16th, 2013

Lecture 17: Classes (Chapter 15)

CS Prelim 1 Review Fall 2017

Conditionals & Control Flow

CSE 331 Midterm Exam Sample Solution 2/13/12

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

CS 1110 Prelim 2 November 6th, 2012

CS Prelim 2 Review Fall 2015

Lecture 5: Strings

CSE 230 Computer Science II (Data Structure) Introduction

CS Prelim 2 Review Fall 2014

Lecture 12. Lists (& Sequences)

Sorting and Searching

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

CS1110. Lecture 1: Final review session. Review materials See website for a version of last year s final with conventions redone to match this year.

CS Prelim 2 Review Fall 2017

RECURSION (CONTINUED)

CS Prelim 2 Review Fall 2012

Announcements for This Lecture

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Announcements for This Lecture

CS Lecture 25: Models, Views, Controllers, and Games. Announcements

HEAPS & PRIORITY QUEUES

Lecture 2: Variables & Assignments

CSE 331 Software Design and Implementation. Lecture 3 Loop Reasoning

Announcements For This Lecture

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Repetition Through Recursion

CS 361 Data Structures & Algs Lecture 11. Prof. Tom Hayes University of New Mexico

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Spanning Trees. Lecture 22 CS2110 Spring 2017

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

Lecture 25. Sequence Algorithms (Continued)

12/30/2013 S. NALINI,AP/CSE

CS 1110 Final, December 17th, Question Points Score Total: 100

Assertions, pre/postconditions

Announcements For This Lecture

Abstract Data Types Chapter 1

CS1 Lecture 11 Feb. 9, 2018

Mathematical Induction

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CS 1110, LAB 2: FUNCTIONS AND ASSIGNMENTS

Use the scantron sheet to enter the answer to questions (pages 1-6)

CS 1110 Prelim 2 November 14th, 2013

PREPARING FOR PRELIM 2

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

CS Prelim 1 Review Fall 2016

CS 1110 Final, December 8th, Question Points Score Total: 100

Prof. Dr. A. Podelski, Sommersemester 2017 Dr. B. Westphal. Softwaretechnik/Software Engineering

Digital Circuits ECS 371

CS 1110, LAB 2: ASSIGNMENTS AND STRINGS

Lecture 6: Specifications & Testing

Transcription:

CS 1110 Lecture 19: Loop invariants Announcements Prelim 2 conflicts Today (April 2) is two weeks before the prelim, and the deadline for submitting prelim conflicts. Instructor travel This week and the next two weeks, Profs. Lee and Marschner will be traveling on and off. Instructor office hours are unaffected, though there will sometimes be just one of us available. Slides by D. Gries, L. Lee, S. Marschner, W. White

While-Loops and Flow print 'Before while' count = 0 i = 0 while i < 3: print 'Start loop '+`i` count = count + i i = i + 1 print 'End loop ' print 'After while' Output: Before while Start loop 0 End loop Start loop 1 End loop Start loop 2 End loop After while

Some Important Terminology assertion: true-false statement placed in a program to assert that it is true at that point Can either be a comment, or an assert command precondition: assertion placed before a statement Same idea as function precondition, but more general postcondition: assertion placed after a statement loop invariant: assertion supposed to be true before and after each iteration of the loop Distinct from attribute invariant iteration of a loop: one execution of its repetend

Some Important Terminology assertion: true-false statement placed in a program to assert that it is true at that point Can either be a comment, or an assert command precondition: assertion placed before a statement Same idea as function precondition, but more general postcondition: assertion placed after a statement loop invariant: assertion supposed to be true before and after each iteration of the loop Distinct from attribute invariant Gives methodology for designing loops iteration of a loop: one execution of its repetend

Assertions versus Asserts Assertions prevent bugs Help you keep track of what you are doing Also track down bugs Make it easier to check belief code mismatches The assert statement is also an assertion an assertion you are asking Python to enforce Cannot always convert a comment to an assert The root of all bugs! # x is the sum of 1..n Comment form of the assertion. x? n 1 x? n 3 x? n 0

Preconditions & Postconditions n precondition 1 2 3 4 5 6 7 8 # x = sum of 1..n-1 x = x + n n = n + 1 # x = sum of 1..n-1 postcondition x contains the sum of these (6) n 1 2 3 4 5 6 7 8 Precondition: assertion placed before a segment Postcondition: assertion placed after a segment x contains the sum of these (10) Relationship Between Two If precondition is true, then postcondition will be true

Solving a Problem precondition # x = sum of 1..n n = n + 1 # x = sum of 1..n What statement do you put here to make the postcondition true? A: x = x + 1 B: x = x + n C: x = x + n+1 D: None of the above E: I don t know postcondition

Solving a Problem precondition # x = sum of 1..n n = n + 1 # x = sum of 1..n What statement do you put here to make the postcondition true? postcondition A: x = x + 1 B: x = x + n C: x = x + n+1 D: None of the above E: I don t know Remember the new value of n

Invariants: Assertions That Do Not Change Loop Invariant: an assertion that is true before and after each iteration (execution of repetend) x = 0; i = 2 while i <= 5: x = x + i*i i = i +1 # x = sum of squares of 2..5 Invariant: x = sum of squares of 2..i-1 in terms of the range of integers that have been processed so far false i = 2 i <= 5 # invariant true x = x + i*i i = i +1 The loop processes the range 2..5

Invariants: Assertions That Do Not Change x = 0; i = 2 # Inv: x = sum of squares of 2..i-1 while i <= 5: x 0 4 i? 2 13 3 29 4 54 5 6 x = x + i*i i = i +1 i = 2 # Post: x = sum of squares of 2..5 Integers that have been processed: Range 2..i-1: 2, 3, 4, 5 2..1 2..2 2..3 2..4 2..5 (empty) # invariant true i <= 5 x = x + i*i Invariant was always true just before test of loop condition. So it s true when loop terminates false i = i +1 The loop processes the range 2..5

Designing Integer while-loops # Process integers in a..b # inv: integers in a..k-1 have been processed k = a while k <= b: process integer k k = k + 1 # post: integers in a..b have been processed Command to do something Equivalent postcondition init invariant cond true Process k invariant false k= k +1;

Designing Integer while-loops 1. Recognize that a range of integers b..c has to be processed 2. Write the command and equivalent postcondition 3. Write the basic part of the for-loop 4. Write loop invariant 5. Figure out any initialization 6. Implement the repetend (process k) # Process b..c Initialize variables (if necessary) to make invariant true # Invariant: range b..k-1 has been processed while k <= c: # Process k k = k + 1 # Postcondition: range b..c has been processed

Finding an Invariant Command to do something # Make b True if no int in 2..n-1 divides n, False otherwise b = True k = 2 # invariant: b is True if no int in 2..k-1 divides n, False otherwise while k < n: # Process k; if n % k == 0: b = False k = k +1 # b is True if no int in 2..n-1 divides n, False otherwise What is the invariant? Equivalent postcondition 1 2 3 k-1 k k+1 n

Finding an Invariant # set x to # adjacent equal pairs in s[0..len(s)-1] for s = 'ebeee', x = 2 # invariant:??? k = 0 while k < len(s): # Process k; k = k +1 # x = # adjacent equal pairs in s[0..len(s)-1] Command to do something Equivalent postcondition k: next integer to process. Which have been processed? A: 0..k B: 1..k C: 0..k 1 D: 1..k 1 E: I don t know What is the invariant? A: x = no. adj. equal pairs in s[1..k] B: x = no. adj. equal pairs in s[0..k] C: x = no. adj. equal pairs in s[1..k 1] D: x = no. adj. equal pairs in s[0..k 1] E: I don t know

Reason carefully about initialization # s is a string; len(s) >= 1 # Set c to largest element in s c =?? Command to do something k =?? # inv: c is largest element in s[0..k 1] while k < len(s): # Process k k = k+1 # c = largest char in s[0..len(s) 1] Equivalent postcondition 1. What is the invariant? 2. How do we initialize c and k? A: k = 0; c = s[0] B: k = 1; c = s[0] C: k = 1; c = s[1] D: k = 0; c = s[1] E: None of the above An empty set of characters or integers has no maximum. Therefore, be sure that 0..k 1 is not empty. You must start with k = 1.