GithubHelp home page GithubHelp logo

danieldotwav / fibonacci-sequence-dynamic-programming Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 3 KB

This Java program calculates Fibonacci numbers using two methods: a simple recursive approach and an optimized dynamic programming approach with memoization. It explores algorithm efficiency, recursive function calls, and the use of memoization to improve computational performance.

Java 100.00%
dynamic-programming fibonacci-numbers java memoization

fibonacci-sequence-dynamic-programming's Introduction

Fibonacci Sequence Using Dynamic Programming

Project Overview

This Java project demonstrates two different approaches to calculating numbers in the Fibonacci sequence: a simple recursive method and an optimized version using dynamic programming with memoization.

Algorithms Used

  1. Simple Recursive Method (fib):

    • This method implements a straightforward recursive algorithm for calculating Fibonacci numbers.
    • Time Complexity: O(2^n) - Exponential
    • Space Complexity: O(n) - Linear (due to the recursion call stack)
    • Drawback: It involves a lot of repeated calculations, making it inefficient for larger values of n.
  2. Dynamic Programming with Memoization (fib_improved):

    • This method improves upon the simple recursive method by storing already calculated Fibonacci numbers in a HashMap.
    • Time Complexity: O(n) - Linear
    • Space Complexity: O(n) - Linear (for storing the computed values in the hash map)
    • Advantage: Significantly faster for larger values of n as it avoids redundant calculations.

Code Snippet

import java.util.HashMap;

public class Main {
    static int fib(int n) {
        if (n <= 2) {
            return 1;
        }
        return fib(n - 1) + fib(n - 2);
    }

    static long fib_improved(int n, HashMap<Integer, Long> solutions) {
        if (n == 1 || n == 2) {
            return 1;
        }
        if (solutions.containsKey(n)) {
            return solutions.get(n);
        }
        long result = fib_improved(n - 1, solutions) + fib_improved(n - 2, solutions);
        solutions.put(n, result);
        return result;
    }
}

Potential Improvements and Tradeoffs

  • Iterative Approach: Implementing an iterative solution for the Fibonacci sequence can further reduce the space complexity to O(1), as it eliminates the need for a call stack or a hash map.
  • BigInteger for Large Numbers: For extremely large values of n, using long may still lead to overflow. In such cases, BigInteger can be used, though it will have a performance impact.
  • Parallel Computing: For further optimization, especially in a multi-core environment, parallel computing techniques can be explored. However, this adds complexity to the code and may not always lead to significant performance gains due to the overhead of thread management.

Conclusion

This project illustrates the power of dynamic programming in optimizing recursive algorithms. By using memoization, the Fibonacci sequence calculation becomes feasible for large values of n, demonstrating a significant improvement over the simple recursive approach.

fibonacci-sequence-dynamic-programming's People

Contributors

danieldotwav avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.