Developing Loops from Invariants

Slides:



Advertisements
Similar presentations
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.
Advertisements

Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
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 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
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.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
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 Program Correctness CIS 375 Bruce R. Maxim UM-Dearborn.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
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.
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.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
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.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 27.
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
1 CS100J 21 March 2006 Arrays: searching & sorting. Reading: 8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Chapter 3 of Programming Languages by Ravi Sethi
SWEN421 – Lecture 4 Contracts and Correctness
Correctness issues and Loop invariants
Start reading Sec and chapter 7 on loops
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
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.
Specifications What? Not how!.
LOOPS.
6.001 SICP Stacks and recursion
Some Basics for Problem Analysis and Solutions
Arrays & Functions Lesson xx
Algorithm design and Analysis
Review 4 Lists and Sequences.
CS 220: Discrete Structures and their Applications
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Asymptotic complexity Searching/sorting
Binary Search and Loop invariants
Asymptotic complexity
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS November 2010 Developing array algorithms. Reading:
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Searching and Sorting Hint at Asymptotic Complexity
Developing loops from invariants
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS100J Nov 04, 2003 Arrays. Reading: 8
Review 4 Lists and Sequences.
CS100A Sections Dec Loop Invariant Review C Review and Example
Developing Loops from Invariants
COP4020 Programming Languages
Searching and Sorting Hint at Asymptotic Complexity
CSE 373: Data Structures and Algorithms
Algorithms and Data Structures Lecture II
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

Developing a Loop on a Range of Integers Given a range of integers a..b to process. Possible alternatives Could use a for-loop: for x in range(a,b+1): Or could use a while-loop: x = a; while x <= b: Which one you can use will be specified But does not remove the need for invariants Invariants: assertion supposed to be true before and after each iteration of the loop

Developing an Integer Loop (a) Suppose you are trying to implement the command Process a..b Write the command as a postcondition:      post: a..b has been processed.

Developing an Integer Loop (b) Set-up using while: while k <= b: # Process k k = k + 1 # post: a..b has been processed.

Developing an Integer Loop (c) Add the invariant: # invariant: a..k-1 has been processed while k <= b: # Process k k = k + 1 # post: a..b has been processed. Note it is post condition with the loop variable

Developing an Integer Loop (d) Fix the initialization: init to make invariant true # invariant: a..k-1 has been processed while k <= b: # Process k k = k + 1 # post: a..b has been processed. Has to handle the loop variable (and others)

Developing an Integer Loop (e) Figure out how to “Process k”: init to make invariant true # invariant: a..k-1 has been processed while k <= b: # Process k implementation of “Process k” k = k + 1 # post: a..b has been processed.

Pay attention to range: This affects the loop condition! a..b or a+1..b or a…b-1 or … This affects the loop condition! Range a..b-1, has condition k < b Range a..b, has condition k <= b Note that a..a-1 denotes an empty range There are no values in it a..b how many elements? b – a + 1

Horizontal Notation for Sequences b <= sorted >= 0 k len(b) Example of an assertion about an sequence b. It asserts that: b[0..k–1] is sorted (i.e. its values are in ascending order) Everything in b[0..k–1] is ≤ everything in b[k..len(b)–1]

Algorithm Inputs We may specify that the list in the algorithm is b[0..len(b)-1] or a segment b[h..k] or a segment b[m..n-1] Work with whatever is given! Remember formula for # of values in an array segment Following – First e.g. the number of values in b[h..k] is k+1–h. ? h k b

Example Question, Fall 2013 Final sorted 0 k pre: b 0 h k post: b Unchanged, values in b[h+1..k] b[0..k] w/o duplicates inv: b 0 p h k Unchanged, values all in b[h+1..k] ??? b[p+1..k] w/o duplicates Example: Input [1, 2, 2, 2, 4, 4, 4] Output [1, 2, 2, 2, 1, 2, 4]

Unchanged, values all in b[h+1..k] Solution to Fall 2013 Final inv: b 0 p h k Unchanged, values all in b[h+1..k] unchanged b[p+1..k] w/o duplicates # Assume 0 <= k, so the list segment has at least one element p = h = # inv: b[h+1..k] is original b[p+1..k] with no duplicates # b[p+1..h] is unchanged from original list w/ values in b[h+1..k] # b[0..p] is unchanged from original list while :

Unchanged, values all in b[h+1..k] Solution to Fall 2013 Final inv: b 0 p h k Unchanged, values all in b[h+1..k] unchanged b[p+1..k] w/o duplicates # Assume 0 <= k, so the list segment has at least one element p = k-1 h = k-1 # inv: b[h+1..k] is original b[p+1..k] with no duplicates # b[p+1..h] is unchanged from original list w/ values in b[h+1..k] # b[0..p] is unchanged from original list while :

Unchanged, values all in b[h+1..k] Solution to Fall 2013 Final inv: b 0 p h k Unchanged, values all in b[h+1..k] unchanged b[p+1..k] w/o duplicates # Assume 0 <= k, so the list segment has at least one element p = k-1 h = k-1 # inv: b[h+1..k] is original b[p+1..k] with no duplicates # b[p+1..h] is unchanged from original list w/ values in b[h+1..k] # b[0..p] is unchanged from original list while 0 <= p:

Unchanged, values all in b[h+1..k] Solution to Fall 2013 Final inv: b 0 p h k Unchanged, values all in b[h+1..k] unchanged b[p+1..k] w/o duplicates # Assume 0 <= k, so the list segment has at least one element p = k-1 h = k-1 # inv: b[h+1..k] is original b[p+1..k] with no duplicates # b[p+1..h] is unchanged from original list w/ values in b[h+1..k] # b[0..p] is unchanged from original list while 0 <= p: if b[p] != b[p+1]: b[h] = b[p] h = h-1 p = p-1

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 ___________ : # Okay to use b, c, d, h, k, and carry # Anything else should be ‘local’ to while 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 double check corner cases! h = len(c) while h > 0: What will happen when h=1 and h=len(c)? If you use h in c (e.g. c[h]) can you possibly get an error? # 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: … Range is off by 1. How do you know?

DOs and DON’Ts #3 DON’T put variables directly above vertical line. Where is j? Is it unknown or >= x? <= x x ? >= x h i j k b 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           |     -------

Dutch National Flag Sequence of 0..n-1 of red, white, blue colors Arrange to put reds first, then whites, then blues Input is the list b of integers Modifies the list according to the invariant. ??? 0 len(b) pre: b 0 len(b) post: b < 0 == 0 > 0 0 j k m len(b) Inv: b < 0 == 0 ??? > 0

Dutch National Flag 0 j k m len(b) Inv: b < 0 == 0 ??? > 0 def dutch_national_flag(b): j = 0; k = 0; m = len(b) while k < m: if b[k] == 0: k = k + 1 elif b[k] > 0: _swap(b, k, m) m = m – 1 else: # b[k] < 0 _swap(b, k, j) j = j + 1

Dutch National Flag 0 j k m len(b) Inv: b < 0 == 0 ??? > 0 def dutch_national_flag(b): j = 0; k = 0; m = len(b) while k < m: if b[k] == 0: k = k + 1 elif b[k] > 0: _swap(b, k, m) m = m – 1 else: # b[k] < 0 _swap(b, k, j) j = j + 1 dutch_national_flag([3,-1,5,-2,0]) k, j m 3 -1 5 -2 k, j m -1 5 -2 3 j k m -1 5 -2 3 j k m -1 5 -2 3 j k m -1 -2 5 3 j k, m -1 -2 5 3

Questions?