Download presentation
Presentation is loading. Please wait.
1
MS 101: Algorithms Instructor Neelima Gupta ngupta@cs.du.ac.in
2
Table Of Contents Proving Correctness of Algorithms
3
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;
4
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.
5
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
6
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.
7
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.
8
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 second condition 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.
9
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.
10
Search for the no. 15 11015202530354550 1101520 1520
11
Search for the no. 30 11015202530354550 30354550 30
12
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;
13
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 k th time –L[i] ≠ x for every i<first & for every i>last Prove the above hypothesis by induction
14
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[m t ]=x. Thus, mid is a position of occurrence of x as mid=m t. –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 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.
15
Proof of Induction Hypothesis Let f i and l i be the values of first and last when the test condition at line 3 is executed for the i th 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 k th iteration either first was set to (f k + l k )/2 +1 i.e. f k+1 = (f k + l k )/2 +1 or last was set to (f k + l k )/2 -1 i.e l k+1 = (f k + l k )/2 -1
16
Proof of Induction Hypothesis contd.. Case 1: f k+1 > l k+1 ( only if f k = l k and L[(f k + l k )/2] ≠ x) By induction hypothesis L[i] ≠ x i< f k L[i] ≠ x i> l k (= f k ) And, also L[f k ] = L[(f k + l k )/2] ≠ x So x is not present, hence trivially L[i] ≠ x i< f k+1 L[i] ≠ x i> l k+1 Thus H(k+1) is true.
17
Proof of Induction Hypothesis contd.. Case 2: f k+1 <= l k+1 a:) when L[(f k + l k )/2] <x then f k+1 = (f k + l k )/2 +1 i< f k +1, L[i] ≤ L[(f k + l k )/2]<x (Why?)
18
Proof of Induction Hypothesis contd.. Case 2: f k+1 <= l k+1 a:) when L[(f k + l k )/2] <x then f k+1 = (f k + l k )/2 +1 i< f k +1, L[i] ≤ L[(f k + l k )/2]<x (input is sorted) i.e. L[i] ≠ x i<= f k+1 – 1 or I < f k+1 Also, by induction hypothesis L[i] ≠ x i> l k (= l k+1 ) Thus H(k+1) is true.
19
Proof of Induction Hypothesis contd.. b:) When L[(f k + l k )/2] >x then l k+1 =(f k + l k )/2 -1 i> l k+1, L[i] ≥ L[(f k + l k )/2]>x (Why?) i.e. L[i] ≠ x i> l k+1
20
Proof of Induction Hypothesis contd.. b:) When L[(f k + l k )/2] >x then l k+1 =(f k + l k )/2 -1 i> l k+1, L[i] ≥ L[(f k + l k )/2]>x (Why?) i.e. L[i] ≠ x i> l k+1 Also, by induction hypothesis L[i] ≠ x i< f k+1 (= f k ) Hence H(k+1) is true. This proves that our hypothesis is correct
21
Assignment 5 Show that Insertion sort works correctly by using Induction.
22
Up Next Introduction to some tools to designing algorithms through Sorting
23
The End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.