I haven't been able to grasp the concept of complexity fully and I was wondering how I would be able to calculate it for method f(n) in this code:
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random r = new Random();
r.setSeed(System.currentTimeMillis());
int n = r.nextInt(20) + 1;
f(n);
}
private static void f(int n){
if(n > 0){
g(n);
System.out.println();
f(n-1);
}
}
private static void g(int n){
if(n > 0){
System.out.print('X');
g(n-1);
}
}
}
I understand that this is a recursive method which is what is confusing me. I see that every time the function f() is called, g() is is called, and run n times, and then f() calls itself as n-1 again until n=0. I don't know where to start Any help would be great. thanks.
The time complexity of this algorithm is O(log(b)) while computing power(a,b). This is because at every level in recursion sub-tree, we are doing only one computation(and using that value sub-sequently) and there are log(b) levels overall.
Recursion can reduce time complexity. An example of this is calculating fibonacci numbers. If you calculate the fibonacci sequence up to a number n using recursion rather than iteration, the time to complete the task when compared to that of the iterative approach was much greater.
A common technique for determining the runtime of recursive functions is to write out recurrence relations that describe the runtime as a quantity defined in terms of itself. Let's start with g. If we let cn denote the runtime of g(n), then we have
We can look over a couple of values to c to see if we spot a pattern:
Generally speaking, it looks like cn = n + 1. You can formalize this by using a proof by induction if you'd like, but for now we're going to take it on faith. This means that the runtime of g is O(n).
Now, let's let dn be the runtime of calling f(n). Notice that
We can expand this out to see if we see a pattern.
Generally, it looks like dn = n + (n-1) + (n-2) + ... + 1. You can formalize this by induction if you'd like. This is a famous sum, and it works out to n(n+1) / 2. This quantity happens to be Θ(n2), so the overall runtime is Θ(n2).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With