+ All Categories
Home > Documents > Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be...

Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be...

Date post: 29-May-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
53
Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 1 - Loop Invariants and Binary Search Chapter 4.3.3 and 9.3.1
Transcript
Page 1: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 1 -

Loop Invariants and Binary Search

Chapter 4.3.3 and 9.3.1

Page 2: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 2 -

Outline

Ø  Iterative Algorithms, Assertions and Proofs of Correctness

Ø  Binary Search: A Case Study

Page 3: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 3 -

Outline

Ø  Iterative Algorithms, Assertions and Proofs of Correctness

Ø  Binary Search: A Case Study

Page 4: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 4 -

Assertions

Ø An assertion is a statement about the state of the data at a specified point in your algorithm.

Ø An assertion is not a task for the algorithm to perform.

Ø You may think of it as a comment that is added for the benefit of the reader.

Page 5: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 5 -

Loop Invariants

Ø Binary search can be implemented as an iterative algorithm (it could also be done recursively).

Ø  Loop Invariant: An assertion about the current state useful for designing, analyzing and proving the correctness of iterative algorithms.

Page 6: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 6 -

Other Examples of Assertions

Ø Preconditions: Any assumptions that must be true about the input instance.

Ø Postconditions: The statement of what must be true when the algorithm/program returns.

Ø Exit condition: The statement of what must be true to exit a loop.

Page 7: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 7 -

Iterative Algorithms

Take one step at a time

towards the final destination

loop (done)

take step

end loop

Page 8: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 8 -

From the Pre-Conditions on the input instance we must establish the loop invariant.

Establishing Loop Invariant

Page 9: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 9 -

Maintain Loop Invariant

Ø Suppose that q We start in a safe location (pre-condition)

q  If we are in a safe location, we always step to another safe location (loop invariant)

Ø Can we be assured that the computation will always be in a safe location?

Ø  By what principle?

Page 10: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 10 -

Maintain Loop Invariant •  By Induction the computation will always be in a safe location.

(0)

, ( )

, ( ) ( 1)

S

i S i

i S i S i

⇒ ⎫⎪⎪⇒∀ ⇒⎬⎪⎪⇒∀ ⇒ + ⎭

Page 11: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 11 -

Ending The Algorithm Ø  Define Exit Condition

Ø  Termination: With sufficient progress,

the exit condition will be met.

Ø  When we exit, we know q  exit condition is true

q  loop invariant is true

from these we must establish

the post conditions.

Exit

Exit

0 km Exit

Page 12: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 12 -

Definition of Correctness <PreCond> & <code> è<PostCond>

If the input meets the preconditions,

then the output must meet the postconditions.

If the input does not meet the preconditions, then nothing is required.

Page 13: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 13 -

Outline

Ø  Iterative Algorithms, Assertions and Proofs of Correctness

Ø  Binary Search: A Case Study

Page 14: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 14 -

Define Problem: Binary Search

Ø PreConditions q Key 25

q Sorted List

Ø PostConditions q Find key in list (if there).

3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

Page 15: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 15 -

Define Loop Invariant

Ø Maintain a sublist.

Ø  If the key is contained in the original list, then the key is contained in the sublist.

key 25

3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

Page 16: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 16 -

Define Step

Ø Cut sublist in half.

Ø Determine which half the key would be in.

Ø Keep that half.

key 25

3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

If key ≤ mid, then key is in left half.

If key > mid, then key is in right half.

mid

Page 17: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 17 -

Define Step

Ø  It is faster not to check if the middle element is the key.

Ø Simply continue.

key 43

3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

If key ≤ mid, then key is in left half.

If key > mid, then key is in right half.

Page 18: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 18 -

Make Progress

Ø  The size of the list becomes smaller.

3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

79 km

75 km

Page 19: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 19 -

Exit Condition

Ø  If the key is contained in the original list,

then the key is contained in the sublist.

Ø  Sublist contains one element.

Exit

3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

0 km

•  If element = key, return associated entry.

•  Otherwise return false.

key 25

Page 20: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 20 -

Running Time

The sublist is of size n, n/2, n/4, n/8,…,1 Each step O(1) time.

Total = O(log n)

key 25

3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

If key ≤ mid, then key is in left half.

If key > mid, then key is in right half.

Page 21: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 21 -

Running Time

Ø Binary search can interact poorly with the memory hierarchy (i.e. caching), because of its random-access nature.

Ø  It is common to abandon binary searching for linear searching as soon as the size of the remaining span falls below a small value such as 8 or 16 or even more in recent computers.

Page 22: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 22 -

END OF LECTURE FEB 13, 2014

Page 23: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 23 -

<precondition>: A[1..n] is sorted in non-decreasing order<postcondition>: If is in A[1..n], algorithm returns

1, its location

loop-invariant>: If is

BinarySea

in

rch(A[1..n],

whil

)

e p q

key

keyq p

e

n

k y

<>

= =

if [ ]

els

2

1

return( )

return("Key n

A[1..n], then

e

endendif [ ]

end

is in A[p..

ot in list")

q]p qmid

q mid

p mi

key A m

key

id

key A p

e

d

plse

+⎢ ⎥= ⎢ ⎥⎣ ⎦

=

= +

=

Page 24: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 24 -

Simple, right?

Ø Although the concept is simple, binary search is notoriously easy to get wrong.

Ø Why is this?

Page 25: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 25 -

Boundary Conditions

Ø  The basic idea behind binary search is easy to grasp.

Ø  It is then easy to write pseudocode that works for a ‘typical’ case.

Ø Unfortunately, it is equally easy to write pseudocode that fails on the boundary conditions.

Page 26: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 26 -

1

if [ ]

else

end

q mid

p

key A mid

mid

=

=

+

Boundary Conditions

1

if [ ]

else

end

q mid

p

key A mid

mid

=

=

<

+or

What condition will break the loop invariant?

Page 27: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 27 -

Boundary Conditions

key 36

3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

mid

sC eod lek cey t A[m rige hid] t lf: ha≥ →

Bug!!

Page 28: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 28 -

1

if [ ]

else

end

q mid

p

key A mid

mid

=

=

+

Boundary Conditions

1

if [ ]

else

end

q mid

p

key A mid

mid

=

=

<

+

if < [ ]

else

end

1q mid

p

key A mid

mid

= −

=

OK OK Not OK!!

Page 29: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 29 -

key 25

3 5 6 13 18 21 21 25 36 43 49 51 53 60 72 74 83 88 91 95

Boundary Conditions

mid 2+⎢ ⎥= ⎢ ⎥⎣ ⎦p q mid

2+⎡ ⎤= ⎢ ⎥⎢ ⎥p q

or

Shouldn’t matter, right? Select mid 2

p q+⎡ ⎤= ⎢ ⎥⎢ ⎥

Page 30: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 30 -

6 74

Boundary Conditions

key 25

95 91 88 83 72 60 53 51 49 43 36 25 21 21 18 13 5 3

If key ≤ mid, then key is in left half.

If key > mid, then key is in right half.

mid Select mid

2p q+⎡ ⎤= ⎢ ⎥⎢ ⎥

Page 31: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 31 -

25 18 74

Boundary Conditions

key 25

95 91 88 83 72 60 53 51 49 43 36 21 21 13 6 5 3

If key ≤ mid, then key is in left half.

If key > mid, then key is in right half.

mid Select mid

2p q+⎡ ⎤= ⎢ ⎥⎢ ⎥

Page 32: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 32 -

25 13 74

Boundary Conditions

key 25

95 91 88 83 72 60 53 51 49 43 36 21 21 18 6 5 3

If key ≤ mid, then key is in left half.

If key > mid, then key is in right half.

• Another bug!

No progress toward goal:

Loops Forever!

mid Select mid

2p q+⎡ ⎤= ⎢ ⎥⎢ ⎥

Page 33: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 33 -

if [

mid

]2

1else

end

key A mi

p q

q mid

p mid

d

+⎢ ⎥= ⎢ ⎥⎣ ⎦

= +

≤=

Boundary Conditions

if [

mid

]2

1else

end

key A mi

p q

q mid

p mid

d

+⎡ ⎤= ⎢ ⎥⎢ ⎥

= +

≤=

if < [

mid2

1]

else

end

key A mid

p q

q mid

p mid

+⎡ ⎤= ⎢ ⎥⎢ ⎥

= −

=

OK OK Not OK!!

Page 34: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 34 -

if [

mid

]2

1else

end

key A mi

p q

q mid

p mid

d

+⎢ ⎥= ⎢ ⎥⎣ ⎦

= +

≤=

Getting it Right

Ø How many possible algorithms?

Ø How many correct algorithms?

Ø Probability of guessing correctly?

midr 2

o ? p q+⎡ ⎤= ⎢ ⎥⎢ ⎥

if < [ or ?]key A mid

elseo

end

1r q mid

p mid

= −

=

Page 35: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 35 -

BinarySearch(A[1..n],key)<precondition>: A[1..n] is sorted in non-decreasing order<postcondition>: If key is in A[1..n], algorithm returns its locationp = 1, q = nwhile q ≥ p

< loop-invariant>: If key is in A[1..n], then key is in A[p..q]

mid =p + q

2

⎣⎢⎢

⎦⎥⎥

if key <A[mid]q = mid − 1

else if key > A[mid]p = mid + 1

elsereturn(mid)

endendreturn("Key not in list")

Alternative Algorithm: Less Efficient but More Clear

ΘStill (log ), but with slightly larger constant.n

Page 36: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 36 -

Ø A volunteer, please.

Card Trick

Page 37: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 37 -

Pick a Card

Done

Thanks to J. Edmonds for this example.

Page 38: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 38 -

Loop Invariant: The selected card is one

of these.

Page 39: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 39 -

Which

column?

left

Page 40: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 40 -

Loop Invariant: The selected card is one

of these.

Page 41: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 41 -

Selected column is placed in the middle

Page 42: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 42 -

I will rearrange the cards

Page 43: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 43 -

Relax Loop Invariant: I will remember the same

about each column.

Page 44: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 44 -

Which

column?

right

Page 45: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 45 -

Loop Invariant: The selected card is one

of these.

Page 46: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 46 -

Selected column is placed in the middle

Page 47: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 47 -

I will rearrange the cards

Page 48: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 48 -

Which

column?

left

Page 49: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 49 -

Loop Invariant: The selected card is one

of these.

Page 50: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 50 -

Selected column is placed in the middle

Page 51: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 51 -

Here is your card.

Wow!

Page 52: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 52 -

Ternary Search

Ø  Loop Invariant: selected card in central subset of cards

Ø How many iterations are required to guarantee success?

1Size of subset = / 3

wheretotal number of cards

iteration index

in

ni

−⎡ ⎤⎢ ⎥

==

Page 53: Loop Invariants and Binary Search - York University · Loop Invariants ! Binary search can be implemented as an iterative algorithm (it could also be done recursively). ! Loop Invariant:

Last Updated: 2014-02-25 9:59 AM CSE 2011 Prof. J. Elder - 53 -

Learning Outcomes

Ø  From this lecture, you should be able to: q Use the loop invariant method to think about iterative algorithms.

q Prove that the loop invariant is established.

q Prove that the loop invariant is maintained in the ‘typical’ case.

q Prove that the loop invariant is maintained at all boundary conditions.

q Prove that progress is made in the ‘typical’ case

q Prove that progress is guaranteed even near termination, so that the exit condition is always reached.

q Prove that the loop invariant, when combined with the exit condition, produces the post-condition.

q Trade off efficiency for clear, correct code.


Recommended