Meanings and objectives of Tabulation
Last Updated :
26 Dec, 2023
Tabulation is a fundamental concept in Dynamic programming (DP), which entails dividing a problem into smaller subproblems and an array to hold the outcomes of these subproblems.
What is Tabulation?
One of the two primary methodologies in dynamic programming is tabulation, and memoization serves as the other. Tabulation creates a table (often an array) and fills it up one row at a time. It begins with resolving the smallest subproblems first and builds up towards larger subproblems using those answers until the main problem is resolved.
- A bottom-up strategy in dynamic programming is tabulation.
- The outcomes of the subproblems are kept in a table, which is often an array.
- Comparing tabulation to memoization in terms of time complexity, tabulation is often more effective.
Steps to Implement Tabulation:
Tabulation follows these steps:
- Determine the factors that need to be optimized and clearly identify the issue you’re trying to solve.
- Make a table to hold the outcomes of the subproblems (often an array). The problem determines the table’s size.
- Create base cases or initial values for the Table. These numbers stand in for the answers to the smallest subproblems.
- Fill in the table from the smallest subproblems to the largest using iteration (usually loops). Based on previously calculated subproblems, determine the solutions to each subproblem.
- Once the table is fully filled, the final result is usually found in the last entry of the table.
Let’s take an example: Fibonacci Sequence
Follow the steps to solve the problem using Dynamic Programming(tabulation):
- Find the nth Fibonacci number.
- Create an array fib to store intermediate Fibonacci numbers.
- Initialize the Table: fib[0] = 0 fib[1] = 1
- Use a loop to calculate fib[i] = fib[i-1] + fib[i-2] for i from 2 to n.
- The nth Fibonacci number is fib[n].
Below is the implementation of the above idea:
C++
#include <iostream>
#include <vector>
int fibonacci_tabulation( int n) {
std::vector< int > fib(n + 1, 0);
fib[0] = 0;
fib[1] = 1;
for ( int i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}
int main() {
int n = 10;
int result = fibonacci_tabulation(n);
std::cout << result << std::endl;
return 0;
}
|
Java
public class Main {
public static int fibonacciTabulation( int n) {
int [] fib = new int [n + 1 ];
fib[ 0 ] = 0 ;
fib[ 1 ] = 1 ;
for ( int i = 2 ; i <= n; i++) {
fib[i] = fib[i - 1 ] + fib[i - 2 ];
}
return fib[n];
}
public static void main(String[] args) {
int n = 10 ;
int result = fibonacciTabulation(n);
System.out.println(result);
}
}
|
Python
def fibonacci_tabulation(n):
fib = [ 0 ] * (n + 1 )
fib[ 0 ] = 0
fib[ 1 ] = 1
for i in range ( 2 , n + 1 ):
fib[i] = fib[i - 1 ] + fib[i - 2 ]
return fib[n]
n = 10
result = fibonacci_tabulation(n)
print (result)
|
C#
using System;
class Program
{
static int FibonacciTabulation( int n)
{
int [] fib = new int [n + 1];
fib[0] = 0;
fib[1] = 1;
for ( int i = 2; i <= n; i++)
{
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}
static void Main()
{
int n = 10;
int result = FibonacciTabulation(n);
Console.WriteLine(result);
}
}
|
Javascript
function fibonacciTabulation(n) {
let fib = new Array(n + 1);
fib[0] = 0;
fib[1] = 1;
for (let i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}
function main() {
let n = 10;
let result = fibonacciTabulation(n);
console.log(result);
}
main();
|
Conclusion:
Tabulation is a powerful technique in dynamic programming for solving complex problems efficiently by storing subproblems results in array.
Share your thoughts in the comments
Please Login to comment...