SUNY Geneseo Department of Computer Science


Induction and Recursion

{Date}

CSci 141, Fall 2004
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

CS Learning Center open

Voter registration and information today, Sept. 28, 11:00 - 8:30, College Union

Questions?

Numbering tiles for bouncing in lab

[Tile Robot Starts on Is Number n]

Reading Summary

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

Search a SuperArray

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

[Recursive Search in Items 0 through k-1]

Draw a Check Mark

[Robot Drawing a Yellow Check Mark]

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

[Robot Paints Two Tiles]

Mini-assignment: prove final algorithm above correct (and/or fix)

Next

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)


Next Lecture