Doubt in the complexity analysis of an algorithm

Asked

Viewed 139 times

2

I need to know how to compute the complexity of this algorithm using the recurrence equation.

public int qdr(int x, int n) {
   if(n == 0)
     return 1;
   else
     return x * qdr(x, n-1);
}

It is a recursive algorithm that reads two numbers x and n and returns the result of x n

  • Homework lol

  • @Jorgeb. is College’s exercise list, is that I have proof today and need to understand how to calculate the complexity of recursive algorithms using the kk recurrence equation

  • http://www.google.pt/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CEQFjA&url=http%3A%2F%2Fwww.lume.ufrgs.br%2Fbitstream%2Fhandle%2F10183%2F2133%2F000269187.pdf%3Fsequence%3D1&ei=8M0NVNe4NI7baPD3gKAO&usg=Afqspyxxickystldyb55eudvnjzw&sig2=9tootoonxJemYZqtZfmC-fmC-Nq&bvm=bv.74649129,d.d2s

1 answer

3


and very simple :)

when n > 1, the function makes an operation that has constant time, multiplication, and makes a recursive call that has size n - 1.

so the recurrence is:

T(n) = T(n-1) + 1 (ou O(1), da na mesma)
T(1) = 1

if you solve you will arrive at a sum of type 1 + 1 + 1+ ... + 1, with n a. that of n. therefore, the function is linear, O(n).

  • I understand, I will continue studying here :D thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.