Proving the Correctness of Algorithms Algorithm Design and Analysis 2015 - Week 2

Slides:



Advertisements
Similar presentations
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Advertisements

Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Mathematical Induction II Lecture 14: Nov
Lecture 4 Divide and Conquer for Nearest Neighbor Problem
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Revisiting Induction Steve Wolfman, based on work by Patrice Belleville and others 1.
Discrete Mathematics Lecture 4: Sequences and Mathematical Induction
Discrete Mathematics Lecture 4 Harper Langston New York University.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
Analysis of Algorithms
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank. Modified By Mingwu Chen Induction.
22C:19 Discrete Math Induction and Recursion Fall 2011 Sukumar Ghosh.
Induction and recursion
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
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.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Reading and Writing Mathematical Proofs
1 Program Correctness CIS 375 Bruce R. Maxim UM-Dearborn.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
MS 101: Algorithms Instructor Neelima Gupta
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Discrete Mathematics Lecture 4 Alexander Bukharovich New York University.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
Reasoning about programs March CSE 403, Winter 2011, Brun.
1 Inductive Proofs Rosen 6 th ed., § Mathematical Induction A powerful, rigorous technique for proving that a predicate P(n) is true for every.
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
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.
Recursion and Induction  For advanced algorithm development, recursion is an essential design technique Recursive Procedures What is a Proof? Induction.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
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.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
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
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Induction and Recursion CSC-2259 Discrete Structures Konstantin Busch - LSU1.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Proving the Correctness of Algorithms
CPSC 121: Models of Computation 2008/9 Winter Term 2
CS2210:0001Discrete Structures Induction and Recursion
Mathematical Induction II
Reasoning About Code.
Proving Properties of Recursive List Functions
CS 3343: Analysis of Algorithms
Discrete Mathematics and its Applications
Applied Discrete Mathematics Week 9: Integer Properties
Predicate Transformers
Divide and Conquer (Merge Sort)
Algorithms and Data Structures Lecture II
Presentation transcript:

Proving the Correctness of Algorithms Algorithm Design and Analysis Week 2

Lecture Outline Proving the Correctness of Algorithms –Preconditions and Postconditions –Loop Invariants –Induction – Math Review –Using Induction to Prove Algorithms

What does an algorithm ? 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: found:boolean; –Precondition: a is sorted in ascending order –Postcondition: found is true if x is in a, and found is false otherwise

Correct algorithms 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

Proving correctness An algorithm = a list of actions Proving that an algorithm is totally correct: 1.Proving that it will terminate 2.Proving that the list of actions applied to the precondition imply the postcondition –This is easy to prove for simple sequential algorithms –This can be complicated to prove for repetitive algorithms (containing loops or recursivity) use techniques based on loop invariants and induction

Example – a sequential algorithm Swap1(x,y): aux := x x := y y := aux Precondition: x = a and y = b Postcondition: x = b and y = a Proof: the list of actions applied to the precondition imply the postcondition 1.Precondition: x = a and y = b 2.aux := x => aux = a 3.x : = y => x = b 4.y := aux => y = a 5.x = b and y = a is the Postcondition

Example – a repetitive algorithm Proof: the list of actions applied to the precondition imply the postcondition BUT: we cannot enumerate all the actions in case of a repetitive algorithm ! We use techniques based on loop invariants and induction Algorithm Sum_of_N_numbers Input: a, an array of N numbers Output: s, the sum of the N numbers in a s:=0; k:=0; While (k<N) do k:=k+1; s:=s+a[k]; end

Loop invariants A loop invariant is a logical predicate such that: if it is satisfied before entering any single iteration of the loop then it is also satisfied after the iteration

Example: Loop invariant for Sum of n numbers Algorithm Sum_of_N_numbers Input: a, an array of N numbers Output: s, the sum of the N numbers in a s:=0; k:=0; While (k<N) do k:=k+1; s:=s+a[k]; end Loop invariant = induction hypothesis: At step k, S holds the sum of the first k numbers

Using loop invariants in proofs We must show the following 3 things about a loop invariant: 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.

Example: Proving the correctness of the Sum algorithm (1) Induction hypothesis: S= sum of the first k numbers 1.Initialization: The hypothesis is true at the beginning of the loop: Before the first iteration: k=0, S=0. The first 0 numbers have sum zero (there are no numbers) => hypothesis true before entering the loop

Example: Proving the correctness of the Sum algorithm (2) Induction hypothesis: S= sum of the first k numbers 2.Maintenance: If hypothesis is true before step k, then it will be true before step k+1 (immediately after step k is finished) We assume that it is true at beginning of step k: “S is the sum of the first k numbers” We have to prove that after executing step k, at the beginning of step k+1: “S is the sum of the first k+1 numbers” We calculate the value of S at the end of this step K:=k+1, s:=s+a[k+1] => s is the sum of the first k+1 numbers

Example: Proving the correctness of the Sum algorithm (3) Induction hypothesis: S= sum of the first k numbers 3.Termination: When the loop terminates, the hypothesis implies the correctness of the algorithm The loop terminates when k=n=> s= sum of first k=n numbers => postcondition of algorithm, DONE

Loop invariants and induction Proving loop invariants is similar to mathematical induction: –showing that the invariant holds before the first iteration corresponds to the base case, and –showing that the invariant holds from iteration to iteration corresponds to the inductive step.

Mathematical induction - Review Let T be a theorem that we want to prove. T includes a natural parameter n. Proving that T holds for all natural values of n is done by proving following two conditions: 1.T holds for n=1 2.For every n>1 if T holds for n-1, then T holds for n Terminology: T= Induction Hypothesis 1= Base case 2= Inductive step

Mathematical induction - review Strong Induction: a variant of induction where the inductive step builds up on all the smaller values Proving that T holds for all natural values of n is done by proving following two conditions: 1.T holds for n=1 2.For every n>1 if T holds for all k<= n-1, then T holds for n

Mathematical induction – Example1 Theorem: The sum of the first n natural numbers is n*(n+1)/2 Proof: by induction on n 1.Base case: If n=1, s(1)=1=1*(1+1)/2 2.Inductive step: We assume that s(n)=n*(n+1)/2, and prove that this implies s(n+1)=(n+1)*(n+2)/2, for all n>=1 s(n+1)=s(n)+(n+1)=n*(n+1)/2+(n+1)=(n+1)*(n+2)/2

Mathematical induction – Example2 Theorem: Every amount of postage that is at least 12 cents can be made from 4-cent and 5- cent stamps. Proof: by induction on the amount of postage Postage (p) = m * 4 + n * 5 Base case: –Postage(12) = 3 * * 5 –Postage(13) = 2 * * 5 –Postage(14) = 1 * * 5 –Postage(15) = 0 * * 5

Mathematical induction – Example2 (cont) Inductive step: We assume that we can construct postage for every value from 12 up to k. We need to show how to construct k + 1 cents of postage. Since we have proved base cases up to 15 cents, we can assume that k + 1 ≥ 16. Since k+1 ≥ 16, (k+1)−4 ≥ 12. So by the inductive hypothesis, we can construct postage for (k + 1) − 4 cents: (k + 1) − 4 = m * 4+ n * 5 But then k + 1 = (m + 1) * 4 + n * 5. So we can construct k + 1 cents of postage using (m+1) 4-cent stamps and n 5-cent stamps

Correctness of algorithms Induction can be used for proving the correctness of repetitive algorithms: –Iterative algorithms: Loop invariants –Induction hypothesis = loop invariant = relationships between the variables during loop execution –Recursive algorithms Direct induction –Hypothesis = a recursive call itself ; often a case for applying strong induction

Example: Correctness proof for Decimal to Binary Conversion Algorithm Decimal_to_Binary Input: n, a positive integer Output: b, an array of bits, the bin repr. of n, starting with the least significant bits t:=n; k:=0; While (t>0) do k:=k+1; b[k]:=t mod 2; t:=t div 2; end It is a repetitive (iterative) algorithm, thus we use loop invariants and proof by induction

Example: Loop invariant for Decimal to Binary Conversion Algorithm Decimal_to_Binary Input: n, a positive integer Output: b, an array of bits, the bin repr. of n t:=n; k:=0; While (t>0) do k:=k+1; b[k]:=t mod 2; t:=t div 2; end At step k, b holds the k least significant bits of n, and the value of t, when shifted by k, corresponds to the rest of the bits b k k-1

Example: Loop invariant for Decimal to Binary Conversion Algorithm Decimal_to_Binary Input: n, a positive integer Output: b, an array of bits, the bin repr. of n t:=n; k:=0; While (t>0) do k:=k+1; b[k]:=t mod 2; t:=t div 2; end Loop invariant: If m is the integer represented by array b[1..k], then n=t*2 k +m b k k-1

Example: Proving the correctness of the conversion algorithm Induction hypothesis=Loop Invariant: If m is the integer represented by array b[1..k], then n=t*2^k+m To prove the correctness of the algorithm, we have to prove the 3 conditions: 1.Initialization: The hypothesis is true at the beginning of the loop 2.Maintenance: If hypothesis is true for step k, then it will be true for step k+1 3.Termination: When the loop terminates, the hypothesis implies the correctness of the algorithm

Example: Proving the correctness of the conversion algorithm (1) Induction hypothesis: If m is the integer represented by array b[1..k], then n=t*2^k+m 1.The hypothesis is true at the beginning of the loop: k=0, t=n, m=0(array is empty) n=n*2^0+0

Example: Proving the correctness of the conversion algorithm (2) Induction hypothesis: If m is the integer represented by array b[1..k], then n=t*2^k+m 2.If hypothesis is true for step k, then it will be true for step k+1 At the start of step k: assume that n=t*2^k+m, calculate the values at the end of this step If t=even then: t mod 2==0, m unchanged, t=t / 2, k=k+1=> (t / 2) * 2 ^ (k+1) + m = t*2^k+m=n If t=odd then: t mod 2 ==1, b[k+1] is set to 1, m=m+2^k, t=(t-1)/2, k=k+1 => (t-1)/2*2^(k+1)+m+2^k=t*2^k+m=n

Example: Proving the correctness of the conversion algorithm (3) Induction hypothesis: If m is the integer represented by array b[1..k], then n=t*2^k+m 3.When the loop terminates, the hypothesis implies the correctness of the algorithm The loop terminates when t=0 => n=0*2^k+m=m n==m, proved

Proof of Correctness for Recursive Algorithms In order to prove recursive algorithms, we have to: 1.Prove the partial correctness (the fact that the program behaves correctly) we assume that all recursive calls with arguments that satisfy the preconditions behave as described by the specification, and use it to show that the algorithm behaves as specified 2.Prove that the program terminates any chain of recursive calls eventually ends and all loops, if any, terminate after some finite number of iterations.

Example - Merge Sort MERGE-SORT(A,p,r) if p < r q= (p+r)/2 MERGE-SORT(A,p,q) MERGE-SORT(A,q+1,r) MERGE(A,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 p rq

Example - Merge Sort 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 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 (which is an iterative function) can be done separately, using loop invariants We assume here that MERGE has been proved to fulfill its postconditions (can do it as a distinct exercise)

Correctness proof for Merge-Sort Number of elements to be sorted: n=r-p+1 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. –First recursive call n1=q-p+1=(k+1)/2 subarray A[p.. q] is sorted –Second recursive call n2=r-q=(k+1)/2 subarray A[q+1.. r] is sorted –A, p q, r fulfill now the precondition of Merge –The postcondition of Merge guarantees that the array A[p.. r] is sorted => postconsition of MergeSort

Correctness proof for Merge-Sort Termination: –To argue termination, we have to find a quantity that decreases with every recursive call: the length of the part of A considered by a call to MergeSort –For the base case, we have a one-element array. the algorithm terminates in this case without making additional recursive calls.

Correctness proofs for recursive algorithms Base Case: Prove that RECURSIVE works for n = small_value Inductive Hypothesis: – Assume that RECURSIVE works correctly for n=small_value,..., k Inductive Step: –Show that RECURSIVE works correctly for n = k + 1 RECURSIVE(n) is if (n=small_value) return ct else RECURSIVE(n1) … RECURSIVE(nr) some_code n 1, n 2, … n r are some values smaller than n but bigger than small_value

Lecture Summary Proving that an algorithm is totally correct means: 1.Proving that it will terminate 2.Proving that the list of actions applied to the precondition imply the postcondition How to prove repetitive algorithms: –Iterative algorithms: use Loop invariants, Induction –Recursive algorithms: use induction using as hypothesis the recursive call

Bibliography Proving correctness of algorithms, Induction: –[Manber] - chap 2 –[CLRS] – chap 2.1, chap Course website (Slides, Labs) –