SUNY Geneseo Department of Computer Science
{Date}
CSci 141, Fall 2004
Prof. Doug Baldwin
Reminder that Homework 3 has been assigned
Lab 11 due today
Labs next week & week after
Sections 15.2.1, 15.2.2
Algorithm to draw a tree as extension of LineDrawing class
Recursive
Pseudocode to concrete code
Correctness
Analysis of execution time
Multiple recursions led to exponential time
Problem: what's the greatest value of objects I can fit into a box?
int maxValue( box, objects )
// "box" is a box that might already contain
// some objects, "objects" a set of objects
if objects is empty
return the value of the objects in box
else
pick an object, o, from objects
if o fits in box
bestYet = maxValue( box+o, objects-o )
else
bestYet = 0
return max( bestYet, maxValue( box, objects - o )
What's the worst-case execution time of this algorithm, in terms of number of objects?
Count anything that reasonably represents the work the algorithm does, e.g., the tests ("objects is empty" and "o fits in box"), other actions or calculations ("pick an object...," "max"), etc.
Recurrence on n, number of objects
n | f(n) |
---|---|
0 | 1 |
1 | 2 + 2f(0) = 2 + 2*1 |
2 | 2 + 2f(1) = 2 + 2(2 + 2*1) |
= 2 + 22 + 22 | |
3 | 2 + 2f(2) = 2 + 2(2 + 22 + 22) |
= 2 + 22 + 23 + 23 |
Sum of powers of 2? from 21 through 2n (+ 2n)
Proof
Recall the Towers of Hanoi
Running time to solve for n disks is...?
See if we can see this empirically
Performance limits to computing
Read sections 16.1 and 16.2