Recall our recursive multiply algorithm:

Similar documents
Mathematical Induction

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

CSE 20 DISCRETE MATH WINTER

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

5.3 Recursive definitions and structural induction

15 212: Principles of Programming. Some Notes on Induction

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

15 150: Principles of Functional Programming Sorting Integer Lists

ASYMPTOTIC COMPLEXITY

II (Sorting and) Order Statistics

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

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

Functional Programming in Haskell Part I : Basics

Hoare Logic: Proving Programs Correct

Ramsey s Theorem on Graphs

CS 310 Advanced Data Structures and Algorithms

Lamé s Theorem. Strings. Recursively Defined Sets and Structures. Recursively Defined Sets and Structures

Fall Recursion and induction. Stephen Brookes. Lecture 4

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

Integers and Mathematical Induction

Cisc320 Homework 3 Solutions. Question 1: Original. Insert 170. Insert 34. TA: Matt Saponaro

Arguing for program correctness and writing correct programs

Binary Search to find item in sorted array

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

Today. Types of graphs. Complete Graphs. Trees. Hypercubes.

Reasoning about programs

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

10/9/17. Using recursion to define objects. CS 220: Discrete Structures and their Applications

Computer Science 236 Fall Nov. 11, 2010

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

9.5 Equivalence Relations

Assigning Meaning to Programs, Floyd. Proving Termination with Multiset Orderings

CS126 Final Exam Review

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Today. Finish Euclid. Bijection/CRT/Isomorphism. Review for Midterm.

Chapter 3 (part 3) Describing Syntax and Semantics

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

UNIT 5C Merge Sort Principles of Computing, Carnegie Mellon University

Assertions & Verification & Example Loop Invariants Example Exam Questions

Lecture 22 Tuesday, April 10

CS 220: Discrete Structures and their Applications. Recursive objects and structural induction in zybooks

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

4 Generating functions in two variables

ASYMPTOTIC COMPLEXITY

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Overview. A mathema5cal proof technique Proves statements about natural numbers 0,1,2,... (or more generally, induc+vely defined objects) Merge Sort

Discrete Structures. Fall Homework3

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

Assertions & Verification Example Exam Questions

8. Iteration: Strings

Softwaretechnik. Program verification. Albert-Ludwigs-Universität Freiburg. June 28, Softwaretechnik June 28, / 24

Solutions to the Second Midterm Exam

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

Test 1, Spring 2013 ( Solutions): Provided by Jeff Collins and Anil Patel. 1. Axioms for a finite AFFINE plane of order n.

Lecture Notes on Induction and Recursion

Sorting is a problem for which we can prove a non-trivial lower bound.

Binary Search. Roland Backhouse February 5th, 2001

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

Basic Verification Strategy

Do you remember any iterative sorting methods? Can we produce a good sorting method by. We try to divide and conquer: break into subproblems

Recurrences and Divide-and-Conquer

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9

1 Variations of the Traveling Salesman Problem

Recursion and Structural Induction

Review: Hoare Logic Rules

Recursive Definitions Structural Induction Recursive Algorithms

2009 HMMT Team Round. Writing proofs. Misha Lavrov. ARML Practice 3/2/2014

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Adjacent: Two distinct vertices u, v are adjacent if there is an edge with ends u, v. In this case we let uv denote such an edge.

Solution for Homework set 3

Lecture 6,

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

1 / 43. Today. Finish Euclid. Bijection/CRT/Isomorphism. Fermat s Little Theorem. Review for Midterm.

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

Math 15 - Spring Homework 5.2 Solutions

Lecture Notes on Linear Search

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

Solution to Graded Problem Set 4

Data Structures and Algorithms CMPSC 465

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.

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Unit #2: Recursion, Induction, and Loop Invariants

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

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström

1 The sorting problem

Lab 6 Sorting. Sup Biotech 3 Python. Pierre Parutto

Lecture 4 CS781 February 3, 2011

Reading for this lecture (Goodrich and Tamassia):

P1 Engineering Computation

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

CS 161 Computer Security

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

TECH. Recurrence Equations vs. Recursive Procedures. Recursion and Induction. e.g. Fibonacci Function. The working of recursive procedure

CORRECTNESS ISSUES AND LOOP INVARIANTS

Transcription:

Recall our recursive multiply algorithm: PRECONDITION: x and y are both binary bit arrays of length n, n a power of 2. POSTCONDITION: Returns a binary bit array equal to the product of x and y. REC MULTIPLY 2( x, y): if (len(x) == 1): return (x[0]*y[0]) xl = x[n/2:n] xh = x[0:n/2] yl = x[n/2:n] yh = x[0:n/2] p1 = REC MULTIPLY 2(xh, yh) p2 = REC MULTIPLY 2(xh+xl, yh+yl) p3 = REC MULTIPLY 2(xl, yl) p2 = BINARY ADD(p2,-p1, -p3) p2 = SHIFT(p2, n/2) p1 = SHIFT(p1,n) return BINARY ADD(p1,p2,p3) 12

Let s prove that REC MULTIPLY 2(x,y) does indeed return the product of x and y. Proof by complete induction. 1. Define P (n): Rec Multiply 2( x, y) meets the postcondition, i.e., terminates and returns the product of x and y where x and y are n bit binary numbers and 9k 2 N,n=2 k. RTP: P (n) is true for all even n 2 N and n =1. 2. Base Case: n =1Then xy is returned. 3. Inductive Hypothesis: Assume that for arbitrary n 2 N,n =2 k, that P (m) is true for all m<n, m a power of 2. 4. Inductive Step: By the IH, each call to Rec Multiply 2 correctly returns the product of the parameters. The is guaranteed since each parameter is a power of 2 and <n. by Gauss observation, the sum p 1 2 n +(p 2 p 1 p 3 ) 2 n/2 + p 3 = xy. Therefore, Rec Multiply 2 terminates and returns xy meeting the post-condition. 13

Another Recursive Algorithm Quicksort PRECONDITION: A is an array/list of integers. POSTCONDITION: Returns the integers of A sorted in increasing order. def QUICKSORT(A): if (len(a)==1 or len(a)==0): return (A) else: # make A[0] the pivot L, M, U = [], [], [] for value in A: if (A[0] < value): L.append(value) elif (A[0] == value): M.append(value) else: U.append(value) final = QUICKSORT(L) final.append(m) final.append(quicksort(u)) return(final) end Quicksort Q: Which type of induction should we use to prove that Quicksort is correct? complete induction. 14

Correctness of Quicksort Proof. By complete induction on n = len(a). 1. Define P (n): Let P (n) be Given a non-empty array A where n = len(a), Quicksort terminates and returns a list containing the integers in A sorted in increasing order. 2. IH: Suppose that P (k) holds for all 0 apple k<n. 3. Case 1. n =0or n =1 If n =0or 1, then QUICKSORT terminates and returns A. Notice That A is sorted in increasing order and contains the integers of A. 4. Case 2. n 2 Notice that every value in A belongs to exactly one of L, M or U. In addition, every value in L is less than every value in M and every value in M is less than every value in U. Notice that len(l(< len(a) =n and that len(u) <len(a) = n so U and L satisfy the precondition. Therefore, we can apply the IH, and Quicksort terminates and returns U and L each sorted in increasing order. Therefore, the concatenation of L, M, U contains the integers of A sorted in increasing order satisfying the postcondition. 15

Correctness of Iterative Algorithms Q: What is an iterative algorithm? One that has a loop...i.e., not recursive. Typical Iterative Algorithm Structure Precondition: Postcondition: Requirements about the input. Requirements about the output. variable declarations various statements begin loop more statements. exit statement more statements end loop more statements Q: Which part of the algorithm is the hardest to prove correct? the loop. To prove an algorithm is correct, we need to prove that the looping portion has a well defined behavior and that this behavior ensures that the postcondition is met. The well defined behavior is called a loop invariant. 16

Q: How do we express a loop invariant? It is a statement P (n) that is true for the n th iteration of the loop. Q: How does the postcondition relate to P (n)? If the loop terminates on the k th iteration, then P (k) should meet the postcondition given the other commands after loop termination. Q: Does this remind you of something else we have seen? Proof by induction. Keys to Proving an Iterative Algorithm Correct We will use a 3-step process. 1. Partial Correctness Assume that the program (loop) terminates and show that the loop satisfies an invariant 2. Proof of Termination Prove that given the precondition, the program terminates. 3. Total Correctness Now that we KNOW that the program terminates, use the loop invariant to show that when it terminates, the values of the variables are as required by the postcondition. 17

A Toy Example PRECONDITION: Input is a natural number x. POSTCONDITION: Output is 2 x. def POWER( x ): current = 1 count = 0 while ( count < x ): current = current*2 count = count+1 return(current) Q: What is the exit condition? count < x Q: What is the invariant? ie., what is true each iteration of the loop? Let s look at a few iterations of the loop to find a pattern: n P(n) 0 current = 1 count = 0 1 current = 2 count =1 2 current =4 count =2 3 current =8 count =3 4. current = 16 count =4 i current = 2 i count =i 18

Notation: We will represent the value of a variable x during the k th iteration of the loop by x k. What is the loop invariant P (k)? P (k): If the k th iteration of the loop exists then current = 2 k and count = k. Q: How is this related to the postcondition? When the loop terminates, x=count and so 2 count =2 x = current. Partial Correctness: Prove P (k) true. Proof. Base Case: Before we enter the loop, current =1=2 0 and count = 0, therefore P (0) holds. Inductive Hypothesis Assume that P(i) holds for arbitrary iteration number i, for i 2 N Inductive Step If there is not an (i + 1) st iteration then P (i + 1) is trivially true. why??. Otherwise, there exists an (i + 1) st iteration: current i+1 = current i 2 count [ by i+1 line 5 = ] count i +1 by line 6 = 2 i 2 [ by IH ] = i +1 by IH = 2 i+1 Therefore P (i) holds for all i 2 N. 19

Showing Termination Theorem 2.5 (in the notes) Every decreasing sequence of natural numbers is finite. Q: How does Theorem 2.5 follow from the Well Ordering Principle? think of the values in the sequence as belonging to a set, then the set has a least element, so the sequence must terminate. Consider defining d i = x - count i. What do we know about d i versus d i+1 : d i forms a decreasing sequence of x, x 1,x 2,...,0. How does the exit condition relate to d i? exit condition is that d i > 0. How do we kow that the loop must terminate? the d i form a decreasing sequence, d i 0, so by Theorem 2.5, d i =0 for some i and the loop must exit. Q: Given that the loop invariant holds and that the loop terminates, is the post condition met when the exit condition is true? If the exit condition is true, then x = count. Also by the loop invariant, count = k, and current k =2 k =2 count =2 x. Therefore the postcondition is met. To show termination define a decreasing sequence of natural numbers and use the W.O.P. 20

Multiplication Take 2 PRECONDITION: m 2 N,n2 Z. POSTCONDITION: Returns the value m n. MULTIPLY( m, n ) 1. int x = m; 2. int y = n; 3. int z = 0; 4. while (x 6= 0) 5. if (x mod 2 = 1) 6. z = z+y; 7. x = x div 2; 8. y = y 2; 9. return z; Q: Why does MULTIPLY(n,m) work? Consider: x y = y + y + y + + y (x times) If x is even then x y = 2(y + y + y + + y) (x/2 times) If x is odd then x y =...y + 2(y + y + + y + y) ((x 1)/2 times) = z +(x 1)/2 2y Once x =0, xy = z +0 y Q: Why might we want to use such an algorithm to multiply? because multiply or divide by 2 is cheap as it is just a shift in binary. 21

For correctness, we need to prove three things: 1. Partial Correctness 2. Termination 3. Partial Correctness + Termination! Total correctness. Partial Correctness Q: Which variables would we expect to partake in the loop invariant? m, n, x, y, z as they are all involved in the precondition or postcondition. Loop Invariant: P (i): If the i th iteration exists then mn = z i + x i y i Q: Why do we believe this loop invariant? 2 reasons: 1 when the loop exists, x =0so z = mn. 2. it makes sense from what we were trying before. Claim: P (i) is true for all i 2 N. Proof. 1. Base Case: i = 0, before the loop begins, z = 0 and x = m, y = n so x 0 y 0 + z 0 = mn 2. Induction Hypothesis: Assume that P (i) is true for arbitrary i 2 N. 22

3 Induction Step: RTP: P (i + 1) is true. Assume that the (i + 1) st iteration exists: Since P (i) is true: mn = z i + x i y i If x i mod 2 6= 1then: z i+1 = z i = mn x i y i x i+1 = x i /2 y i+1 = 2y i Therefore, z i+1 = mn 2x i+1 y i+1 /2=mn x i+1 y i+1 If x i mod 2 = 1 then: z i+1 = z i + y i = mn x i y i + y i x i+1 = (x i 1)/2 y i+1 = 2y i Therefore, z i+1 = mn (2x i+1 + 1)y i+1 /2+y i+1 /2 = mn x i+1 y i+1 y i+1 /2+y i+1 /2 = mn x i+1 y i+1 Therefore, P (i) holds for all i 2 N. 23

Termination Q: How can we show that the loop terminates? define a decreasing sequence so that we can use the WOP. Q: What is such a sequence? Notice that x i is decreasing, hence, let d i = x i. Claim: The loop will terminate. Proof. Since x i+1 = x i div2, d i+1 = x i+1 <x i = d i. Since d i is a decreasing sequence of natural numbers (since each x i 2 N), the WOP applies and the sequence must terminate. Notice that the sequence must terminate at x k =0for some k. 24