SUNY Geneseo Department of Computer Science


Recurrence Relations and Algorithms, Part 2

{Date}

CSci 141, Spring 2005
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Questions?

Recurrence Analysis of Recursive Algorithms

How many times does this execute its "if n == 0" test?

    isOdd( n )
        if n == 0
            return false
        if n == 1
            return true
        else
            return isOdd( n-2 )

How many "X's" does this print?

    busyPrint( n )
        if n == 0
            print "X"
        else
            busyPrint( n - 1 )
            busyPrint( n - 1 )
            busyPrint( n - 1 )
n x(n)
0 1
1 3 x(0) = 3 * 1
2 3 x(1) = 3 * 3

How many times does the towers of Hanoi algorithm move a disk?

    solve( n, source, dest, spare )
        if n > 0
            solve( n-1, source, spare, dest )
            move disk from source to spare
            solve( n-1, spare, dest, source )

Next

Rest of this week: Prof. Zollo does an extended example of design and analysis of recursion

Next week: A notation for relating concrete operation counts to general execution time

Read Section 9.2


Next Lecture