Developing loops from invariants

Slides:



Advertisements
Similar presentations
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
Advertisements

1 CS March 2009 While loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim in two days: Th 7:30-9pm Uris Aud.
1 CS100J 27 March 2007 Algorithms on arrays Reading: 8.3–8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous.
1 CS100J October 06, 2005 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
1 CS100J 15 March, 2006 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
1 Assignment 6. Developing Loops Due on Tuesday, 30 October, by midnight (submitted electronically). Note that each question will be graded on the following.
1 CS100J 29 March 2007 Arrays: searching & sorting. Reading: 8.5 Why did I get a Christmas Card on Halloween? Searching and sorting algorithms are on the.
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
1 CS March 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
1 CS100J 31 October 2006 Arrays: searching & sorting. Reading: 8.5 Merry Christmas!! On Halloween? Searching and sorting algorithms are on the course website.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
1 CS100J 06 March 2008 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
UMBC CMSC 104 – Section 01, Fall 2016
Outline lecture Revise arrays Entering into an array
I/O Streams File I/O 2-D array review
SWEN421 – Lecture 4 Contracts and Correctness
Prof. Mark Glauser Created by: David Marr
Correctness issues and Loop invariants
Start reading Sec and chapter 7 on loops
Mr Barton’s Maths Notes
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
Simple Sorting Algorithms
CS October 2009 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Algebra 6. Factorising Quadratics
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
Arrays & Functions Lesson xx
Big-Oh and Execution Time: A Review
Developing Loops from Invariants
Algorithm design and Analysis
Announcements P2 is due tomorrow Prelim on Monday
CS October 2008 The while loop and assertions
Writing Methods AP Computer Science A.
CS 220: Discrete Structures and their Applications
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Asymptotic complexity Searching/sorting
Simple Sorting Algorithms
Binary Search and Loop invariants
© T Madas.
Mr Barton’s Maths Notes
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Asymptotic complexity
CS100J Lecture 14 Previous Lecture
CS November 2010 Developing array algorithms. Reading:
Sorting and Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Simple Sorting Algorithms
Searching and Sorting Hint at Asymptotic Complexity
Linear and Binary Search
CS100J Nov 04, 2003 Arrays. Reading: 8
Simple Sorting Algorithms
CS100A Sections Dec Loop Invariant Review C Review and Example
Developing Loops from Invariants
EECE.2160 ECE Application Programming
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
CS100J 16 Oct, 2007 Assignment A5: loops
CS October 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Presentation transcript:

Developing loops from invariants CS1110 Final Review

Info My name: Feel free to ask questions at any time Slides will be posted online

Outline 4 questions for loop How to develop loops from invariants Common mistakes What’s in the exam

Four loopy questions How does it start (does the initialization make the invariant true? When does it stop (the invariant together with falsity of the guard should imply the postcondition)? Does the repetend make progress toward termination? Does the repetend keep the invariant true?

Developing a for-loop Develop a for-loop when you recognize that a range of integers a..b has to be processed. The steps we propose for developing the loop allows it to be developed with a minimum of fuss and a maximum chance of success. The development of most of the loop is almost mechanical, allowing you to focus on the difficult, creative parts.

Developing a for-loop-(a) Suppose the command you are trying to implement is Process a..b Write the command as a postcondition:      post: a..b has been processed.

Developing loops-(b) write the loop:     for (int k= a; k <= b; k= k+1) {          //Process k     }     // post: a..b has been processed.

Developing loops-(c) Fill in the invariant     // invariant: a..k-1 has been processed     for (int k= a; k <= b; k= k+1) {          Process k     }     // post: a..b has been processed.

Developing loops-(d) Fix the initialization     init to make invariant true;     // invariant: a..k-1 has been processed     for (int k= a; k <= b; k= k+1) {          Process k     }     // post: a..b has been processed.

Developing loops-(e) Figure out how to "Process k"     init to make invariant true;     // invariant: a..k-1 has been processed     for (int k= a; k <= b; k= k+1) {          // Process k         implementation of "Process k"     }     // post: a..b has been processed.

Range Pay attention to range: a..b or a+1..b or a…b-1 or … To process a range a..b-1, change the loop condition to k < b Note that a..a-1 denotes an empty range —no values in it

Spring’08-Prelim3 Question 3 A magic square is a square where each row and column adds up to the same number (sometimes, one also includes the diagonals, but for this problem, we won’t). For example, in the following 5-by-5 square, the elements in each row and column add up to 70:

While-Loop for (int k= a; k <= b; k= k+1) {…S…} int k= a; while (k <= b) { S; k= k+1; } You will not be asked to develop the invariant for a while-loop (unless it is one of the algorithms you must know). You may be asked to develop one given the invariant.

Another example Prelim 3 Question 4. Addition of 2 numbers h=? k=? carry=? //invariant: b[h..] contains the sum of c[h..] and d[k..], except that the carry into position k-1 is in ‘carry’ While (_________) { } //postcondition: b contains the sum of c and d // except that the carry contains the 0 or 1 that // belongs in the beginning

DOs and DON’Ts #1 DO use variables given in the invariant. DON’T Use other variables. //invariant: b[h..] contains the sum of c[h..] and d[k..], except that the carry into position k-1 is in ‘carry’ While (_________) { //use h, k, carry // you can declare variables here } On the test, we may give them a question where we state the precondition, postcondition, and loop invariant and ask them to write the loop with initialization. When this is done, they MUST use the given invariant. They should NOT add other variables declared outside the loop, although they may declare variables within the repetend.

DOs and DON’Ts #2 DO understand invariants first x = some function of b[A..k-1] For (int k=A;….;k=k+1) X= some function of b[k..b.length-1] For (int k=b.length;…;k=k-1) h=c.length k=d.length carry=0 //invariant: b[h..] contains the sum of c[h..] and d[k..], except that the carry into position k-1 is in ‘carry’ While (h>0) //why is not >=0? Most serious problem here is that they don't first try to understand the loop invariant and then use it, using the four loopy questions. They MUST read and understand the invariant. Here's some points. (1) if the invariant says,    x = some function of b[A..k-1] then then they should probably make the range A..k-1 empty by setting k to A and then figuring out what x is for b[A..A-1]. On the other hand, if it says,   x = some function of b[k..b.length-1] then they should probably initialize k to b.length so that the range is b.length..b.length-1  and then figure out what x should be for the empty range.

DOs and DON’Ts #3 DO double check corner cases! for (x=0; x<b.length; x++) What will happen when x=0 and x=b.length-1? If you use x in array, e.g. a[x], will x always be between 0...a.length-1? h=c.length k=d.length carry=0 //invariant: b[h..] contains the sum of c[h..] and d[k..], except that the carry into position k-1 is in ‘carry’ While (h>0) { h--; k--; //directly using b[h] or c[h] or d[k] is wrong! }

DOs and DON’Ts #4 DON’T put the variable directly above the vertical line. // Where is k? v ≥ C ? all Z’s k ? 0 3 k 8 For that matter, any loop that does this    Process b[h..k] Probably has a loop invariant that looks like this:     h                     i                      k   -------------------------------------- b|   processed |     ?                   |   ------------------------------------- BIG PROBLEM IN DRAWING SUCH DIAGRAMS. DO NOT PUT THE VARIABLE DIRECTLY ABOVE THE VERTICAL LINE. Stress this over and over:           k     -------          THIS IS STUPID! Shows lack of care. It's ambiguous           |     -------             k     -------          Now it labels the first element of the segment           |     -------

What’s in the exam Array diagrams, invariant from pre- and post- condition, initialization, body of loop, etc. We will NOT ask you to write a loop invariant for a while-loop EXCEPT: Linear search, binary search, dutch national flag, partition algorithm of quicksort, insertion sort, and selection sort: We may give you a problem that requires you to write a loop (with initialization) that processes a range of integers. You should be able to write a postcondition, then write the loop header "for (int k ...)", then write a loop invariant, and finally develop the various parts of the loops and initialization

Questions?