SUNY Geneseo Department of Computer Science


Induction and Algorithms, Part 1

{Date}

CSci 141, Spring 2004
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Hand out Homework 1

Hand out Lab 5

Questions?

Reading - Section 7.1.2

Induction proofs on algorithms

Simple example, takeJourney

Implicit vs explicit induction variable

Value-producing recursion

Strong induction vs weak induction

[Splitting an Array into "Small" and "Large" Sections]

[Recursive Random Selection from Items below Position p]

Applying Induction to Algorithms

Unbalanced parentheses

Mini-assignment: prove Tuesday's algorithm correct

    unbalancedParens( int n ) {
        if ( n > 0 ) {
            print "("
            unbalancedParens( n-1 )
            print "))"
        }
    }

Assume n >= 0

Induction on n

Base case: n = 0

Assume it's true for k-1, k-1 >= 0, so k >0

For n = k...

Proof Examples

Multiplication by repeated addition:

    int multiply( int x, int y ) {
        if ( y == 0 ) {
            return 0;
        }
        else {           // y > 0 by precondition
            return x + multiply( x, y-1 );
        }
    }

How about incorrect multiplication algorithm?

    int multiply( int x, int y ) {
        if ( y == 0 ) {
            return 0;
        }
        else {           // y > 0 by precondition
            return x + multiply( x, y+1 );
        }
    }

Next Lecture