2

For a school assignment, we have to create a memorized fibonacci function that reuses the recursive implementation of computing the fibonacci.

What is a good way to design our memorized function such that it takes advantage of an already existing function? This is my implementation so far:

Base class:

    public int computeFibonacci(int position) {
        assertPosition(position);

        if (position < 2) {
            return 1;
        }
        return computeFibonacci(position - 1) + computeFibonacci(position - 2);
    }

Inherited class:

    public int computeFibonacci(int position) {
        assertPosition(position);

        if (position < 2) {
            return 1;
        }

        if (this.memoizedList.containsKey(position)) {
            return this.memoizedList.get(position);
        }
        int result = super.computeFibonacci(position - 1) + super.computeFibonacci(position - 2);
        this.memoizedList.put(position, result);
        return result;
    }
2
  • you wanna read this post for optimal solution - geeksforgeeks.org/… Commented Nov 6, 2019 at 6:48
  • You don't need to call super implementation. If you call super implementation then you won't be able to make use of memoized values. Commented Nov 6, 2019 at 11:43

2 Answers 2

2

There is a confusion about reusing the recursive implementation in Base Class. If you call the recursive implementation, it will recursively calculate fib(n) = fib(n-1) + fib(n-2) = ..., which is conflict with memo function. Memo function tries to save time for calculated ones. For memo:

public int computeFibonacci(int position) {
        assertPosition(position);

        if (position < 2) {
            return 1;
        }

        if (this.memoizedList.containsKey(position)) {
            return this.memoizedList.get(position);
        }
        int result = computeFibonacci(position - 1) + computeFibonacci(position - 2);
        this.memoizedList.put(position, result);
        return result;
    }

For example, you try to print the first 1000 elements in fibonacci, memo function will save time on calculated ones.

Sign up to request clarification or add additional context in comments.

Comments

1

Your sub-class version doesn't take full advantage of the cached values. For example, if you call computeFibonacci(5), and memoizedList doesn't contain that key, you would call super.computeFibonacci(4) and super.computeFibonacci(3) even if they are already cached. Instead, you should call super.computeFibonacci(5).

public int computeFibonacci(int position) {
    assertPosition(position);

    if (position < 2) {
        return 1;
    }

    if (this.memoizedList.containsKey(position)) {
        return this.memoizedList.get(position);
    } else {
        int result = super.computeFibonacci(position);
        this.memoizedList.put(position, result);
        return result;
    }
}

3 Comments

Note that this function does not reimplement the original computeFibonacci. If reimplementing the original function is superfluous.
@JoopEggen I'm not sure what you mean by that. My suggestion for the sub-class's computeFibonacci obtains the value stored in the cache (if available) or calls the super class implementation otherwise.
Oh it was praise in a way, as the OP repeated the implementation of the original method. Your answer is halfways to a general solution for memoisation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.