Memoization example in java

In this tutorial, we will see about Memoization example in java.

Let me start with the question.

Would you like to do same task again and again when you know that it is going to give you same result? I think Answer will be No.

So Memoization ensures that method does not execute more than once for same inputs by storing the results in the data structure(Usually Hashtable or HashMap or Array).
Let’s understand with the help of Fibonacci example.
Here is sample fibonacci series.

0,1,1,2,3,5,8,13,21,34,55,89,144..

So it has recurrence relation of:

F(n)= F(n-1)+F(n-2)

So Let’s write recurrence function for it.

When you run above code with n=5, you will get below output.

Calculating fibonacci number for: 5
Calculating fibonacci number for: 4
Calculating fibonacci number for: 3
Calculating fibonacci number for: 2
Calculating fibonacci number for: 2
Calculating fibonacci number for: 3
Calculating fibonacci number for: 2
Fibonacci value for n=5: 5

As you can see, we are calculating fibonacci number for 2 and 3 more than once.

Let’s draw a recursive tree for fibonacci series with n=5.
Here two children of node will represent recursive call it makes.
Memoization
If you notice here, we are calculating f(3) twice and f(2) thrice here, we can avoid duplication with the helping of caching the results.
We will use one instance variable memoizeTable for caching the result.

  • Check if n is already present in memoizeTable, if yes, return the value
  • If n is not present in memoizeTable, then compute and put the result in memoizeTable.

When you run above program, you will get below output.

Putting result in cache for 2
Putting result in cache for 3
Getting value from computed result for 2
Putting result in cache for 4
Getting value from computed result for 3
Putting result in cache for 5
Fibonacci value for n=5: 5

As you can see, we are not computing fibonacci number for 2 and 3 more than once.
That’s all about Memoization in java.

Related Posts

  • 29 November

    Top 100+ Java coding interview questions

    I have been posting data structure and coding interview questions on various topics such as Array, Queue, Stack, Binary tree, LinkedList, String, Number, ArrayList, etc. So I am consolidating a list of java coding interview questions to create an index post. I will keep adding links to this post whenever I will add new java […]

  • 18 April

    Minimum Number of Jumps to reach last Index

    If you want to practice data structure and algorithm programs, you can go through 100+ data structure and algorithm programs. In this post, we will see how to find Minimum Number of Jumps to reach last Index. Problem Given an array A of positive integers possibly zeroes, every index indicating the maximum length of a […]

  • 28 March

    Sort an array of 0s, 1s and 2s

    If you want to practice data structure and algorithm programs, you can go through 100+ data structure and algorithm programs. In this post, we will see how to sort an array of 0s, 1s and 2s.We have already seen a post on sort 0s and 1s in an array. Problem Given an array containing zeroes, […]

  • 04 March

    Check if it is possible to reach end of given Array by Jumping

    If you want to practice data structure and algorithm programs, you can go through 100+ data structure and algorithm programs.   Problem Given an array with positive integers as elements indicating the maximum length of a jump which can be made from any position in the array. Check if it is possible to have […]

  • 17 February

    Check if Array Elements are Consecutive

    If you want to practice data structure and algorithm programs, you can go through 100+ data structure and algorithm programs. In this post, we will see how to check if array elements are consecutive. Problem Given an array, we need to check if array contains consecutive elements. For example: Input: array[] = {5, 3, 4, […]

  • 04 February

    LCA of a K-ary Tree in O(Sqrt(height))

    If you want to practice data structure and algorithm programs, you can go through 100+ data structure and algorithm programs. In this post, we will see about how to find Lowest Common Ancestor of a K-ary Tree in O(Sqrt(height)).We have already seen how to find LCA of n-ary tree in O(n) complexity. Problem Given a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.