Fibonacci Algorithm

The Fibonacci Bottom-Up Algorithm is a dynamic programming technique used to compute the Fibonacci numbers more efficiently compared to the traditional recursive method. This algorithm follows the bottom-up approach, meaning it starts from the base case(s) and builds up the solution using previously computed values. Instead of using recursion, it employs a simple loop to calculate the Fibonacci numbers, making it more time and memory efficient. The core idea behind this algorithm is to use an array or a list to store the Fibonacci numbers as they are calculated, so that they can be easily accessed when required. In this algorithm, the Fibonacci sequence is computed iteratively by initializing an array or list with the first two base values, F(0) = 0 and F(1) = 1. Then, a loop is run from the third Fibonacci number (i.e., F(2)) to the desired Fibonacci number (F(n)). In each iteration, the current Fibonacci number is calculated as the sum of the previous two Fibonacci numbers, and this value is then stored in the array or list. The previously computed Fibonacci numbers are used to calculate the new one, thus eliminating redundant calculations and significantly reducing the time complexity. The time complexity of the Fibonacci Bottom-Up Algorithm is O(n), which is a vast improvement over the O(2^n) complexity of the traditional recursive method, making it suitable for calculating larger Fibonacci numbers.
package DynamicProgramming;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author Varun Upadhyay (https://github.com/varunu28)
 */

public class Fibonacci {

    private static Map<Integer, Integer> map = new HashMap<>();


    public static void main(String[] args) {

        // Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        System.out.println(fibMemo(n));
        System.out.println(fibBotUp(n));
        System.out.println(fibOptimized(n));
        sc.close();
    }

    /**
     * This method finds the nth fibonacci number using memoization technique
     *
     * @param n The input n for which we have to determine the fibonacci number
     *          Outputs the nth fibonacci number
     **/
    public static int fibMemo(int n) {
        if (map.containsKey(n)) {
            return map.get(n);
        }

        int f;

        if (n <= 1) {
            f = n;
        } else {
            f = fibMemo(n - 1) + fibMemo(n - 2);
            map.put(n, f);
        }
        return f;
    }

    /**
     * This method finds the nth fibonacci number using bottom up
     *
     * @param n The input n for which we have to determine the fibonacci number
     *          Outputs the nth fibonacci number
     **/
    public static int fibBotUp(int n) {

        Map<Integer, Integer> fib = new HashMap<>();

        for (int i = 0; i <= n; i++) {
            int f;
            if (i <= 1) {
                f = i;
            } else {
                f = fib.get(i - 1) + fib.get(i - 2);
            }
            fib.put(i, f);
        }

        return fib.get(n);
    }


    /**
     * This method finds the nth fibonacci number using bottom up
     *
     * @param n The input n for which we have to determine the fibonacci number
     *          Outputs the nth fibonacci number
     *          <p>
     *          This is optimized version of Fibonacci Program. Without using Hashmap and recursion.
     *          It saves both memory and time.
     *          Space Complexity will be O(1)
     *          Time Complexity will be O(n)
     *          <p>
     *          Whereas , the above functions will take O(n) Space.
     * @author Shoaib Rayeen (https://github.com/shoaibrayeen)
     **/
    public static int fibOptimized(int n) {
        if (n == 0) {
            return 0;
        }
        int prev = 0, res = 1, next;
        for (int i = 2; i <= n; i++) {
            next = prev + res;
            prev = res;
            res = next;
        }
        return res;
    }
}

LANGUAGE:

DARK MODE: