Loop Invariants

advertisement
Loop Invariants
A loop invariant is defined on pages 17 and 18. A loop invariant is a boolean statement
that must satisfy three conditions:
Initialization condition:
Maintenance condition:
Termination condition:
the invariant is true before entering the loop
the invariant is true every time we go through the loop
the invariant is true when we exit the loop.
How do we prove that? In this class, in English, using some common sense arguments.
Usually the proof builds upon itself. Check out the proof on page 18.
Let’s take an example of a very simple loop:
1.
x=1
2.
for ( k = 1, k  4, k++ ) {
3.
x++
4.
}
5.
Let’s say that the loop invariant for this loop is “x is always positive”. Yes, this is a fluffy
example, just to show you how to prove it.
Initialization condition has to hold on line 2, after we set k=1 and we didn’t go through
the loop yet. So – the invariant is true, x is 1.
Maintenance condition has to hold every time we go through the loop.
So we go through the loop once, and then set k=2. x=1, so LI is true.
Then we go through the loop once, and set k=3. x=2. LI is true.
And so on.
Termination condition: we got out of the loop and are now on line 5. x=5, so LI is true.
(fyi, k=5 on line 5).
The first obstacle is to define a loop invariant. Then it is relatively easy to prove it.
1.
//Sort in increasing order
BubbleSortA(A, n) {
for ( k = 1, k  n, k++ ) {
for ( j = n, j  k+1, j-- ) {
if A[j] < A[j-1]
swap A[j], A[j-1]
}
}
}
print i, j, A
}
a. Loop invariant for the inner loop:
A[j] is the smallest element of A<n, n-1, … j>.
Proof: Initialization: j= n: true, A[n] is the smallest of A[n].
Maintenance: j = n-1, j-2, …,2,1: true, because A[j] can get swapped with
A[j-1].
Termination: j = i. True, because A[i+1] and A[i] can get swapped, and
the maintenance condition holds.
b. Loop invariant for the outer loop:
A<1, 2, … i-1> consists of i-1 smallest elements of A, in sorted order.
Proof: Initialization: i=1: true, A[1,…0] is an empty array.
Maintenance: i=2, 3, …, n: true, because A’[i] is always the smallest
element of A[i, …, n]. (using the proof in part b).
Termination: i=n+1: true, the array is sorted.
c. When the array is sorted in decreasing order. Same running time as insertion sort.
2.
//search for value v in array A of n elements
LinearSearch(A, v) {
for (i=1, i ≤ n, i++) {
if A[i] =v
return I
}
return NIL
}
at most n+1
at most n
at most 1
at most 1
--------------T(n) = O(n)
Loop invariant:
v has not been found in A[1, 2, …, i-1].
3.
SelectionSort(A, n) {
for (j=1, j ≤ N, j++) {
smallest = j
for i = j+1, i ≤ N, i++) {
if A[i] ≤ A[smallest] {
smallest = I
}
}
swap A[j] and A[smallest]
}
}
N-1+1+1
N
Sum[j=1,N] (N-(j+1)+1+1)
Sum[j=1,N] (N-(j+1)+1)
Sum[j=1,N] ((N-(j+1)+1+1)*(0or1))
N
----------------T(n) =Θ(n2)
Loop invariant:
A[1, 2, …, j-1] contains the j-1 smallest elements of A, in ascending sorted order.
Proof:
Init.: j=1:
Maint.: j=2, 3, …, N:
Term.: j=N+1:
Download