Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.

Similar presentations


Presentation on theme: "1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR."— Presentation transcript:

1 1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR

2 2 How to Prove that Your Algorithm Implementation is Correct? for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]=key for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]=key

3 3 Loop Invariants Loop invariant – an assertion that is true before and after each execution of the body loop [precondition] while (guard) { loop-body } [postcondition] We must show three things about loop invariants: –Initialization – statement is true before first iteration –Maintenance – if it is true before an iteration, then it remains true before the next iteration –Termination – when loop terminates the invariant gives a useful property to show the correctness of the algorithm

4 4 Loop Invariant and Mathematical Induction A loop invariant should serve two purposes: –to state what the loop is supposed to accomplish –to help in proving the algorithm correctness. To prove that P is a loop invariant we use a mathematical induction.

5 5 Mathematical Induction Induction is a specialized proof technique Induction is used to prove universal properties (induction goal). 1.Base Step 2.Induction Hypothesis 3.Induction Step Often steps 2 and 3 are combined into one step ~ Maintenance and Termination ~ Initialization

6 6 Mathematical Induction Used to prove a sequence of statements (S(1), S(2), … S(n)) indexed by positive integers Proof: –Initial step: prove that the statement is true for n = 1 –Induction step: assume that S(n=k) is true and prove that S(n=k+1) is true for all n ≥ 1 Find case k “within” case k+1

7 7 Example Prove:  n  N, Initial Step: We must verify that the correctness of the statement for n=1 Induction Hypothesis: let us assume that the statement is correct for n = k Induction Step: we must prove the correctness of the statement for n=k+1 (given the correctness of the statement for n=k) Proof:  correct  this is what we were looking for (induction goal)  assume this is correct

8 8 Example Prove that: 2n + 1 ≤ 2 n for all n ≥ 3 Basis step/Initial step: –n = 3: 2  3 + 1 ≤ 2 3  7 ≤ 8 TRUE Induction step: –Induction hypothesis: Assume inequality is true for n=k, and prove it for n=k+1 Assume: 2k + 1 ≤ 2 k Must prove: 2(k + 1) + 1 ≤ 2 k+1 2(k + 1) + 1 = (2k + 1 ) + 2 ≤ 2 k + 2 ≤  2 k + 2 k = 2 k+1, since 2 ≤ 2 n for n ≥ 1 Induction step: it is important to single out the induction hypothesis

9 9 Let P(n) be the statement 8 n − 2 n is divisible by 6. We prove by mathematical induction that the statement P(n) is true, for every non-negative integer n. 8 0 − 2 0 8 k − 2 k 8 k+1 − 2 k+1 = 8*8 k − 2 k+1 = 8*(8 k − 2 k ) + 8*2 k − 2 k+1 = 8*(8 k − 2 k ) + 8*2 k − 2*2 k = 8*(8 k − 2 k ) + 2 k (8 − 2) = 8*(8 k − 2 k ) + 6*2 k Divisible by 6 (induction hypothesis)

10 10 Example: Insertion Sort Problem: sort n numbers in A[1..n]. Input: n, numbers in A Output: A in sorted order:  i  [2..n], A[i-1] <= A[i] for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]=key for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]=key

11 11 Invariant: at the start of each for loop, A[1…j-1] consists of elements originally in A[1…j- 1] but in sorted order for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]=key for j=2 to length(A) do key=A[j] i=j-1 while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]=key Example: Insertion Sort Initialization: j = 2, the invariant trivially holds because A[1] is a sorted array. Maintenance: the inner while loop finds the position i with A[i] <= key, and shifts A[j-1], A[j-2], …, A[i+1] right by one position. Then key, formerly known as A[j], is placed in position i+1 so that A[i] ≤ A[i+1] < A[i+2]. A[1…j-1] sorted + A[j]  A[1…j] sorted Termination: the loop terminates, when j=n+1. Then the invariant states: “A[1…n] consists of elements originally in A[1…n] but in sorted order.”


Download ppt "1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR."

Similar presentations


Ads by Google