Loop Invariants: Part 2 7 January 2019 OSU CSE 1 Maintaining the - - PowerPoint PPT Presentation

loop invariants part 2
SMART_READER_LITE
LIVE PREVIEW

Loop Invariants: Part 2 7 January 2019 OSU CSE 1 Maintaining the - - PowerPoint PPT Presentation

Loop Invariants: Part 2 7 January 2019 OSU CSE 1 Maintaining the Loop Invariant A claimed loop invariant is valid only if the loop body actually maintains the property, i.e., the loop invariant remains true at the end of each execution of


slide-1
SLIDE 1

Loop Invariants: Part 2

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

Maintaining the Loop Invariant

  • A claimed loop invariant is valid only if

the loop body actually maintains the property, i.e., the loop invariant remains true at the end of each execution of the loop body

  • To show this, you may assume:

– The loop invariant is valid at the start of the loop body – The loop condition is true

7 January 2019 OSU CSE 2

slide-3
SLIDE 3

The Loop Invariant Picture

test false true loop-body

while (test) { loop-body }

7 January 2019 OSU CSE 3

To show the loop invariant true at this red point... ...assume the invariant is true at this green point.

slide-4
SLIDE 4

Isn’t This Reasoning Circular?

  • To justify the assumption that the loop

invariant holds just after the loop condition test, didn’t we argue that assumption was valid because the loop invariant holds just before the test?

  • This is not circular reasoning but rather

mathematical induction

– See the confidence-building approach for reasoning about why recursion works

7 January 2019 OSU CSE 4

slide-5
SLIDE 5

The Loop Invariant Picture

test false true loop-body

while (test) { loop-body }

7 January 2019 OSU CSE 5

Show the loop invariant is true at this red point... ...which means it is true at this green point

  • n the first iteration...
slide-6
SLIDE 6

The Loop Invariant Picture

test false true loop-body

while (test) { loop-body }

7 January 2019 OSU CSE 6

Show the loop invariant is true at this red point at the end of the first iteration... ...which means it is true at this green point

  • n the second iteration...
slide-7
SLIDE 7

The Loop Invariant Picture

test false true loop-body

while (test) { loop-body }

7 January 2019 OSU CSE 7

Show the loop invariant is true at this red point at the end of the k-th iteration... ...which means it is true at this green point

  • n the (k+1)-st iteration...
slide-8
SLIDE 8

The Loop Invariant Picture

test false true loop-body

while (test) { loop-body }

7 January 2019 OSU CSE 8

Show the loop invariant is true at this red point at the end of the last iteration... ...which means it is true at this green point when the loop terminates.

slide-9
SLIDE 9

Example #2

double power(double x, int p)

  • Returns x to the power p.
  • Requires:

p > 0

  • Ensures:

power = x^(p)

7 January 2019 OSU CSE 9

slide-10
SLIDE 10

Example #2: Method Body

double result = 1.0; double factor = x; int pLeft = p; /** * @updates result, factor, pLeft * @maintains * pLeft >= 0 and * result * factor^(pLeft) = x^(p) * @decreases * pLeft */ while (pLeft > 0) { ... } return result;

7 January 2019 OSU CSE 10

slide-11
SLIDE 11

7 January 2019 OSU CSE 11

x = 3.0 p = 5 result = 1.0 factor = 3.0 pLeft = 5 /** * @maintains * pLeft >= 0 and * result * factor^(pLeft) = x^(p) */ while (pLeft > 0) { ... } x = 3.0 p = 5 result = factor = pLeft =

What are the values of the

  • ther variables

here?

slide-12
SLIDE 12

What Loop Body Would Work?

  • Observation: pLeft is positive at the start
  • f the loop body, and the loop body has to

decrease it

  • How could you decrease pLeft?

7 January 2019 OSU CSE 12

slide-13
SLIDE 13

Idea 1: Decrement pLeft

/** * @updates result, factor, pLeft * @maintains * pLeft >= 0 and * result * factor^(pLeft) = x^(p) * @decreases * pLeft */ while (pLeft > 0) { ... pLeft--; }

7 January 2019 OSU CSE 13

slide-14
SLIDE 14

The Rest of the Loop Body

  • This is true at the start of the loop body

(for each clause: why?):

pLeft >= 0 and result * factor^(pLeft) = x^(p) and pLeft > 0

  • This has to be true at the end of the loop

body (for each clause: why?):

pLeft - 1 >= 0 and result * factor^(pLeft - 1) = x^(p)

7 January 2019 OSU CSE 14

slide-15
SLIDE 15

The Rest of the Loop Body

  • This is true at the start of the loop body

(why?):

pLeft >= 0 and result * factor^(pLeft) = x^(p) and pLeft > 0

  • This has to be true at the end of the loop

body (why?):

pLeft - 1 >= 0 and result * factor^(pLeft - 1) = x^(p)

7 January 2019 OSU CSE 15

Since x and p do not change in the loop (why?), the two circled expressions must be equal at the end of the loop body.

slide-16
SLIDE 16

The Rest of the Loop Body

  • We need to update result from

resulti to resultf, and/or update factor from factori to factorf, to make this true:

resulti * factori^(pLeft) = resultf * factorf^(pLeft - 1)

  • How could you do that?

7 January 2019 OSU CSE 16

slide-17
SLIDE 17

The Rest of the Loop Body

  • We need to update result from

resulti to resultf, and/or update factor from factori to factorf, to make this true:

resulti * factori^(pLeft) = resultf * factorf^(pLeft - 1)

  • How could you do that?

7 January 2019 OSU CSE 17

One line of code that updates result: result *= factor;

slide-18
SLIDE 18

Idea 2: Halve pLeft

/** * @updates result, factor, pLeft * @maintains * pLeft >= 0 and * result * factor^(pLeft) = x^(p) * @decreases * pLeft */ while (pLeft > 0) { ... pLeft /= 2; }

7 January 2019 OSU CSE 18

slide-19
SLIDE 19

The Rest of the Loop Body

  • This is true at the start of the loop body

(for each clause: why?):

pLeft >= 0 and result * factor^(pLeft) = x^(p) and pLeft > 0

  • This has to be true at the end of the loop

body (for each clause: why?):

pLeft/2 >= 0 and result * factor^(pLeft/2) = x^(p)

7 January 2019 OSU CSE 19

slide-20
SLIDE 20

The Rest of the Loop Body

  • We need to update result from

resulti to resultf, and/or update factor from factori to factorf, to make this true:

resulti * factori^(pLeft) = resultf * factorf^(pLeft/2)

  • How can you do that?

– Remember: pLeft may be even or odd, but start with the simpler case where it is even

7 January 2019 OSU CSE 20