Loop Invariant - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Loop Invariant

Description:

Changes meaning every iteration. Slide 4. Lastra, 2001 ... Dutch Flag Problem. Dutch flag has been cut into many thin horizontal strips and all mixed up. ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 37
Provided by: anselmo9
Category:

less

Transcript and Presenter's Notes

Title: Loop Invariant


1
Loop Invariant
  • COMP 114
  • Thursday March 7

2
Announcements
  • Program 3

3
Loop Invariant
  • An assertion
  • In a loop
  • Changes meaning every iteration

4
Example
  • Assert.pre( true )
  • i 0
  • while (true)
  • B i 0
  • //Inv(Aj0 lt j lt iBj0)
  • if (i 9) break
  • i

5
Generalized Loop
assert(P) while(true) C assert(INV)
if(b) break D assert(Q)
6
Conditions
  • Invariant must be true first time
  • P C INV
  • True on exit
  • INV b gt Q
  • True within loop body
  • INV !b D C INV

7
Example 1
  • Some entry Bk of B0...n is equal to 0
  • Assign variable, sum, the sum of subarray
    B0..k-1.

Precondition for loop is assert (sum 0 i
0) // and (E j 0 lt j lt n Bj 0)
  •  Desired postcondition is
  • assert (Bi 0)
  • // and sum (Sum j 0 lt j lt i Bj)
  • // and (A j 0 lt j lt i Bj ! 0)

8
Loop
  • while (true)
  • // Invariant goes here
  • if (Bi 0) break
  • sum sum Bi
  • i

9
Loop
  • while (true)
  • // sum (Sum j 0 lt j lt i Bj)
  • // (A j 0 lt j lt i Bj ! 0)
  • if (Bi 0) break
  • sum sum Bi
  • i

10
Dutch Flag Problem
  • Dutch flag has been cut into many thin horizontal
    strips and all mixed up.
  • We need to put it back together.

11
Computer Representation
  • INPUT Array f0..n (n 1 strips) each of which
    has an R, W, or B.
  • Sort the array so all Rs are first, followed by
    Ws, then the Bs.

R
R
R

W
W
W
W

B
B
B
12
Program (condensed to fit page)
  • int x0 int y0 int zf.length-1
  • while (true)
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

13
Loop Invariant
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B

14
Conditions
  • Invariant must be true first time
  • P C INV
  • True within loop body
  • INV !b D C INV
  • True on exit
  • INV b gt Q

15
First Time Through
  • int x0 int y0 int zf.length-1
  • while (true)
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1...f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • . . .

W
R
W
B
B
R
W
R
B
W
x
z
y
16
W
R
W
B
B
R
W
R
B
W
x
z
y
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

17
W
R
W
B
B
R
W
R
B
W
x
y
z
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

18
R
W
W
B
B
R
W
R
B
W
x
y
z
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

19
R
W
W
B
B
R
W
R
B
W
x
y
z
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

20
R
W
W
W
B
R
W
R
B
B
x
y
z
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

21
R
W
W
W
B
R
W
R
B
B
x
y
z
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

22
R
W
W
W
B
R
W
R
B
B
x
y
z
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

23
R
W
W
W
R
R
W
B
B
B
x
y
z
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

24
R
R
W
W
W
R
W
B
B
B
x
y
z
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

25
R
R
R
W
W
W
W
B
B
B
x
z
y
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

26
R
R
R
W
W
W
W
B
B
B
y
x
z
  • // Inv f0...x-1 are R
  • // fx...y-1 are W
  • // fy...z are unknown color
  • // fz1... f.length - 1 are B
  • if (y z1) break 
  • switch(fy)
  • case 'W'
  • y break
  • case 'B'
  • swap(f,y,z) z-- break
  • default
  • swap(f,x,y) x y

27
Did That Help Understanding?
  • Formal explanation
  • Diagram helped too, of course

28
Special Case While Statement
while(true) C assert(INV) if(b)
break D
while(!b) D
No C
Where does the assertion go?
29
Possibilities
Repeat INV
  • assert(INV)
  • while(!b)
  • D
  • assert(INV)

30
do-while
while(true) C assert(INV) if(b)
break D
do C while(!b)
No D
31
do-while Implementations
Before test
do C assert(INV) while(!b)
32
for Loop
P for(i low i lt high i) C Q
33
Conditions
  • No looping (high lt low)
  • (P i low high lt low) gt Q
  • Before loop body
  • P i low low lt high gt INV
  • Executing loop
  • INV i lt high C i INV
  • At end
  • INV i gt high gt Q

34
for Loop Implementation
  • for (int ilow inv() i lt high i)
  • C

35
After Break
  • Sorting

36
Have a Good Break
Write a Comment
User Comments (0)
About PowerShow.com