Presentation is loading. Please wait.

Presentation is loading. Please wait.

Proving algorithms (programs) correct with induction

Similar presentations


Presentation on theme: "Proving algorithms (programs) correct with induction"— Presentation transcript:

1 Proving algorithms (programs) correct with induction

2 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, ”

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

4 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)

5 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)

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

7 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.

8 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)}

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)

10 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.

11 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

12 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); }

13 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

14 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)

15 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. 

16 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).


Download ppt "Proving algorithms (programs) correct with induction"

Similar presentations


Ads by Google