Algorithm design problem set

lazy808
Lecture2-IterativeAlgorithms-BinarySearch.pdf

LE/EECS3101

Design and Analysis of Algorithms

Iterative Algorithms & Loop Invariants – Binary Search

Karim Jahed

Binary Search

1 fun b i n a r y S e a r c h (A [ 1 . . n ] , k e y )

2 p =1 , q=n

3 while p ≤ q 4 mid = ( p + q ) /2

5 i f k e y < A [ mid ]

6 q = mid − 1 7 e l s e i f k e y > A [ mid ]

8 p = mid + 1

9 e l s e

10 return mid

1

Binary Search

1 fun b i n a r y S e a r c h (A [ 1 . . n ] , k e y )

2 p =1 , q=n

3 precond p and q are set to the first and last indices in the list, respectively

4 while p ≤ q 5 i n v a r i a n t if key is in A[1..n], then key is in A[p..q]

6 mid = ( p + q ) / 2

7 i f k e y < A [ mid ]

8 q = mid − 1 9 e l s e i f k e y > A [ mid ]

10 p = mid + 1

11 e l s e

12 return mid

13 postcond If key is in A[1..n], returns its location

1

Proof of the loop invariant

LI: ‘if key is in A[1..n], then key is in A[p..q]’

• Initialization: Per the precondition, p = 1 and q = n so LI(1) holds.

• Maintenance: Assuming that the loop invariant holds in the ith

iteration, we can prove that the loop invariant holds in the ith + 1

iteration by case exploration:

• key == A[mid] - in this case the loop terminates and there is no ith + 1 iteration.

• key < A[mid] - since the list is sorted, key cannot possibly be in A[mid..n]. Therefore, if key is in A[1..n] then it must be in

A[1..mid-1].

• key > A[mid] - since the list is sorted, key cannot possibly be in A[1..mid]. Therefore, it key is in A[1..n] then it must be in

A[mid+1..n].

2

Proof of the loop invariant

LI: ‘if key is in A[1..n], then key is in A[p..q]’

• Initialization: Per the precondition, p = 1 and q = n so LI(1) holds.

• Maintenance: Assuming that the loop invariant holds in the ith

iteration, we can prove that the loop invariant holds in the ith + 1

iteration by case exploration:

• key == A[mid] - in this case the loop terminates and there is no ith + 1 iteration.

• key < A[mid] - since the list is sorted, key cannot possibly be in A[mid..n]. Therefore, if key is in A[1..n] then it must be in

A[1..mid-1].

• key > A[mid] - since the list is sorted, key cannot possibly be in A[1..mid]. Therefore, it key is in A[1..n] then it must be in

A[mid+1..n].

2

Proof of the loop invariant

LI: ‘if key is in A[1..n], then key is in A[p..q]’

• Termination: Because integer division rounds down mid gets smaller at each iteration. Eventually one of two cases are possible:

• key is found and returned. The loop terminates and the post condition is asserted.

• All the sub-slices of the array are inspected, p exceeds q and the loop terminates. In this case, the post condition is also asserted since

according to the loop invariant key was never in A[1..n].

3