Jump to content
Urch Forums

one more runtime


nonevent99

Recommended Posts

function f(n) {

if (n

return f(n-1) + f(n-2);

}

 

What is the run-time R(n) as a function of f(n)?

 

A) Theta(f)

B) Theta(lg f base 2)

C) Theta(log f base phi)

D) Theta(f^p) for some integer p > 1

E) None of the above

 

What is the run-time R(n) as a function of f(n) if O(1) memoization is added?

Link to comment
Share on other sites

Is the answer (A) ?

 

f(n) = f(n-1) + f(n-2)

 

R(n) is the number of nodes in a binary tree with n-1 levels. Considering the first call and its subsequent calls 2^(n-1)-1, the R(n) is O(2^(n-1)).

 

So, R(n) as a function of f(n) is Theta(f).

 

If O(1) memoization, R(n) will be Theta(n) ?

Link to comment
Share on other sites

 

 

I think you guys have the right answer. Good job.

 

The formal way to argue this might be to construct a recurrence function for the run time,

 

R(n) =

1 if n

R(n-1) + R(n-2) + 1 if n > 2

 

where R is the number of function calls.

 

If you solve this recurrence R(n) = R(n-1) + R(n-2) + 1, you have the same general solution as the solution to f(n) = f(n-1) + f(n-2), but with a different particular solution and slightly different boundary conditions.

 

Thus, R(n) must take the form A*phi^n + B*phihat^n + C, where A, B, and C are constant coefficients chosen to satisfy the boundary conditions. In terms of big-theta notation, this solution differs by, at most, a constant coefficient from the solution to f(n).

 

Consequently, R = Theta(f)

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...