SUNY Geneseo Department of Computer Science
{Date}
CSci 141, Fall 2004
Prof. Doug Baldwin
CS Learning Center open
Voter registration and information today, Sept. 28, 11:00 - 8:30, College Union
Numbering tiles for bouncing in lab
End of Section 7.1
Implicit induction - no explicit index, e.g., induction on number of steps from wall
Strong vs weak induction
Induction to prove correctness of recursive algorithms
Proofs often prose more than equations
Given value, x, and position, i, return True if any item between positions 0 and i of the SuperArray equals x, return False otherwise
// In class SuperArray...
boolean search( int x, int i )
if i < 0
return false
else if A[i] == x
return true
else
return this.search( x, i-1 )
Prove that this algorithm satisfies the specification above
Induction on the value of i
Base case, i = -1
Induction hypothesis, assume search returns true when x is in A between positions 0 and i = k-1 >= -1, false if x not in those positions
Show search returns true when x is in A between positions 0 and i = k
Preconditions
Postconditions
Design algorithm
// In some subclass of Robot
drawCheck( int n )
if n = 1
this.paint( Yellow )
turn around
else
this.paint( Yellow )
this.move()
this.drawCheck( n-1 )
this.move()
this.turnRight()
this.move()
this.paint( Yellow )
this.turnleft()
Prove it correct
Mini-assignment: prove final algorithm above correct (and/or fix)
Analyzing the execution time of recursive algorithms
Read Section 7.2 through "A First Example" in 7.2.2 (i.e., to middle of page 230)