MS 101 Algorithms Instructor Neelima Gupta nguptacs du

  • Slides: 23
Download presentation
MS 101: Algorithms Instructor Neelima Gupta ngupta@cs. du. ac. in

MS 101: Algorithms Instructor Neelima Gupta ngupta@cs. du. ac. in

Table Of Contents Proving Correctness of Algorithms

Table Of Contents Proving Correctness of Algorithms

Proving the correctness of Algorithm • Sequential Search • 1. index = 1; 2.

Proving the correctness of Algorithm • Sequential Search • 1. index = 1; 2. While index ≤ n and L[index] ≠ x do index = index + 1; 3. if index > n then index = 0;

Defining the I/O of the algorithm • Input : Given an array L containing

Defining the I/O of the algorithm • Input : Given an array L containing n items (n ≥ 0) and given x, • Output: – the sequential search algorithm terminates – with index = first occurrence of x in L, if found – and, index = 0 otherwise.

Loop Invariant • Hypothesis: For 1 ≤ k ≤ n + 1, L(k): when

Loop Invariant • Hypothesis: For 1 ≤ k ≤ n + 1, L(k): when the control reaches the test in line 2 for kth time – index = k and, – for 1 ≤ i ≤ k-1, L[i] ≠ x. • Prove the above hypothesis by induction

Proving the Hypothesis by induction • Base Case : H[1] is true vacuously. •

Proving the Hypothesis by induction • Base Case : H[1] is true vacuously. • Let H(k) be true • We will prove that H(k+1) is also true.

control reaches the test in line 2 for (k+1)th time only if L(k) ≠

control reaches the test in line 2 for (k+1)th time only if L(k) ≠ x …………. …. . (i) Also since H(k) is true 1 ≤ i ≤ k-1, L[i] ≠ x …………. . (ii) from (i) and (ii) we get, 1 ≤ i ≤ k, L[i] ≠ x ·˙· it holds for index = k+1 Thus by induction our hypothesis is true.

Correctness contd. . • Suppose the test condition is executed exactly k times. i.

Correctness contd. . • Suppose the test condition is executed exactly k times. i. e. body of the loop is executed k-1 times. – Case 1: k = n+1, by loop invariant hypothesis, index = n+ 1 and for 1 ≤ i ≤ n, L[i] ≠ x. Since index = n+ 1, line 3 sets index to 0 and by secondition above x is not in the array. So correct. – Case 2: k ≤ n, => index =k and loop terminated because L[k] = x. Thus index is the position of the first occurrence of x in the array. Hence the algorithm is correct in either case.

Binary Search • Input : Given an array L containing n items (n ≥

Binary Search • Input : Given an array L containing n items (n ≥ 0) ordered such that L(1) <= L(2) <= L(3) <=… <=L(n) and given x, • Output: The binary search algorithm terminates – with index = an occurrence of x in L, if found – and, index = 0 otherwise. Binary search can only be applied if the array to be searched is already sorted.

Search for the no. 15 1 10 15 20 25 30 35 45 50

Search for the no. 15 1 10 15 20 25 30 35 45 50

Search for the no. 30 1 10 15 20 25 30 35 45 50

Search for the no. 30 1 10 15 20 25 30 35 45 50 30

1. index_first = 1; 2. index_last=n; 3. While index_first<=index_last do 4. index_mid= floor((index_first+index_last)/2); 5.

1. index_first = 1; 2. index_last=n; 3. While index_first<=index_last do 4. index_mid= floor((index_first+index_last)/2); 5. if L[index_mid]=x 6. Exit loop 7. if L[index_mid]>x 8. index_last=index_mid-1; 9. if L[index_mid]<x 10. index_first=index_mid+1; 11. 12. If index_first>index_last then 13. index_mid=0;

Proof of correctness The Loop Invariant Let r be the maximum number of times

Proof of correctness The Loop Invariant Let r be the maximum number of times the loop starting from line 3 will run. • Hypothesis: For 1 ≤ k ≤ r + 1, H(k): when the control reaches the test in line 3 for kth time – L[i] ≠ x for every i<first & for every i>last • Prove the above hypothesis by induction

Correctness of the algorithm assuming the hypothesis (the loop invariant) Suppose the test condition

Correctness of the algorithm assuming the hypothesis (the loop invariant) Suppose the test condition is executed t times. – Case 1: first<=last. We exit from the loop because L[mt]=x. Thus, mid is a position of occurrence of x as mid=mt. – Case 2: If first>last, we exit from the loop starting at line 3. Line 13 sets the value of mid to 0 since first>last. By loop invariant hypothesis, for i<first and i>last, L[i] ≠ x i. e. x is not found in the array. The algorithm correctly returns 0 in mid. Hence assuming that the statement H(k) is correct the algorithm works correctly.

Proof of Induction Hypothesis Let fi and li be the values of first and

Proof of Induction Hypothesis Let fi and li be the values of first and last when the test condition at line 3 is executed for the ith time with f 1 and l 1 being 1 and n respectively. • Assume that the statement is true for H(k) • We will prove that it is true for H(k+1) In the kth iteration either first was set to (fk + lk)/2 +1 i. e. fk+1 = (fk + lk)/2 +1 or last was set to (fk + lk)/2 -1 i. e lk+1 = (fk + lk)/2 -1

Proof of Induction Hypothesis contd. . • Case 1: fk+1 > lk+1( only if

Proof of Induction Hypothesis contd. . • Case 1: fk+1 > lk+1( only if fk = lk and L[(fk + lk)/2] ≠ x) By induction hypothesis L[i] ≠ x i< fk L[i] ≠ x i> lk (= fk) And, also L[fk ] = L[(fk + lk)/2] ≠ x So x is not present, hence trivially L[i] ≠ x i< fk+1 L[i] ≠ x i> lk+1 Thus H(k+1) is true.

Proof of Induction Hypothesis contd. . Case 2: fk+1 <= lk+1 a: ) when

Proof of Induction Hypothesis contd. . Case 2: fk+1 <= lk+1 a: ) when L[(fk + lk)/2] <x then fk+1 = (fk + lk)/2 +1 i< fk +1, L[i] ≤ L[(fk + lk)/2]<x (Why? )

Proof of Induction Hypothesis contd. . Case 2: fk+1 <= lk+1 a: ) when

Proof of Induction Hypothesis contd. . Case 2: fk+1 <= lk+1 a: ) when L[(fk + lk)/2] <x then fk+1 = (fk + lk)/2 +1 i< fk +1, L[i] ≤ L[(fk + lk)/2]<x (input is sorted) i. e. L[i] ≠ x i<= fk+1 – 1 or I < fk+1 Also, by induction hypothesis L[i] ≠ x i> lk(= lk+1)

Proof of Induction Hypothesis contd. . b: ) When L[(fk + lk)/2] >x then

Proof of Induction Hypothesis contd. . b: ) When L[(fk + lk)/2] >x then lk+1 =(fk + lk)/2 -1 i> lk+1 , L[i] ≥ L[(fk + lk)/2]>x (Why? ) i. e. L[i] ≠ x i> lk+1

Proof of Induction Hypothesis contd. . b: ) When L[(fk + lk)/2] >x then

Proof of Induction Hypothesis contd. . b: ) When L[(fk + lk)/2] >x then lk+1 =(fk + lk)/2 -1 i> lk+1 , L[i] ≥ L[(fk + lk)/2]>x (Why? ) i. e. L[i] ≠ x i> lk+1 Also, by induction hypothesis L[i] ≠ x i< fk+1(= fk) Hence H(k+1) is true. This proves that our hypothesis is correct

Assignment 5 • Show that Insertion sort works correctly by using Induction.

Assignment 5 • Show that Insertion sort works correctly by using Induction.

Up Next Introduction to some tools to designing algorithms through Sorting

Up Next Introduction to some tools to designing algorithms through Sorting

The End

The End