Proving algorithms (programs) correct with induction

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Recursive Definitions and Structural Induction
Recurrences. What is a Recurrence Relation? A system of equations giving the value of a function from numbers to numbers in terms of the value of the.
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
Elementary Number Theory and Methods of Proof. Basic Definitions An integer n is an even number if there exists an integer k such that n = 2k. An integer.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
Searching Arrays Linear search Binary search small arrays
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Induction and recursion
Discrete Mathematics Chapter 4 Induction and Recursion 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)
Induction and recursion
Chapter 4: Induction and Recursion
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
Chapter 5 With Question/Answer Animations. Section 5.1.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Discrete Structure Li Tak Sing( 李德成 ) Lecture 13 1.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
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.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
1 CMSC 341 Math Review. 2 Exponents Identities (X A ) B = X AB X A * X B = X A+B X A / X B = X A-B X A + X B  X A+B.
1 2/21/2016 MATH 224 – Discrete Mathematics Sequences and Sums A sequence of the form ar 0, ar 1, ar 2, ar 3, ar 4, …, ar n, is called a geometric sequence.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Induction and Recursion CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Proof by Induction CSC 172 SPRING 2002 LECTURE 7.
Lecture 3.4: Recursive Algorithms CS 250, Discrete Structures, Fall 2015 Nitesh Saxena Adopted from previous lectures by Zeph Grunschlag.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Searching Arrays Linear search Binary search small arrays
Recursive Definitions
Chapter 4: Induction and Recursion
Recursive Algorithms Section 5.4.
MATH 224 – Discrete Mathematics
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Analysis of Algorithms CS 477/677
Greatest Common Divisor
Greatest Common Divisor
CS 3343: Analysis of Algorithms
CS2210:0001Discrete Structures Induction and Recursion
Chapter 7 Single-Dimensional Arrays
Induction and Recursion
Hubert Chan (Chapters 1.6, 1.7, 4.1)
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Chapter 5 Induction and Recursion
Intro to Recursion.
Searching CSCE 121 J. Michael Moore.
CS 3343: Analysis of Algorithms
Applied Discrete Mathematics Week 9: Integer Properties
Module 8 – Searching & Sorting Algorithms
Algorithms Recurrences.
Induction and recursion
From the last time: gcd(a, b) can be characterized in two different ways: It is the least positive value of ax + by where x and y range over integers.
Presentation transcript:

Proving algorithms (programs) correct with induction

Fibonacci Sequence "How many pairs of rabbits will be produced in a year, beginning with a single pair, if in every month each pair bears a new pair which becomes productive from the second month on?" The result can be expressed numerically as: 1, 1, 2, 3, 5, 8, 13, 21, 34 ...”

Fibonacci Sequence 1, 1, 2, 3, 5, 8, 13, 21, 34 ...” F1 = 1 F2 = 1 F3 = F1 + F2 Fn = Fn-1 + Fn-2

How do we prove this program is correct? Fibonacci Sequence How do we prove this program is correct? (1) Fibonacci(n) (2) if n = 1 (3) return 1 (4) else if n = 2 (5) return 1 (6) else (7) return Fibonacci(n - 1) + Fibonacci(n - 2)

Fibonacci Proof by strong induction Base Case: Two Base Cases: n = 1 and n = 2. Fibonacci(1) = 1 Lines (2) and (3) Fibonacci(1) = 1 Lines (4) and (4)

Fibonacci Proof by strong induction Assumption: Fibonacci(k) is correct for all values of k ≤ n, where n,k∈ℕ

Fibonacci Proof by strong induction Induction Step: Assuming Fibonacci(k) is correct for all k ≤ n Definition: Fn = Fn-1 + Fn-2 Line (7) returns Fibonacci(n-1) + Fibonacci(n-2) when called with Fibonacci(n) when n > 2. Substituting n for k, we see this is the same function.

Check if a value is a power of 4 (1) bool isPowerOfFour(int num) { (2) if (num < 1) { (3) return false; (4) } (5) if (num == 1) { (6) return true; (7) } (8) return ((num % 4 == 0) && (isPowerOfFour(num / 4))); (9)}

isPowerOfFour proof Basis: If num < 1 then num4 < 1, and thus False. (2) and (3) If num = 1 then num4 < 1 = 14 = 1 thus true. (4) and (5) If num % 4 = 0 then true (8)

isPowerOfFour proof Induction Step (strong): Assume isPowerOfFour(n) works for all n ≤ k Consider case k+1 e.g. isPowerOfFour(k+1) if (k+1)%4 = 0 then return true, this is a power of 4. otherwise return solution of isPowerOfFour(k+1/4) Since (k+1/4) < k, this call will return a correct answer.

Binary Search Find an element key in a sorted array A of size n: A[1] ≤ A[2] ≤ … ≤ A[n-1] int binarySearch(int key, int A[n], left, right) returns the index of the key in range A[left] and A[right] (inclusive) Animation

Binary Search int binarySearch(int x, int[] a, int left, int right) { int m = (left+right)/2; if (x == a[m]) return m; if (x < a[m]) return binarySearch(x, a, left, m−1) else return binarySearch(x, a, m+1, right); }

BinarySearch proof Basis: If array to be sorted is length 0 or 1 (right-left ≤ 1) A single element array is sorted, as is a zero element array So works for n = 0 and n = 2

BinarySearch proof Induction Step (strong): Assume search works for all n = (right-left) where n ≤ k (inductive hypothesis) Consider a k+1, that is BinarySearch(x, a, left, right) where right – left = k+1 Case 1: x ==a[(left+right)/2]. This is a match, and we are done. Case 2: x < a[(left+right)/2]. Then BinarySearch(x, a, left,(right-left)/2-1) But ((right-left)/2)-1 – left = ((k+1) – 1 ) – left < k-1, so the number of elements in this search is < k-1. So this search will work (by the inductive hypothesis) Case 3: x > a[(left+right)/2]. Then BinarySearch(x, a,(right-left)/2+1, right) But right - ((right-left)/2)+1 = right - ((k+1) +1 ) < k-1, so the number of elements in this search is < k-1. So this search will work (by the inductive hypothesis)

Greatest Common Denominator  The greatest common divisor (gcd, for short) of  a and  b, written gcd(a,b), is the largest positive integer that divides both a and b. 

Greatest Common Denominator a) gcd(a,b)=gcd(b,a), b) if a>0 and a|b then gcd(a,b)=a, c) if a≡c(mod b), then gcd(a,b)=gcd(c,b).