Lec06_Correctness

Lec06_Correctness - Proving correctness Proving correctness...

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

View Full Document Right Arrow Icon
Proving correctness
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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?
Background image of page 4
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}”
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online