Binary Search and Loop invariants

Slides:



Advertisements
Similar presentations
SORTING Lecture 12B CS2110 – Spring InsertionSort 2 pre: b 0 b.length ? post: b 0 b.length sorted inv: or: b[0..i-1] is sorted b 0 i b.length sorted.
Advertisements

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
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.
Weakest pre-conditions and towards machine consistency Saima Zareen.
CS100J Nov 04, 2003 Arrays --sorting. Reading: 8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous kind.
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.
Discrete Mathematics Lecture 4: Sequences and Mathematical Induction
Discrete Mathematics Lecture 4 Harper Langston New York University.
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 Teaching the development of algorithms Teach skill, not only facts David Gries Computer Science Department Cornell University November 2004 DrJava is.
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 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 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:
Reasoning with invariants Jordi Cortadella Department of Computer Science.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
CSC 211 Data Structures Lecture 13
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.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
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.
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.
1 CS April 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
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.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Correctness issues and Loop invariants
Reasoning with invariants
GC211Data Structure Lecture2 Sara Alhajjam.
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
CS 3343: Analysis of Algorithms
Developing Loops from Invariants
Announcements P2 is due tomorrow Prelim on Monday
CS October 2008 The while loop and assertions
Testing change to a linked list
CS 220: Discrete Structures and their Applications
Programming Languages 2nd edition Tucker and Noonan
"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 Lecture 11 CS2110 – Fall 2018.
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant.
Given value and sorted array, find index.
Asymptotic complexity Searching/sorting
Search algorithms for vectors
"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 Lecture 11 CS2110 – Spring 2019.
Output Variables {true} S {i = j} i := j; or j := i;
Asymptotic complexity
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS November 2010 Developing array algorithms. Reading:
CS100J Lecture 15 Previous Lecture This Lecture Sorting
Chapter 18 Recursion.
Sorting and Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
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
Linear and Binary Search
CS100J Nov 04, 2003 Arrays. Reading: 8
CS100A Sections Dec Loop Invariant Review C Review and Example
Developing Loops from Invariants
Programming Languages 2nd edition Tucker and Noonan
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

Binary Search and Loop invariants Lecture 12A CS2110 – Spring 2014

Develop binary search in sorted array b for v pre: b 0 b.length ? post: b 0 h b.length <= v > v 2 2 4 4 4 4 7 9 9 9 9 pre: b 0 4 5 6 7 b.length Example: If v is 7 or 8, h is 6 If v is 4, 5, or 6, h is 5 If v in b, h is index of rightmost occurrence of v. If v not in b, h is index before where it belongs.

Develop binary search in sorted array b for v pre: b 0 b.length ? post: b 0 h b.length <= v > v Better than Binary search in last lecture because it Finds not a random occurrence of v but the rightmost one. Useful in some situations If v is not in b, it gives useful information: it belongs between b[h] and b[h+1] Works also when array is empty!

Develop binary search in sorted array b for v pre: b 0 b.length ? Store a value in h to make this true: post: b 0 h b.length <= v > v Get loop invariant by combining pre- and post- conditions, adding variable t to mark the other boundary inv: b 0 h t b.length <= v ? > v

How does it start (what makes the invariant true)? pre: b 0 b.length ? inv: b 0 h t b.length <= v ? > v Make first and last partitions empty: h= -1; t= b.length;

When does it end (when does invariant look like postcondition)? b 0 h b.length <= v > v inv: b 0 h t b.length <= v ? > v Stop when ? section is empty. That is when h = t-1. Therefore, continue as long as h != t-1. h= -1; t= b.length; while ( ) { } h != t-1

How does body make progress toward termination (cut How does body make progress toward termination (cut ? in half) and keep invariant true? inv: b 0 h t b.length <= v ? > v Let e be index of middle value of ? Section. Maybe we can set h or t to e, cutting ? section in half b 0 h e t b.length <= v ? > v h= -1; t= b.length; while ( h != t-1 ) { } int e= (h+t)/2;

How does body make progress toward termination (cut How does body make progress toward termination (cut ? in half) and keep invariant true? inv: b 0 h t b.length <= v ? > v b 0 h e t b.length <= v ? ? > v If b[e] <= v, then so is every value to its left, since the array is sorted. Therefore, h= e; keeps the invariant true. b 0 h e t b.length <= v <= v ? > v h= -1; t= b.length; while ( h != t-1 ) { int e= (h+t)/2; } if (b[e] <= v) h= e;

How does body make progress toward termination (cut How does body make progress toward termination (cut ? in half) and keep invariant true? inv: b 0 h t b.length <= v ? > v b 0 h e t b.length <= v ? ? > v If b[e] > v, then so is every value to its right, since the array is sorted. Therefore, t= e; keeps the invariant true. b 0 h e t b.length <= v ? > v > v h= -1; t= b.length; while ( h != t-1 ) { int e= (h+t)/2; if (b[e] <= v) h= e; } else t= e;

Loop invariants pre: post: inv: We used the concept of a loop invariant in developing algorithms to reverse a linked list and do a binary search on a sorted array. ? pre: b 0 b.length post: b 0 h b.length <= v > v inv: b 0 h t b.length <= v ? > v

Loop invariant: Important part of every formal system for proving loops correct. Extremely useful tool in developing a loop. Create (first draft of) invariant from pre- and post-conditions, then develop the parts of the loop from precondition, postcondition, invariant. ? pre: b 0 b.length post: b 0 h b.length <= v > v inv: b 0 h t b.length <= v ? > v

Loop invariant: Important part of every formal system for proving loops correct. Invariant can be written in English, mathematics, diagrams, or mixtures of these. The important points are precision, clarity. inv: b 0 h t b.length <= v ? > v inv: b[0..h] <= v < b[t..b.length-1] inv: b[0..h] <= v < b[t..] inv: everything in b[0..h] is at most v, everything in b[t..] is greater than v

About notation b[h..k]. b[h..k] has k+1–h elements h h+1 h+2 h+3 [h..h+3] 4 elements [h..h+2] 3 elements Convention: The notation b[h..k] is used only when h <= k+1. For example, b[0..-2] is not allowed. When h = k+1, b[h..k] denotes the empty segment starting at b[h]. [h..h+1] 2 elements [h..h] 1 element [h..h-1] How many elements? Use the formula: 0!

Developing loop from pre, post, inv: 4 loopy questions // pre // inv while ( b ) { // inv && b } // inv && ! b // post 1. How does it start? What init makes invariant true? init 2. When can it stop? Choose b so that inv && !b implies post 4. How do we make sure invariant is maintained? Ensure inv remains true; 3. How does body make progress toward termination? progress