Presentation is loading. Please wait.

Presentation is loading. Please wait.

ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.

Similar presentations


Presentation on theme: "ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION."— Presentation transcript:

1 ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION

2 MOTIVATION You need to prove your algorithms are correct: Power of the solution: Conjecture vs. Lemma! Building a proof may help find (and fix) mistakes

3 SIMPLE CORRECTNESS PROOF Two main conditions: The algorithm is complete/correct: the post-condition is respected on all possible inputs satisfying the pre-condition Precondition: a predicate I on the input data Postcondition: a predicate O on the output data Correctness: proving I ⇒ O The algorithm terminates For all possible input, the algorithm exits

4 LOOP INVARIANTS One possible scheme: prove an invariant is true for all iterations 1.Initialization: the invariant(s) is true prior to the first iteration of the loop 2.Maintenance: if the invariant is true for iteration n, it is true for iteration n+1 3.Termination: when the loop terminates, the invariant is true on the entire input

5 PROVING THE BUBBLESORT ALGORITHM Algorithm bubblesort Input: Integer a[] Output: Integer a[] sorted by increasing order for i = 1 to n for j = n to i+1 if a[j] < a[j-1] swap a[j,j-1] end if end for end for Sort Animation

6 BUBBLESORT Let A′ denote the output of BUBBLESORT(A). To prove that BUBBLESORT is correct, we need to prove that it terminates and that A′[1]≤A′[2]≤ ⋯ ≤A′[n] where n=A.length. In order to show that BUBBLESORT actually sorts, what else do we need to prove?

7 PROVING BUBBLESORT 1.State precisely a loop invariant for the inner for loop, and prove that this loop invariant holds. 2.Using the termination condition of the loop invariant proved above, state a loop invariant for the outer for loop 1-4 that will allow you to prove inequality A′[1]≤A′[2]≤ ⋯ ≤A′[n]

8 INNER LOOP INVARIANT At the start of each iteration of the inner for loop, (1) the subarray A[j...] consists of elements that were in A[j...]before entering the inner loop (possibly) in different order and (2) A[j] is the smallest of those elements. Initialization: It holds trivially, because A[j...] consists of only one element and it is the last element of A when execution starts the inner loop. Maintenance: On each step, we replace A[j] with A[j−1] if it is smaller. Because we're only adding the previous element and possibly swapping two values (1) holds. Since A[j−1] becomes the smallest of A[j] and A[j−1] and the loop invariant states that A[j] is the smallest one in A[j...], we know that (2) holds. Termination: After the loop terminates, we will get j=i. This implies that A[i] is the smallest element of the subarray A[i...] and array contains the original elements in some order.

9 OUTER LOOP INVARIANT At the start of each iteration, A[1..i−1] consists of sorted elements, all of which are smaller or equal to the ones in A[i...]. Initialization: Initially we have the empty array, which holds trivially. Maintenance: The invariant of the inner loop tells us that on each iteration, A[i] becomes the smallest element of A[i...] while the rest get shuffled. This implies that at the end of the loop: A[i]<A[k], for i<k Termination: The loop terminates with i=n, where n is the length of the array. Substituting the n for i in the invariant, we have that the subarray A[1..n] consists of the original elements, but in sorted order. This is the entire array, so the entire array is sorted.

10 WORST-CASE RUNNING TIME? The number of comparisons is

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

12 FIBONACCI SEQUENCE 1, 1, 2, 3, 5, 8, 13, 21, 34...” F 1 = 1 F 2 = 1 F 3 = F 1 + F 2 F n = F n-1 + F n-2

13 FIBONACCI SEQUENCE (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) How do we prove this program is correct?

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

15 FIBONACCI PROOF BY STRONG INDUCTION Assumption: Fibonacci(k) is correct for all values of k ≤ n, where n,k ∈ℕ

16 FIBONACCI PROOF BY STRONG INDUCTION Induction Step: Assuming Fibonacci(k) is correct for all k ≤ n Definition: F n = F n-1 + F n-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.

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

18 ISPOWEROFFOUR PROOF Basis: If num < 1 then num 4 < 1, and thus False. (2) and (3) If num = 1 then num 4 < 1 = 1 4 = 1 thus true. (4) and (5) If num % 4 = 0 then true (8)

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

20 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

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

22 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

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

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

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

26 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 "ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION."

Similar presentations


Ads by Google