SUNY Geneseo Department of Computer Science
{Date}
CSci 141, Fall 2004
Prof. Doug Baldwin
First hour exam Thursday (Oct. 7)
I'm out of town (most of) Wednesday through Sunday after Fall break (i.e., gone Oct. 13 through 17).
Rest of Section 7.2 (i.e., middle of page 230 through middle of page 238)
When number of steps not equal to parameter
Recurrences make it easier to analyze number of steps in complex recursions
As abstract summaries of algorithm, same recurrence may arise from several algorithms, only solve it once
Count steps to represent execution time
count( char c, String s )
if ( s.length() == 0 )
return 0
else if ( s.charAt(0) == c )
return 1 + count( c, s.substring(1) )
else
return count( c, s.substring(1) )
How many string operations does this do to count characters in an n-character string?
Set up recurrence
Find closed form
n = 0 | 1 |
n = 1 | 3 + f(0) = 3 + 1 |
n = 2 | 3 + 3 + 1 |
hasCapital( str )
if ( str.length() == 0 )
return false
else if ( str.charAt(0) is upper case )
return true
else
return hasCapital( str.substring(1) )
What is the worst-case number of string operations this does to check an n-character string?
Worst case is when no capital letters
Recurrence relation
Closed form:
solve( n, source, spare, dest )
if n > 0
solve( n-1, source, dest, spare )
move_disk( source, dest )
solve( n-1, spare, source, dest )
How many "move_disk" operations does this do to move an n-disk tower?
Recurrence relation:
Closed form (from the reading)
(Oct. 14: career services)
Oct. 19: Relating step counts to execution time