dimanche 1 janvier 2017

complexity and number of steps in recursive fibonacci

I have a task to make an analysis about recursive fibonacci algorithm. The algorithm has O(2^n) complexity. I have read that n is depth and in another article n is input size in 2^n. So what is the truth? Then how to count number of steps(maybe we can also call it recursive calls) to get a fibonacci number. I have a code like this:

#include <bits/stdc++.h>

using namespace std;

long fibonacci(long);

long jl=0;
double rt;

int main(){
    long result, number;

    scanf("%ld", &number);
    clock_t mulai = clock();
    result = fibonacci(number);
    rt =  ((double) (clock() - mulai)) / CLOCKS_PER_SEC;
    printf("Fibonacci (%ld) = %ld\n", number, result);
    printf("Jumlah langkah = %ld\n", jl);
    printf("Running Time = %.10f\n", rt);
    return 0;
}

long fibonacci(long n){
    jl++;
    if(n==0 || n==1)
        return n;
    else
        return fibonacci(n-1) + fibonacci(n-2);
}

jl in this code is number of steps(recursive calls). These are my compute example:

F(1),jl=1

F(2),jl=3

F(3),jl=5

F(5),jl=15

So, is it true or false? If false what is the correct code? Thank you.

Aucun commentaire:

Enregistrer un commentaire