0

I've been tasked with finding the answers for the questions below, but honestly I'm completely lost as where to start. I'm not looking for straight answers per say (although they would be appreciated), but rather how I can find/derive them. I understand recursion, I just don't understand how to find a recurrence equation.

a.) Give the recurrence for the expected running time of RANDOM.

b.) Give the exact recurrence equation for the expected number of recursive calls executed by a call to RANDOM(n).

c.) Give the exact recurrence equation for the expected number of times the rerun statements on line 14 is executed, in all called to RANDOM(n), recursive or not.

Pseudocode:

Function RANDOM(u)

  1. if u = 1 then

  2.   return(1)

  3. else

  4.    assign x=0 with probability 1/2, or

  5.    assign x=1 with probability 1/3, or

  6.    assign x=2 with probability 1/6

  7.    if x=0 then

  8.       return(RANDOM(u-1) + RANDOM(u-2))

  9.    end-if

  10.    if x=1 then

  11.       return(RANDOM(u) + 2*RANDOM(u-1))

  12.    end-if

  13.    if x=2 then

  14.       return(3*RANDOM(u) + RANDOM(u) + 3)

  15.    end-if

  16. end-if

end-RANDOM

Philip Bal
  • 23
  • 9
  • Any of your own attempts? It is quite hard to explain how to do such a specific question without *actually* doing it fully; thus it would seem like a free homework service regardless of whether you are "looking for straight answers" or not. – meowgoesthedog Sep 27 '18 at 16:13

1 Answers1

0

First of all it is important to note that, since the questions ask for run time / no. of calls, the coefficients in front of the recursive calls to RANDOM don't matter (because none of the answers depend on the actual return value).

Also, since the questions ask for expected quantities, you can mix the appropriate recursive calls probabilistically.


a)

Starting off quite easy. Probabilistic mixing of functions:

T(u) = [1/2] * [T(u-1) + T(u-2)] + 
       [1/3] * [T(u)   + T(u-1)] + 
       [1/6] * [T(u)   + T(u)  ]        // + constant amount of work

b)

Same as before, but remember to add one for each call:

N(u) = [1/2] * [N(u-1) + N(u-2) + 2] +
       [1/3] * [N(u)   + N(u-1) + 2] +
       [1/6] * [N(u)   + N(u)   + 2]    // no constants here

c)

This is trickier than the other two. It seems contradictory that the question asks for "[all calls to RANDOM(u)] whether recursive or not", but only the ones on line 14 which are recursive...

Anyways ignoring this minor detail, the key thing to note is that the call to RANDOM(u) on line 11 can also produce the required recursive calls, without contributing to the total count itself. Adapting the above:

R(u) = [1/3] * [R(u)           ] +     // don't add 1 here
       [1/6] * [R(u) + R(u) + 2]       // add 2 as before

Note that the question probably expects you to rearrange all of the T(u), N(u), R(u) terms to the LHS. I'll leave that to you since it is trivial.

meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40
  • Very clear thank you. Just a couple of things I'm not quite sure on. For finding the number of times line 14 is executed, why do you take into account line 11? The way it seems to me is you have the initial first call, and then the recursive ones, yielding something like 1 + (1/6)[R(u) + R(u) + 2] ? Also, I'm not quite sure what LHS stands for. Does this have to deal with expanding the terms until a pattern emerges? I believe that is done for time complexity, but I'm unsure of the same principle carries over to the recurrence relation itself? – Philip Bal Sep 27 '18 at 17:41
  • @PhilipBal because, as I have explained in the answer, the call on line 11 can also lead to recursive calls itself, since it's argument is also `u`. I put 1 + etc inside the brackets so that the base case is included. – meowgoesthedog Sep 27 '18 at 17:44