Proving correctness.

Slides:



Advertisements
Similar presentations
Divide and Conquer (Merge Sort)
Advertisements

Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
A Basic Study on the Algorithm Analysis Chapter 2. Getting Started 한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님 1.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
COMP 171 Data Structures and Algorithms Tutorial 4 In-Class Exercises: Algorithm Design.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
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
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
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.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
BY Lecturer: Aisha Dawood.  an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces.
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
September 9, Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
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.
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.
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.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Proving the Correctness of Algorithms
Algorithms Sorting – Part 3.
Sort Algorithm.
Analysis of Algorithms CS 477/677
16 Searching and Sorting.
CS212: Data Structures and Algorithms
Math/CSE 1019C: Discrete Mathematics for Computer Science Fall 2012
Merging Merge. Keep track of smallest element in each sorted half.
Algorithms CSCI 235, Fall 2017 Lecture 13 Elementary Sorts II
Algorithm Design & Analysis
CS 3343: Analysis of Algorithms
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
Analysis of Bubble Sort and Loop Invariant
CS 583 Analysis of Algorithms
Ch 7: Quicksort Ming-Te Chi
Divide and Conquer (Merge Sort)
Ch 2: Getting Started Ming-Te Chi
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS 583 Analysis of Algorithms
Divide and Conquer (Merge Sort)
Algorithms: the big picture
Ch. 2: Getting Started.
Divide & Conquer Algorithms
Topic: Divide and Conquer
Analysis of Algorithms
Algorithms Sorting.
CS 583 Analysis of Algorithms
The Selection Problem.
Lecture No 5A Advance Analysis of Institute of Southern Punjab Multan
Algorithms CSCI 235, Spring 2019 Lecture 19 Order Statistics
Algorithms CSCI 235, Spring 2019 Lecture 13 Elementary Sorts II
nalysis of lgorithms A A Universidad Nacional de Colombia
Quicksort Quick sort Correctness of partition - loop invariant
Algorithms and Data Structures Lecture II
Presentation transcript:

Proving correctness

Proving correctness Proof based on loop invariants Steps of proof: 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 execution of a loop, it is also true before the next execution 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): “At the start of the j th iteration (for j = 1, … , n) of the for loop maximum = max{A[i]| i = 0, …, j - 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 jth iteration of the for loop maximum = max{A[i] | i = 0, …, j - 1} We will show that before the (j + 1)th iteration maximum =max{A[i] | i = 0, …, j} The code computes maximum=max(maximum, A[j]) =max(max{A[i] | i= 0, …, j - 1}, A[j]) = max{A[i] | i = 0, …, j}

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

Example: Insertion sort INSERTION-SORT(A) for j  2 to length[A] key  A[j] // Insert A[j] into sorted A[1.. j - 1] i  j – 1 while i > 0 and A[i] > key A[i + 1]  A[i] i  i – 1 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[1.. 2 - 1]= 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 moves A[j - 1], A[j - 2], etc., to the right 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?

Example: Merge procedure of merge-sort algorithm

Proof of correctness Loop invariant for MERGE(A, p, q, r): At the start of the k th iteration (line 12) of the for loop A[p.. k-1] contains k-p smallest elements of L[1…n1 + 1] and R[1…n2 +1] A[p.. k -1] is sorted L[i] and R[j] are the smallest elements of their arrays that have not been copied into A

Initialization We need to show loop invariant is true at the start of the execution of the for loop Prior to the first iteration (k=p) A[p…k-1] is empty; it contains k-p = 0 smallest elements of L and R i=j=1 L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A 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 kth iteration of the for loop, A[p.. k -1] contains the k-p smallest elements After the kth iteration, L[i] is copied into A[k] (assume L[i]<R[j]; otherwise, R[j] is copied to A[k]), then A[p..k] contains k-p+1 smallest element;

Termination k = r + 1. A[p…k-1] is A[p…k] Contains the k-p = r-p+1 smallest elements of L[ ] and R[ ], in sorted order L[ ] and R[ ] have all been copied to A[ ], except two largest elements (infinite), which are used as the sentinels.