Loop Invariants and Binary Search

Similar documents
Analysis of Binary Search algorithm and Selection Sort algorithm

Lecture Notes on Linear Search

Zabin Visram Room CS115 CS126 Searching. Binary Search

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Binary search algorithm

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Computer Science 210: Data Structures. Searching

6. Standard Algorithms

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

Lecture 3: Finding integer solutions to systems of linear equations

Regression Verification: Status Report

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Section IV.1: Recursive Algorithms and Recursion Trees

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Binary Heap Algorithms

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Binary Multiplication

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

A simple algorithm with no simple verication

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Automated Program Behavior Analysis

Algorithms and Data Structures

Lecture Notes on Binary Search Trees

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Binary Search Trees CMPSC 122

Chapter 3. if 2 a i then location: = i. Page 40

Data Structures and Algorithms Written Examination

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

CS473 - Algorithms I

SQL Query Evaluation. Winter Lecture 23

Change Impact Analysis

Randomized algorithms

Regular Languages and Finite Automata

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Computing Concepts with Java Essentials

Introduction to Programming System Design. CSCI 455x (4 Units)

Chapter 8: Bags and Sets

Mathematical Induction. Mary Barnes Sue Gordon

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Software Testing. Quality & Testing. Software Testing

Pseudo code Tutorial and Exercises Teacher s Version

A Randomized Searching Algorithm and its Performance analysis with Binary Search and Linear Search Algorithms

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Software Testing & Analysis (F22ST3): Static Analysis Techniques 2. Andrew Ireland

Query Processing C H A P T E R12. Practice Exercises

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Sequential Data Structures

CS 3719 (Theory of Computation and Algorithms) Lecture 4

Applied Algorithm Design Lecture 5

Rigorous Software Development CSCI-GA

CS106A, Stanford Handout #38. Strings and Chars

Integer Factorization using the Quadratic Sieve

Lemma 5.2. Let S be a set. (1) Let f and g be two permutations of S. Then the composition of f and g is a permutation of S.

Recursion vs. Iteration Eliminating Recursion

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Near Optimal Solutions

Algorithms. Margaret M. Fleck. 18 October 2010

1 Definition of a Turing machine

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

The KaleidaGraph Guide to Curve Fitting

Data Structures. Algorithm Performance and Big O Analysis

EFFICIENT EXTERNAL SORTING ON FLASH MEMORY EMBEDDED DEVICES

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

TECHNICAL UNIVERSITY OF CRETE DATA STRUCTURES FILE STRUCTURES

6.2 Permutations continued

Solving Problems Recursively

Lecture 4 Online and streaming algorithms for clustering

Searching Algorithms

6.042/18.062J Mathematics for Computer Science. Expected Value I

This asserts two sets are equal iff they have the same elements, that is, a set is determined by its elements.

Linked Lists: Implementation Sequences in the C++ STL

Cosmological Arguments for the Existence of God S. Clarke

AP Computer Science AB Syllabus 1

Cryptography and Network Security Prof. D. Mukhopadhyay Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Algorithms are the threads that tie together most of the subfields of computer science.

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

Chapter 2 Data Storage

Lecture 11: Tail Recursion; Continuations

each college c i C has a capacity q i - the maximum number of students it will admit

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Mathematical Induction. Lecture 10-11

Problem Set 7 Solutions

5. Linear algebra I: dimension

Node-Based Structures Linked Lists: Implementation

[Refer Slide Time: 05:10]

Lecture 1: Course overview, circuits, and formulas

Computational Geometry. Lecture 1: Introduction and Convex Hulls

Sources: On the Web: Slides will be available on:

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

Analysis of a Search Algorithm

Algorithm Design and Recursion

Recursion and Dynamic Programming. Biostatistics 615/815 Lecture 5

Transcription:

Loop Invariants and Binary Search Chapter 4.3.3 and 9.3.1-1 -

Outline Ø Iterative Algorithms, Assertions and Proofs of Correctness Ø Binary Search: A Case Study - 2 -

Outline Ø Iterative Algorithms, Assertions and Proofs of Correctness Ø Binary Search: A Case Study - 3 -

Assertions Ø An assertion is a statement about the state of the data at a specified point in your algorithm. Ø An assertion is not a task for the algorithm to perform. Ø You may think of it as a comment that is added for the benefit of the reader. - 4 -

Loop Invariants Ø Binary search can be implemented as an iterative algorithm (it could also be done recursively). Ø Loop Invariant: An assertion about the current state useful for designing, analyzing and proving the correctness of iterative algorithms. - 5 -

Other Examples of Assertions Ø Preconditions: Any assumptions that must be true about the input instance. Ø Postconditions: The statement of what must be true when the algorithm/program returns. Ø Exit condition: The statement of what must be true to exit a loop. - 6 -

Iterative Algorithms Take one step at a time towards the final destination loop (done) take step end loop - 7 -

Establishing Loop Invariant From the Pre-Conditions on the input instance we must establish the loop invariant. - 8 -

Maintain Loop Invariant Ø Suppose that q We start in a safe location (pre-condition) q If we are in a safe location, we always step to another safe location (loop invariant) Ø Can we be assured that the computation will always be in a safe location? Ø By what principle? - 9 -

Maintain Loop Invariant By Induction the computation will always be in a safe location. S(0) isi, ( ) isi, ( ) Si ( + 1) - 10 -

Ending The Algorithm Ø Define Exit Condition Exit Ø Termination: With sufficient progress, the exit condition will be met. 0 km Exit Ø When we exit, we know q exit condition is true q loop invariant is true from these we must establish Exit the post conditions. - 11 -

Definition of Correctness <PreCond> & <code> è <PostCond> If the input meets the preconditions, then the output must meet the postconditions. If the input does not meet the preconditions, then nothing is required. - 12 -

Outline Ø Iterative Algorithms, Assertions and Proofs of Correctness Ø Binary Search: A Case Study - 13 -

Define Problem: Binary Search Ø PreConditions q Key 25 q Sorted List 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 Ø PostConditions q Find key in list (if there). 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95-14 -

Define Loop Invariant Ø Maintain a sublist. Ø If the key is contained in the original list, then the key is contained in the sublist. key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95-15 -

Define Step Ø Cut sublist in half. Ø Determine which half the key would be in. Ø Keep that half. key 25 mid 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key mid, then key is in left half. - 16 - If key > mid, then key is in right half.

Define Step Ø It is faster not to check if the middle element is the key. Ø Simply continue. key 43 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key mid, then key is in left half. - 17 - If key > mid, then key is in right half.

Make Progress Ø The size of the list becomes smaller. 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 79 km 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 75 km - 18 -

Exit Condition key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 0 km Ø If the key is contained in the original list, then the key is contained in the sublist. Ø Sublist contains one element. Exit If element = key, return associated entry. Otherwise return false. - 19 -

Running Time The sublist is of size n, n / 2, n / 4, n / 8,,1 Each step O(1) time. Total = O(log n) key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key mid, then key is in left half. - 20 - If key > mid, then key is in right half.

Running Time Ø Binary search can interact poorly with the memory hierarchy (i.e. caching), because of its random-access nature. Ø It is common to abandon binary searching for linear searching as soon as the size of the remaining span falls below a small value such as 8 or 16 or even more in recent computers. - 21 -

END OF LECTURE FEB 13, 2014-22 -

BinarySearch(A[1..n], key) <precondition>: A[1..n] is sorted in non-decreasing order <postcondition>: If key is in A[1..n], algorithm returns its location p = 1, q = n while q > p < loop-invariant>: If key is in A[1..n], then key is in A[p.. q] p + q mid = 2 if key A[ mid ] q = mid else p = mid + 1 end end if key = A[ p] return( p) else return("key not in list") end - 23 -

Simple, right? Ø Although the concept is simple, binary search is notoriously easy to get wrong. Ø Why is this? - 24 -

Boundary Conditions Ø The basic idea behind binary search is easy to grasp. Ø It is then easy to write pseudocode that works for a typical case. Ø Unfortunately, it is equally easy to write pseudocode that fails on the boundary conditions. - 25 -

Boundary Conditions if key A[ mid ] q = mid else p = mid + 1 end or if key < A[ mid ] q = mid else p = mid + 1 end What condition will break the loop invariant? - 26 -

Boundary Conditions key 36 mid 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 Code: key A[mid] select right half Bug!! - 27 -

Boundary Conditions if key A[ mid ] q = mid else p = mid + 1 end if key < A[ mid ] q = mid 1 else p = mid end if key < A[ mid ] q = mid else p = mid + 1 end OK OK Not OK!! - 28 -

Boundary Conditions mid p+ q = 2 or mid p+ q = 2 key 25 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 Shouldn t matter, right? Select mid p + q = 2-29 -

Boundary Conditions key 25 mid Select mid p + q = 2 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key mid, then key is in left half. If key > mid, then key is in right half. - 30 -

Boundary Conditions key 25 mid Select mid p + q = 2 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key mid, then key is in left half. If key > mid, then key is in right half. - 31 -

Boundary Conditions Another bug! key 25 No progress toward goal: Loops Forever! mid Select mid p + q = 2 3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95 If key mid, then key is in left half. If key > mid, then key is in right half. - 32 -

Boundary Conditions p + q mid = 2 if key A[ mid ] q = mid else p = mid + 1 end p + q mid = 2 if key < A[ mid ] q = mid 1 else p = mid end p + q mid = 2 if key A[ mid ] q = mid else p = mid + 1 end OK OK Not OK!! - 33 -

Ø How many possible algorithms? Ø How many correct algorithms? Ø Probability of guessing correctly? Getting it Right p + q mid = 2 if key A[ mid ] q = mid else p = mid + 1 end p + q o r mid =? 2 or if key < A[ mid ]? or q = mid 1 else end p = mid - 34 -

Alternative Algorithm: Less Efficient but More Clear BinarySearch(A[1..n],key) <precondition>: A[1..n] is sorted in non-decreasing order <postcondition>: If key is in A[1..n], algorithm returns its location p = 1, q = n while q p end < loop-invariant>: If key is in A[1..n], then key is in A[p..q] mid = p + q 2 if key <A[mid] q = mid 1 else if key > A[mid] else end p = mid + 1 return(mid) return("key not in list") Still Θ(log n), but with slightly larger constant. - 35 -

Card Trick Ø A volunteer, please. - 36 -

Pick a Card Done - 37 - Thanks to J. Edmonds for this example.

Loop Invariant: The selected card is one of these. - 38 -

Which column? left - 39 -

Loop Invariant: The selected card is one of these. - 40 -

Selected column is placed in the middle - 41 -

I will rearrange the cards - 42 -

Relax Loop Invariant: I will remember the same about each column. - 43 -

Which column? right - 44 -

Loop Invariant: The selected card is one of these. - 45 -

Selected column is placed in the middle - 46 -

I will rearrange the cards - 47 -

Which column? left - 48 -

Loop Invariant: The selected card is one of these. - 49 -

Selected column is placed in the middle - 50 -

Here is your card. Wow! - 51 -

Ternary Search Ø Loop Invariant: selected card in central subset of cards Size of subset = n / 3 where n = i 1 total number of cards i = iteration index Ø How many iterations are required to guarantee success? - 52 -

Learning Outcomes Ø From this lecture, you should be able to: q Use the loop invariant method to think about iterative algorithms. q Prove that the loop invariant is established. q Prove that the loop invariant is maintained in the typical case. q Prove that the loop invariant is maintained at all boundary conditions. q Prove that progress is made in the typical case q Prove that progress is guaranteed even near termination, so that the exit condition is always reached. q Prove that the loop invariant, when combined with the exit condition, produces the post-condition. q Trade off efficiency for clear, correct code. - 53 -