Mathematical Induction

Similar documents
Recursively Defined Functions

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

Recall our recursive multiply algorithm:

Recursive Definitions Structural Induction Recursive Algorithms

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

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Announcements. CS243: Discrete Structures. Strong Induction and Recursively Defined Structures. Review. Example (review) Example (review), cont.

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

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

Integers and Mathematical Induction

Solutions to the Second Midterm Exam

Discrete Mathematics Lecture 4. Harper Langston New York University

CSE 20 DISCRETE MATH WINTER

Recursion. Chapter 5

COT 3100 Spring 2010 Midterm 2

Fall Recursion and induction. Stephen Brookes. Lecture 4

CS 310 Advanced Data Structures and Algorithms

Assertions & Verification & Example Loop Invariants Example Exam Questions

Assertions & Verification Example Exam Questions

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

CS158 Section B Exam 1 Key

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

More Complicated Recursion CMPSC 122

1 Elementary number theory

(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

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

CS 3410 Ch 7 Recursion

Recursion and Structural Induction

1 The sorting problem

Math Introduction to Advanced Mathematics

Chapter Summary. Recursively defined Functions. Recursively defined sets and Structures Structural Induction

Computer Science 236 Fall Nov. 11, 2010

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

Discrete Structures. Fall Homework3

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Arguing for program correctness and writing correct programs

ASYMPTOTIC COMPLEXITY

12/30/2013 S. NALINI,AP/CSE

Exponentiation. Evaluation of Polynomial. A Little Smarter. Horner s Rule. Chapter 5 Recursive Algorithms 2/19/2016 A 2 M = (A M ) 2 A M+N = A M A N

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

Reasoning about programs

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

Practice Final. Read all the problems first before start working on any of them, so you can manage your time wisely

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

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

1. Find f(1), f(2), f(3), and f(4) if f(n) is defined recursively by f(0) = 1 and for n = 0, 1, 2,

Reading 8 : Recursion

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

ASSIGNMENT 4 SOLUTIONS

Lemma (x, y, z) is a Pythagorean triple iff (y, x, z) is a Pythagorean triple.

15-122: Principles of Imperative Computation (Section G)

Inference rule for Induction

Propositional Logic. Andreas Klappenecker

Shell CSCE 314 TAMU. Functions continued

An Annotated Language

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Chapter 3 Programming with Recursion

6.046 Recitation 1: Proving Correctness Bill Thies, Fall 2004 Outline

Softwaretechnik. Program verification. Software Engineering Albert-Ludwigs-University Freiburg. June 30, 2011

DVA337 HT17 - LECTURE 4. Languages and regular expressions

P1 Engineering Computation

Mathematical Induction

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

Recursive definition of sets and structural induction

Lecture 6,

One of the most important areas where quantifier logic is used is formal specification of computer programs.

THE PRINCIPLE OF INDUCTION. MARK FLANAGAN School of Electrical, Electronic and Communications Engineering University College Dublin

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Building Java Programs

ASYMPTOTIC COMPLEXITY

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

Recursion vs Induction

4&5 Binary Operations and Relations. The Integers. (part I)

Binary Search to find item in sorted array

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

COMP250: Loop invariants

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

Lecture 3.4: Recursive Algorithms

Searching Algorithms/Time Analysis

AXIOMS FOR THE INTEGERS

CS Lecture 19: Loop invariants

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions)

Proof Techniques Alphabets, Strings, and Languages. Foundations of Computer Science Theory

1. Chapter 1, # 1: Prove that for all sets A, B, C, the formula

Lecture Notes on Induction and Recursion

Lecture Notes on Linear Search

Lecture Notes on Contracts

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

5.3 Recursive definitions and structural induction

15 212: Principles of Programming. Some Notes on Induction

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

Hoare Logic: Proving Programs Correct

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

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

Chapter 1 Programming: A General Overview

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Backward Reasoning: Rule for Assignment. Backward Reasoning: Rule for Sequence. Simple Example. Hoare Logic, continued Reasoning About Loops

Transcription:

Mathematical Induction Victor Adamchik Fall of 2005 Lecture 3 (out of three) Plan 1. Recursive Definitions 2. Recursively Defined Sets 3. Program Correctness Recursive Definitions Sometimes it is easier to define an object using a self-reference. For example, A linked list is either empty list or a node followed by a linked list. A binary tree is either empty tree or a node containing left and right binary trees. Sometimes self-referencing leads to paradoxes: The set of all sets which aren't elements of themselves. The process of defining an object using a self-reference is called recursion. A primitive recursion is a restricted way of defining fn1 in terms of fn. A recursive function is a function that calls itself in order to return an answer. A recursive definition of a function is given in two parts 1. a set of base values (or initial values) 2. a rule for calculating fn in terms of previous values

This is a typical example of recursive definition (or inductive definition) Here is another example, f0 5 fn1 fn 1 GCD0, b b GCDa, b GCDb, a, if ab GCDa, b GCDb mod a, a A palindrome is an expression that reads the same backwards and forwards. For example, Rats live on no evil star Madam I'm Adam Let us define a palindrome over a, b, c, d alphabet recursively. Initial values: General rule: P 0 P 1 a, b, c, d P n1 aa, bb, cc, d d P n1, n1 Recursively Defined Sets We start with an example, 2 S if as bs abs Claim. The above set is a set of positive even integers. Proof. Let E be a set of ALL positive even integers. We have to prove

1. E S 2. S E Prove 1). We need to prove that EVERY even positive integer belongs to S. The proof is by induction. Let Pn : 2nS. It's easy to see that the basis step holds: P12S. Assume that Pn is true. What can we say about Pn1? Pn12n12n2S since 2nS and 2 S. Prove 2). We need to prove S E, namely that any element in S is divisible by 2. We use the recursive definition of S: if a S bs abs Let us choose any element xs. By the above rule xaba 1 b 1 a 2 b 2... We continue splitting until we get x22...2 which means that x is divivible by 2. Hence, S E. Set of Strings Given an alphabet. We define a set of all strings over this alphabet: 1. empty string 2. x if and x

The second rule says that new strings are generated by concatenation. The length of a string L is defined by 1. Lempty 0 2. L x L 1, x Based on the above two definition we prove Proof (by induction on 2 ) L 1 2 L 1 L 2, 1, 2 Basis step: 2 empty. By the definition of the length of a string, Inductive step: we assume that L 1 2 L 1 L 1 L 2 L 1 0 L 1 2 L 1 L 2, 1, 2 for all 1L 2 n. We have to prove the above formula for L 2 n1. Note that by recursive definition, Therefore, L 1 2 L 1 xl 1 1 which concludes the proof. 2 x,, x by I H L 1 L 1 L 1 L x

Program Correctness How can we be sure that a particular algorithm implementation is correct? int prod = 1; for(int k=1; k<=n; k++) prod *= k; return prod; The idea is to use a loop invariant - an assertion that is true before and after each execution of the body of the loop. [precondition] while (guard) { loop-body } [postcondition] In the above example, a loop invariant is the following proposition P : prodk 1k n A loop invariant should serve two purposes: to state what the loop is supposed to accomplish and to help in proving the algorithm correctness. To prove that P is a loop invariant we use a mathematical induction. First we note that P is true before the loop is entered, since prod1. Next we assume that P is true for 1k n, namely after n1 loop executions. In the next execution, k is incremented by 1 (thus, it becomes n) and prod k. Since by inductive hypothesis the previous value of prod is k 1, we conclude that prodn. Therefore, P remains true. Finally, we need to show that the program terminates, which is trivial in our case.

Fast Exponentiation The following program computes a n, where n is nonnegative integer, n. int x = a, y = n, z = 1; while (y > 0) if (y%2 == 0) { x *= x; y = y/2;} else { return z; y -= 1; z *= x; } Note, often the hardest part of a loop invariant proof is identifying the invariant. We introduce the following proposition P : z x y a n y and prove (by induction) that it is a loop invariant. Basis step. P is true before the loop starts, because xa, yn and z 1: z x y 1 a n Inductive step. We assume that P is true after some iterations. We must show that P remains true after the next pass. Let variables with hats x, y, z be the values after the loop body was computed. Therefore, we have to prove is true.we consider two cases: z x y a n y 1) y is even. After the execution of the loop body, we have x x 2, y y 2, z z z x y z x 2 y 2 z x y 1) y is odd. After the execution of the loop body, we have by I H a n

x x, y y1, z z x z x y z x x y1 z x y by I H a n Note, in this case we decrement y by one. Is it true that y 0? Yes, it is, because the loop condition is y0. Finally, we must prove that the above program terminates. It follows from the fact that the loop invariant is true when the loop terminates and the loop condition is false zx y a n y y 0 This means that y0 and z x 0 z a n. So, we prove that the algorithm terminates and returns z a n. Fibonacci Numbers The following program computes n-th Fibonacci number, n. int prev = 1, cur = 1; if (n==0 n ==1) return n; if (n ==2) return 1; for(int k=3; k<= n; k++) { int tmp = cur; cur += prev; prev = tmp; } return cur; We introduce the following proposition P : cur F k prev F k1 k 2 Exercise. Prove (by induction) that it is a loop invariant.