Dynamic programming of the Fibonacci sequence

I was studying dynamic programming application to Fibonacci sequence and asked a question. Here's the code for your reference:

import java.math.BigInteger;
import java.util.Arrays;

public class FibonacciNumbersB {

    static BigInteger[] dp = new BigInteger[10000];

    public static void main(String[] args) {
        Arrays.fill(dp, BigInteger.ZERO);
        dp[0] = BigInteger.ONE;
        dp[1] = BigInteger.ONE;

        for(int i = 4; i < 9999; i++)
            System.out.println(fibRecurse(i).toString());
    }

    public static BigInteger fibRecurse(int N) {
        for(int i = 2; i < N; i++) {
            // For numerous calls to this function, this will save as it goes
            if(dp[i].equals(BigInteger.ZERO))
                dp[i] = dp[i - 1].add(dp[i - 2]);
        }

        return dp[N - 1];
    }
}

      

I have an if- dp[i]

equal statement 0

in a method fibRecurse

(although fibRecurse

not recursive).

How much more efficient is it to check whether it has already been calculated, dp[i]

or just leave it dp[i]

equal to the sum of the two previous elements?

+3


source to share


2 answers


I would rather Map<Integer, BigInteger>

use fixed BigInteger[]

when doing this memoization. Please note that your current approach is not recursive. Map

can be declared and initialized as

static Map<Integer, BigInteger> memo = new HashMap<>();
static {
    memo.put(0, BigInteger.ONE);
    memo.put(1, BigInteger.ONE);
}

      

Then check if the current one is present n

in memo

(if it is, put it back) - otherwise the computer and save it. How,

public static BigInteger fibRecurse(int n) {
    if (memo.containsKey(n)) {
        return memo.get(n);
    }
    BigInteger v = fibRecurse(n - 1).add(fibRecurse(n - 2));
    memo.put(n, v);
    return v;
}

      



A version without memoization would just skip memo

like

public static BigInteger fibRecurseSlow(int n) {
    if (n == 0 || n == 1) return BigInteger.ONE;
    BigInteger v = fibRecurse(n - 1).add(fibRecurse(n - 2));
    return v;
}

      

I think you can conclude that the method names I have chosen are slower.

+2


source


import java.math.BigInteger;
import java.util.Arrays;

public class FibonacciNumbersB {

    static BigInteger[] dp = new BigInteger[10000];

    public static void main(String[] args) {
        dp[0] = BigInteger.ONE;
        dp[1] = BigInteger.ONE;
        int N = 9999;
         fibRecurse(N);
        for(int i = 0; i < N; i++)
            System.out.println(dp[i].toString()) ;
    }

    public static void fibRecurse(int N) {
        for(int i = 2; i < N; i++) {

                dp[i] = dp[i - 1].add(dp[i - 2]);
        }
    }
}

      



+1


source







All Articles