Lec06_Correctness

# Lec06_Correctness - Proving correctness Proving correctness...

This preview shows pages 1–6. Sign up to view the full content.

Proving correctness

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Proving correctness Proof based on loop invariants an assertion which is satisfied before each iteration of a loop At termination the loop invariant provides important property that is used to show correctness Steps of proof: Initialization (similar to induction base) Maintenance (similar to induction proof) Termination
More on the steps Initialization : Show loop invariant is true before (or at start of) the first execution of a loop Maintenance : Show that if the loop invariant is true before an execution of a loop it is true before the next execution Termination : When the loop terminates, the invariant gives us an important property that helps show the algorithm is correct

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Example: Finding maximum Findmax(A, n) maximum = A[0]; for (i = 1; i < n; i++) if (A[i] > maximum) maximum= A[i] return maximum What is a loop invariant for this code?
Proof of correctness Loop invariant for Findmax(A): “At the start of the j th iteration (for j = 1, … , n ) of the for loop maximum = max {A[i]| i = 0, …, j - 1}”

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 16

Lec06_Correctness - Proving correctness Proving correctness...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online