Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.

Slides:



Advertisements
Similar presentations
Order Analysis of Algorithms Debdeep Mukhopadhyay IIT Madras.
Advertisements

CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis.
2. Getting started Hsu, Lih-Hsing. Computer Theory Lab. Chapter 2P Insertion sort Example: Sorting problem Input: A sequence of n numbers Output:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
COMP 171 Data Structures and Algorithms Tutorial 4 In-Class Exercises: Algorithm Design.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Insertion Sort.
CS421 - Course Information Website Syllabus Schedule The Book:
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 7 Heapsort and priority queues Motivation Heaps Building and maintaining heaps.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Computer Programming Sorting and Sorting Algorithms 1.
Fall 2008 Insertion Sort – review of loop invariants.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Analysis of Algorithms CS 477/677
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
DAST 2005 Tirgul 6 Heaps Induction. DAST 2005 Heaps A binary heap is a nearly complete binary tree stored in an array object In a max heap, the value.
Introduction CIS 606 Spring The sorting problem Input: A sequence of n numbers 〈 a 1, a 2, …, a n 〉. Output: A permutation (reordering) 〈 a’ 1,
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
Introduction to Algorithm design and analysis
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Reading and Writing Mathematical Proofs
Algorithm Correctness A correct algorithm is one in which every valid input instance produces the correct output. The correctness must be proved mathematically.
26 Sep 2014Lecture 3 1. Last lecture: Experimental observation & prediction Cost models: Counting the number of executions of Every single kind of command.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Lecture 1: Introduction and Overview CSCI 700 – Algorithms 1.
BY Lecturer: Aisha Dawood.  an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
MS 101: Algorithms Instructor Neelima Gupta
Getting Started Introduction to Algorithms Jeff Chastine.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
September 9, Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
1 Algorithms CSCI 235, Fall 2015 Lecture 11 Elementary Sorts.
Algorithms CSCI 235, Fall 2015 Lecture 12 Elementary Sorts II
Lecture 4 1 Advance Analysis of Algorithms. Selection Sort 2 Summary of Steps Find the smallest element in the array Exchange it with the element in the.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000.
Discrete Maths: Invariant/2 1 Discrete Maths Objectives – –to show the use of induction for proving properties of code involving loops use induction.
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
Help Session 3: Induction and Complexity Ezgi, Shenqi, Bran.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
CS 3343: Analysis of Algorithms
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
CS 583 Analysis of Algorithms
Proving correctness.
Ch 7: Quicksort Ming-Te Chi
Analysis of Algorithms
Algorithms Sorting.
CS 583 Analysis of Algorithms
Lecture No 5A Advance Analysis of Institute of Southern Punjab Multan
Algorithms and Data Structures Lecture II
Presentation transcript:

Proving correctness

Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant provides important property that is used to show correctness Steps of proof:  Initialization (similar to induction base)  Maintenance (similar to induction proof)  Termination

More on the steps Initialization: Show loop invariant is true before (or at start of) the first execution of a loop Maintenance: Show that if the loop invariant is true before an iteration of a loop it is true before the next iteration Termination: When the loop terminates, the invariant gives us an important property that helps show the algorithm is correct

Example: Finding maximum Findmax(A, n) maximum = A[0]; for (i = 1; i < n; i++) if (A[i] > maximum) maximum= A[i] return maximum What is a loop invariant for this code?

Proof of correctness Loop invariant for Findmax(A): “ Before the i th iteration (for i = 1, …, n) of the for loop maximum = max{A[0], A[1], …, A[i – 1]} ”

Initialization We need to show loop invariant is true at the start of the execution of the for loop Line 1 sets maximum=A[0] = max{A[i]|i=0, …,1-1} (Note: j=1) So the loop invariant is satisfied at the start of the for loop.

Maintenance Assume that at the start of the i th iteration of the for loop maximum = max{A[j] | j = 0, …, i - 1} We will show that before the (i + 1)th iteration maximum =max{A[j] | j = 0, …, i} The code computes maximum=max(maximum, A[i]) = max(max{A[j] | j = 0, …, i - 1}, A[i]) = max{A[j] | j = 0, …, i}

Termination i = n. So maximum = max{A[j]|j=0, …,n - 1}

Example: Insertion sort INSERTION-SORT(A) 1 for j  2 to length[A] 2 key  A[j] 3 // Insert A[j] into sorted A[1.. j - 1] 4 i  j – 1 5 while i > 0 and A[i] > key 6 A[i + 1]  A[i] 7 i  i – 1 8 A[i + 1]  key

Proof of correctness Loop invariant for INSERTION-SORT(A): “ At the start of the j th iteration (for j = 2, …, length[A]) of the for loop  A[1.. j -1] contains the elements originally in A[1.. j -1] and  A[1.. j -1] is sorted ”

Initialization We need to show loop invariant is true at the start of the execution of the for loop After line 1 sets j = 2 and before it compares j to length[A] we have:  Subarray A[ ]= A[1] contains the original element in A[1]  A[1] is sorted. So the loop invariant is satisfied

Maintenance If loop invariant is true before this execution of a loop it is true before the next execution Assume that at the start of the jth iteration of the for loop A[1.. j -1] contains the elements originally in A[1.. j - 1] and A[1.. j -1] is sorted

Maintenance We will show that the loop invariant is maintained before the (j + 1)th iteration.  We will show that at the start of the (j + 1)th iteration of the for loop A[1.. j] contains the elements originally in A[1.. j] and A[1.. j] is sorted

Maintenance For a more formal proof we need a loop assertion for the while loop We will be less formal and observe that the body of the loop keeps moving to the left until the proper position for A[j] is found and then inserts A[j] into the sub array A[1.. j ]. So:  the sub array A[1.. j] contains the elements originally in A[1.. j] and A[1.. j] is sorted

Termination j = length[A] + 1. The array A[1.. length[A]] contains the elements originally in A[1.. length[A]] and A[1.. length[A]] is SORTED!

Loop invariants 1.sum =0; 2.for (i = 0; i < n; i++) 3. sum = sum + A[i]; What is a loop invariant for this code?