Fibonacci numbers with help of DP
Well...the most and simple program which every programmer may have solved at least once....
Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation....
with base condition
f(0)=0
f(1)=1
In general:
f(n)=f(n-1) +f(n-2)
Basic Approach:
A simple method that is a direct recursive implementation mathematical recurrence relation given above
Here is simple CPP program....
Here we can see that the values are calculated again and again....
like fib(3) is calculated twice....
Time Complexity: T(n) = T(n-1) + T(n-2) which is exponential.
So can we do better....?
YES.....we can....
Remember...we discuss something about DP.....
Lets see the Efficient approach..

Comments
Post a Comment