Do the following problems from the text:
- Include with this portion of your homework a copy of your two
Fibonacci programs, including your memoisation function, from the
implementation section. Analyze the time complexity (big-Oh notation)
of your memoisation function and also of the two Fibonacci programs. You
don't need to include output from running them, just the python code
itself. Using this information, explain why the two (or three) programs
take different amounts of time to run.
- What if you had to write a memoisation function that could not count
on a bounded input size. (In the implementation, recall, we told you
that you would never see inputs larger than 100.) What would change?
What would its running time be then?
This is to be done on the CS machines (tux.cs.drexel.edu , or one of
the lab machines in UCross 151)
For the problems below, let Fib(0)=1, and Fib(1)=1.
For you to do:
- (Makefile target: problem1) Write a simple, recursive Fibonacci
function without using memoisation or any clever tricks. The program will
read an integer from the command line, as an argument to the script.
In the makefile, use the variable
arg to store the
argument. Do not define it in the makefile, we'll define
it when we invoke each target. So, the entry in your makefile might look
prob1 : prob1.py
python prob1.py $(arg)
It can be invoked like this:
arg=9 make prob1
- (Makefile target: problem2) Write a memoisation function as
described in class using an array of size 100. (That is, you may assume
you will never be asked for a number greater than Fib(100).) Use it to
implement a better Fibonacci function.
Clear the memo between runs. We want to explore how fib(n) behaves,
using memoisation. To do this, we time fib(n) for various values of
As above, use
$(arg) as an argument to the script in your
- (Makefile target: problem3) Use the timing function from
homework 2 to print a table of Fibonacci numbers from 1 to 40 (n, time for
Fib-normal(n), time for Fib-memo(n)). Also make a time plot of just your
fib-memo program (n, time-for-fib-memo(n)) from 1 to 40.
†You can use the same Fib function, and create a
wrapper that does memoisation. But be careful, you'll want a new closure
for each experiment. Decorators would normally be the way to go, but,
you're losing some of the fine-grained control, and will make timing more
difficult. This isn't a design course. Keep it simple, roll your own.