Tabulation vs MemoizationThere are two ways of implementing the dynamic programming, or we can say that there are two ways of storing the solution of subproblem so that it can be reused.
Let's understand each approach one by one in brief. What is tabulation?Tabulation is a technique that is used to implement the DP algorithms. It is also known as a bottomup approach. It starts from solving the lowest level subproblem. The solution to the lowest level subproblem will help to solve next level subproblem, and so forth. We solve all the subproblems iteratively until we solve all the subproblems. This approach saves the time when a subproblem needs a solution of the subproblem that has been solved before. Let's understand the tabulation through an example. Consider an example of the Fibonacci series. Suppose we need to calculate the F(5) term. In order to calculate the F(5) term, some subproblems appear more than once. For example, F(3) subproblem occurs twice, and F(4) subproblem occurs four times. Despite of calculating the same subproblem, again and again, we can store the result of a subproblem once and can use the result whenever required. Let's look at the code without storing the result. In the above code, we are simply calculating the Fibonacci series without storing results. The values of F(0) and F(1) are 0 and 1, respectively. The value of the nth term is calculated using the Fibonacci(n1)+Fibonacci(n2). The above code computes the Fibonacci series by storing the results of all the subproblems. We use the condition F[n] == null to determine whether the result is present or not in the array. What is Memoization?Memoization is a technique that is used to implement the DP algorithms. Memoization is also known as a topdown approach. It starts from solving the highestlevel subproblems. Initially, it solves the highestlevel subproblem and then solve the next subproblem recursively and the next. Suppose there are two subproblems, i.e., subproblem A and subproblem B. When subproblem B is called recursively, then it can use the solution of subproblem A, which has already been used. Since A and all the subproblems are memoized, it avoids solving the entire recursion tree generated by B and saves computation time. Let's understand the memoization through an example of the Fibonacci sequence. The sequence of numbers in which number is the sum of two preceding numbers. The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, etc. The condition for calculating the Fibonacci sequence is given below: F(n) = 0 if n = 0 1 if n = 1 F(n1) + f(n2) if n>1 Program with recursive solution The time complexity to find the recursive solution is O(2^{n}). Program with memoization The above code is used for memoization. Memoization is a technique used for storing all the solved subproblems so that we do not need to recalculate the solutions of the already solved subproblems, and it also reduces the time complexity. Let's understand the differences between the tabulation and memoization. 1. Tabulation: Tabulation is a technique used for solving the subproblem recursively. The below code shows the working of the tabulation: Suppose we want to calculate the fibonacci sequence of f(5). The Fibonacci sequence of f(5) is 0, 1, 1, 2, 3, 5. There is a total 15 calls to be used to calculate f(15). Memoization: Memoization is a technique used for solving the subproblem nonrecursively. The above code takes a time complexity of O(2^{n}). This time complexity is nonlinear. To reduce this time complexity, we can use the technique of memoization. This technique reduces the number of calls, and it reduces the time complexity. 2. State transition The tabulation is also known as a bottomup approach. It starts from the base state, which is 0. Once the base state 0 is solved, it moves to state 1. State 1 requires the solution of the base state 0. Once state 1 is solved, we move to state 2. The state 2 requires the solution of state 1. This process will continue till we reach the n state. The 'n' state is a destination state, and it is also known as an up state. This is known as a state transition relationship. The memoization is also known as a topdown approach. It starts from the topmost state, i.e., n. The 'n' state requires the solution of the 'n1' state but the 'n1' state is not yet solved. The 'n1' state requires the solution of the 'n2' state in order to solve its state, but 'n2' state is not yet solved. It will further ask until it reaches the base state. It will recursive calls such as f(n), f(n1), f(n2), ...,0, 1. So, here we have started from the topmost state, and recursively we reached the base state. Let's understand this concept through an example. In the above figure, '0' is the source vertex while 5 is the destination vertex. To reach vertex 5, we can either reach from 2 or 3. The decision would be based on the distance between the vertices. Let's assume that the distance between the vertices (0,2) is 1 and (0,3) is 2. If we consider vertex 2, then the distance from the vertex 0 to 5 is (1+1) =2. If we consider vertex 3, then the distance from the vertex 0 to 5 is (2+1) = 3. The minimum distance is 2 so we will consider vertex 2 to reach vertex 5. Differences between the tabulation and memorizationThe following is the list of differences between tabulation and memoization:
