COMP250: Loop invariants

Similar documents
Lecture 2: Getting Started

Algorithms - Ch2 Sorting

Recurrences and Divide-and-Conquer

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Sorting & Growth of Functions

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

The divide and conquer strategy has three basic parts. For a given problem of size n,

Advanced Algorithms and Data Structures

ECS122A Lecture Notes on Algorithm Design and Analysis

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

EECS 2011M: Fundamentals of Data Structures

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 10

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

Algorithms. Notations/pseudo-codes vs programs Algorithms for simple problems Analysis of algorithms

Lecture 1. Introduction / Insertion Sort / Merge Sort

CS 561, Lecture 1. Jared Saia University of New Mexico

Lecture Notes on Linear Search

2 Proposed Implementation. 1 Introduction. Abstract. 2.1 Pseudocode of the Proposed Merge Procedure

Back to Sorting More efficient sorting algorithms

Computer Programming

Lecture Notes on Quicksort

6/12/2013. Introduction to Algorithms (2 nd edition) Overview. The Sorting Problem. Chapter 2: Getting Started. by Cormen, Leiserson, Rivest & Stein

Sorting L7.2 Recall linear search for an element in an array, which is O(n). The divide-and-conquer technique of binary search divides the array in ha

Lecture 5: Sorting Part A

CS126 Final Exam Review

Recursion: The Beginning

Data Structures and Algorithms Chapter 2

Lecture 9: Sorting Algorithms

Lecture 15 : Review DRAFT

Lecture Notes on Quicksort

Lecture Notes for Chapter 2: Getting Started

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Analyzing Complexity of Lists

Basic C Programming: Examples. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

S1) It's another form of peak finder problem that we discussed in class, We exploit the idea used in binary search.

Cpt S 122 Data Structures. Sorting

Lecture 4 Searching Arrays

Sorting Pearson Education, Inc. All rights reserved.

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Searching Algorithms/Time Analysis

Biostatistics 615/815 Lecture 5: Divide and Conquer Algorithms Sorting Algorithms

Lecture 19 Sorting Goodrich, Tamassia

Keywords Comparisons, Insertion Sort, Selection Sort, Bubble Sort, Quick Sort, Merge Sort, Time Complexity.

Mathematical Induction

Deterministic and Randomized Quicksort. Andreas Klappenecker

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Other techniques for sorting exist, such as Linear Sorting which is not based on comparisons. Linear Sorting techniques include:

Recursive Definitions Structural Induction Recursive Algorithms

1 The sorting problem

Introduction to Computer Algorithms Lecture Notes (undergraduate CS470 course) Grzegorz Malewicz

Divide-and-Conquer CSE 680

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Simple Sorting Algorithms

Sorting and Searching

Assertions & Verification & Example Loop Invariants Example Exam Questions

Outline. CS 561, Lecture 6. Priority Queues. Applications of Priority Queue. For NASA, space is still a high priority, Dan Quayle

Binary Search to find item in sorted array

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

CS420/520 Algorithm Analysis Spring 2010 Lecture 01

Dm507 project 1. Emil Sjulstok Rasmussen Hold: M1

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp )

Sorting Algorithms. For special input, O(n) sorting is possible. Between O(n 2 ) and O(nlogn) E.g., input integer between O(n) and O(n)

Data Structures and Algorithms. Part 2

Assertions & Verification Example Exam Questions

Divide and Conquer. Algorithm D-and-C(n: input size)

COMP Analysis of Algorithms & Data Structures

An Annotated Language

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

Reading 8 : Recursion

Sorting. Task Description. Selection Sort. Should we worry about speed?

Comparison Sorts. Chapter 9.4, 12.1, 12.2

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Computer Science 331

Recursion: The Beginning

Announcements. Biostatistics 615/815 Lecture 5: More on STLs, and Divide and Conquer Algorithms. Recap on STL : vector.

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Solutions to the Second Midterm Exam

CSCI 136 Data Structures & Advanced Programming. Lecture 9 Fall 2018 Instructors: Bills

Practice Sheet 2 Solutions

Go Bears! IE170: Algorithms in Systems Engineering: Lecture 4

Data Structures and Algorithm Analysis (CSC317) Introduction: sorting as example

k-selection Yufei Tao Department of Computer Science and Engineering Chinese University of Hong Kong

Data Structures and Algorithm Analysis (CSC317) Introduction: sorting as example

Reasoning about programs

Lecture Notes: Hoare Logic

Last time. Reasoning about programs. Coming up. Project Final Presentations. This Thursday, Nov 30: 4 th in-class exercise

Lecture 6 Sorting and Searching

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort

Biostatistics 615/815 Lecture 5: More on STLs, and Divide and Conquer Algorithms

Program Verification. Program Verification 307/434

Recall our recursive multiply algorithm:

Parallel Algorithms. 6/12/2012 6:42 PM gdeepak.com 1

Integers and Mathematical Induction

Unit 10: Sorting/Searching/Recursion

Transcription:

COMP250: Loop invariants Jérôme Waldispühl School of Computer Science McGill University Based on (CRLS, 2009) & slides from (Sora,2015)

Algorithm specification An algorithm is described by: Input data Output data Preconditions: specifies restrictions on input data Postconditions: specifies what is the result Example: Binary Search Input data: a:array of integer; x:integer; Output data: index:integer; Precondition: a is sorted in ascending order Postcondition: index of x if x is in a, and -1 otherwise.

Correctness of an algorithm An algorithm is correct if: for any correct input data: it stops and it produces correct output. Correct input data: satisfies precondition Correct output data: satisfies postcondition Problem: Proving the correctness of an algorithm may be complicated when the latter is repetitive or contains loop instructions.

How to prove the correctness of an algorithm? Recursive algorithm Induction proofs Iterative algorithm (For loops)???

Loop invariant A loop invariant is a loop property that hold before and after each iteration of a loop.

Proof using loop invariants We must show: 1. Initialization: It is true prior to the first iteration of the loop. 2. Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration. 3. Termination: When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct.

Analogy to induction proofs Using loop invariants is like mathematical induction. You prove a base case and an inductive step. Showing that the invariant holds before the first iteration is like the base case. Showing that the invariant holds from iteration to iteration is like the inductive step. The termination part differs from classical mathematical induction. Here, we stop the ``induction when the loop terminates instead of using it infinitely. We can show the three parts in any order.

Insertion sort for i 1 to length(a) - 1 j i while j > 0 and A[j-1] > A[j] swap A[j] and A[j-1] j j - 1 end while end for (Seen in Lecture 9)

Insertion sort 1 3 5 6 2 4 n elements already sorted New element to sort 1 2 3 5 6 4 n+1 elements sorted

Loop invariant The array A[0 i-1] is fully sorted. 1 3 5 6 2 4 A[0 i-1] i

Initialization Just before the first iteration (i = 1), the sub-array A[0 i 1] is the single element A[0], which is the element originally in A[0], and it is trivially sorted. 1 3 5 6 2 4 i=1

Maintenance Iteration i 1 3 5 6 2 4 n elements already sorted i Iteration i+1 1 2 3 5 6 4 n+1 elements sorted i+1 Note: To be precise, we would need to state and prove a loop invariant for the ``inner while loop.

Termination The outer for loop ends when i length(a) and increment by 1 at each iteration starting from 1. Therefore, i = length(a). Plugging length(a) in for i 1 in the loop invariant, the subarray A[0 length(a)-1] consists of the elements originally in A[0 length(a)-1] but in sorted order. A[0 length(a)-1] contains length(a) elements (i.e. all initial elements!) and no element is duplicated/deleted. In other words, the entire array is sorted.

Merge Sort MERGE-SORT(A,p,r) if p < r then q=(p+r)/2 MERGE-SORT(A,p,q) MERGE-SORT(A,q+1,r) MERGE(A,p,q,r) p q r Precondition: Array A has at least 1 element between indexes p and r (p r) Postcondition: The elements between indexes p and r are sorted

Merge Sort (Merge method) MERGE-SORT calls a function MERGE(A,p,q,r) to merge the sorted subarrays of A into a single sorted one The proof of MERGE can be done separately, using loop invariants MERGE (A,p,q,r) Precondition: A is an array and p, q, and r are indices into the array such that p <= q < r. The subarrays A[p.. q] and A[q +1.. r] are sorted Postcondition: The subarray A[p..r] is sorted

Procedure Merge Merge(A, p, q, r) 1 n 1 q p + 1 2 n 2 r q 3 for i 1 to n 1 4 do L[i] A[p + i 1] 5 for j 1 to n 2 6 do R[j] A[q + j] 7 L[n 1 +1] 8 R[n 2 +1] 9 i 1 10 j 1 11 for k p to r 12 do if L[i] R[j] 13 then A[k] L[i] 14 i i + 1 15 else A[k] R[j] 16 j j + 1 Input: Array containing sorted subarrays A[p..q] and A[q+1..r]. Output: Merged sorted subarray in A[p..r].

Merge/combine Example A 1 6 8 9 26 32 42 43 L 6 8 26 32 1 9 42 43 R Idea: The lists L and R are already sorted.

Correctness proof for Merge Loop Invariant: The array A[p,k] stored the (k-p+1) smallest elements of L and R sorted in increasing order. Initialization: k = p A contains a single element (which is trivially sorted ) A[p] is the smallest element of L and R Maintainance: Assume that Merge satisfy the loop invariant property until k. (k-p+1) smallest elements of L and R are already sorted in A. Next value to be inserted is the smallest one remaining in L and R. This value is large than those previously inserted in A. Loop invariant property satisfied for k+1. Termination Step: Merge terminates when k=r, thus when r-p+1 elements have been inserted in A All elements are sorted.

Correctness proof for Merge Sort Recursive property: Elements in A[p,r] are be sorted. Base Case: n = 1 A contains a single element (which is trivially sorted ) Inductive Hypothesis: Assume that MergeSort correctly sorts n=1, 2,..., k elements Inductive Step: Show that MergeSort correctly sorts n = k + 1 elements. Termination Step: MergeSort terminate and all elements are sorted. Note: There is no loop here, but we include this to complete the proof of Merge Sort.

Maintenance Inductive Hypothesis: Assume MergeSort correctly sorts n=1,..., k elements Inductive Step: Show that MergeSort correctly sorts n = k + 1 elements. Proof: First recursive call n 1 =q-p+1=(k+1)/2 k => subarray A[p.. q] is sorted Second recursive call n 2 =r-q=(k+1)/2 k => subarray A[q+1.. r] is sorted A, p q, r fulfill now the precondition of Merge The post-condition of Merge guarantees that the array A[p.. r] is sorted => post-condition of MergeSort satisfied.

Termination Step We have to find a quantity that decreases with every recursive call: the length of the subarray of A to be sorted MergeSort. At each recursive call of MergeSort, the length of the subarrayis strictly decreasing. When MergeSort is called on a array of size 1 (i.e. the base case), the algorithm terminates without making additional recursive calls. Calling MergeSort(A,0,n) returns a fully sorted array.