Jan 31
Homework: comments up for first (fibonnaci generally fine; plotting will take some practice); 2nd (several parts - plan accordingly) is posted.
run time practicalities
How many steps is practical for a program that runs in a few minutes? Demonstrate explicitly. Answer : about 1e8 .
What does this imply about the size N of a manageable problem for an algorithm that is
- O(N!)
- O(2N)
- O(N2) - how does this compare to N3?
- O(N log(N)) - and which base should we use?
- O(N)
- O(log(N))
a numerical experiment
As a class exercise
- implement the insertion sort algorithm.
- put in some code to count the number of steps
- ... discuss how many steps for the insertion
- using randomly generated data, generate (n vs steps(n))
- what is the expected O() behavior?
- what should therefore be done to the data to see a straight line plot ?
after class clarification
Attached is the code we wrote in
class. We didn't finish the numerical
experiment, so do *not* expect this
code to work ... it hasn't been debugged.
But after being finished, it would be something you could use to generate a data.csv :
$ python insertion.py > data.csv
You could also choose to average the
results from one size, so rather than output (say)
n, steps
4, 10
4, 12
you'd output one line with "4, 11",
where 11 is the average for n=4.
Either way, you then read into R,
and generate a plot that you hope
will look linear. Since this is
a O(n**2) algorithm, n vs sqrt(steps)
should be linear. Making that plot in R
would be
$ R
> data = read.csv("data.csv")
> plot(data$n, sqrt(data$steps))
Feel free to email me with questions
if any of this doesn't make sense.