Proving The Correctness of Algorithms: Algorithm Design and Analysis
Proving The Correctness of Algorithms: Algorithm Design and Analysis
Algorithms
Algorithm Design and Analysis
http://staff.cs.upt.ro/~ioana/algo/
Lecture Outline
• Proving the Correctness of Algorithms
– Preconditions and Postconditions
– Loop Invariants
– Induction – Math Review
– Using Induction to Prove Algorithms
What does an algorithm ?
• An algorithm is described by:
– Input data
– Output data
– Preconditions: specifies restrictions on input data
– Postconditions: specifies what is the result
• Example: Binary Search
– Input data: a:array of integer; x:integer;
– Output data: found:boolean;
– Precondition: a is sorted in ascending order
– Postcondition: found is true if x is in a, and found
is false otherwise
Correct algorithms
• An algorithm is correct if:
– for any correct input data:
• it stops and
• it produces correct output.
• Proof: by induction on n
1. Base case: If n=1, s(1)=1=1*(1+1)/2
2. Inductive step: We assume that s(n)=n*(n+1)/2, and
prove that this implies s(n+1)=(n+1)*(n+2)/2 , for all
n>=1
s(n+1)=s(n)+(n+1)=n*(n+1)/2+(n+1)=(n+1)*(n+2)/2
Mathematical induction review – Example2
b
20 21 22 2k-1
Example: Loop invariant for
Decimal to Binary Conversion
Algorithm Decimal_to_Binary
Precondition:
Array A has at least 1 element between indexes p and r
(p<=r)
Postcondition:
The elements between indexes p and r are sorted
Example - Merge Sort
• MERGE-SORT calls a MERGE (A,p,q,r)
function MERGE(A,p,q,r) to
merge the sorted subarrays Precondition: A is an
of A into a single sorted one array and p, q, and r
• The proof of MERGE (which are indices into the
is an iterative function) can array such that p <= q
be done separately, using < r. The subarrays
A[p.. q] and A[q +1..
loop invariants r] are sorted
• We assume here that Postcondition: The
MERGE has been proved subarray A[p..r] is
to fulfill its postconditions sorted
(can do it as a distinct
exercise)
Correctness proof for Merge-Sort
• Number of elements to be sorted: n=r-p+1
• Base Case: n = 1
– A contains a single element (which is trivially “sorted”)
• Inductive Hypothesis:
– Assume that MergeSort correctly sorts n=1, 2, ..., k elements
• Inductive Step:
– Show that MergeSort correctly sorts n = k + 1 elements.
– First recursive call n1=q-p+1=(k+1)/2<=k => subarray A[p .. q] is
sorted
– Second recursive call n2=r-q=(k+1)/2<=k => subarray A[q+1 .. r] is
sorted
– A, p q, r fulfill now the precondition of Merge
– The postcondition of Merge guarantees that the array A[p .. r] is
sorted => postconsition of MergeSort
Correctness proof for Merge-Sort
• Termination:
– To argue termination, we have to find a quantity that decreases
with every recursive call: the length of the part of A considered
by a call to MergeSort
– For the base case, we have a one-element array. the algorithm
terminates in this case without making additional recursive calls.
Correctness proofs
for recursive algorithms
RECURSIVE(n) is
if (n=small_value)
return ct
else
RECURSIVE(n1) n1, n2, … nr are some
… values smaller than n but
RECURSIVE(nr) bigger than small_value
some_code