Skip to content

Commit 3364b2f

Browse files
authored
Merge pull request #869 from kosuri-indu/add/dp-algorithm
Add Content: DP algorithms LCS, LIS and FIbonacci Sequence
2 parents fa54355 + dbaa961 commit 3364b2f

File tree

7 files changed

+606
-0
lines changed

7 files changed

+606
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
---
2+
id: fibonacci-sequence
3+
title: Fibonacci Sequence using Dynamic Programming
4+
sidebar_label: Fibonacci Sequence
5+
tags: [python, java, c++, javascript, programming, algorithms, dynamic programming, tutorial, in-depth]
6+
description: In this tutorial, we will learn about the Fibonacci sequence and its implementation using Dynamic Programming in Python, Java, C++, and JavaScript with detailed explanations and examples.
7+
---
8+
9+
# Fibonacci Sequence using Dynamic Programming
10+
11+
The Fibonacci sequence is a classic example that can be efficiently solved using dynamic programming. The Fibonacci sequence is defined as:
12+
$[ F(n) = F(n-1) + F(n-2) ]$
13+
with the base cases:
14+
$[ F(0) = 0, F(1) = 1 ]$
15+
16+
17+
## Problem Statement
18+
19+
Given an integer (n), compute the (n)-th Fibonacci number.
20+
21+
### Intuition
22+
23+
The Fibonacci sequence can be computed using a naive recursive approach, but this results in exponential time complexity due to repeated calculations of the same values. Dynamic Programming (DP) offers a more efficient solution by storing previously computed values.
24+
25+
## Naive Recursive Approach
26+
The naive approach to calculating the Fibonacci sequence uses a simple recursive function which calls recursivly and has exponential time complexity.
27+
28+
```python
29+
def fibonacci_recursive(n):
30+
if n <= 1:
31+
return n
32+
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)
33+
```
34+
35+
## Dynamic Programming Approach
36+
37+
Using DP, we store the results of subproblems to avoid redundant computations. This reduces the time complexity to $O(n)$. The dynamic programming (DP) approach solves the problem more efficiently by storing intermediate results to avoid redundant calculations
38+
39+
### Bottom-Up Approach
40+
41+
We iteratively compute the Fibonacci numbers from the bottom up, storing intermediate results.
42+
43+
## Pseudocode for Fibonacci using DP
44+
45+
#### Initialize:
46+
47+
```markdown
48+
dp[0] = 0
49+
dp[1] = 1
50+
51+
Loop from 2 to n:
52+
dp[i] = dp[i-1] + dp[i-2]
53+
54+
return dp[n]
55+
```
56+
## Implementing Fibonacci using DP
57+
58+
### Python Implementation
59+
60+
```python
61+
def fibonacci_dp(n):
62+
# Base cases
63+
if n <= 1:
64+
return n
65+
66+
# Initialize an array to store Fibonacci numbers
67+
dp = [0] * (n + 1)
68+
69+
# Base values
70+
dp[0] = 0
71+
dp[1] = 1
72+
73+
# Calculate Fibonacci numbers from bottom-up
74+
for i in range(2, n + 1):
75+
dp[i] = dp[i-1] + dp[i-2]
76+
77+
return dp[n]
78+
```
79+
80+
### Java Implementation
81+
82+
```java
83+
public class Fibonacci {
84+
public static int fibonacci_dp(int n) {
85+
// Base cases
86+
if (n <= 1) return n;
87+
88+
// Initialize an array to store Fibonacci numbers
89+
int[] dp = new int[n + 1];
90+
91+
// Base values
92+
dp[0] = 0;
93+
dp[1] = 1;
94+
95+
// Calculate Fibonacci numbers from bottom-up
96+
for (int i = 2; i <= n; i++) {
97+
dp[i] = dp[i - 1] + dp[i - 2];
98+
}
99+
100+
return dp[n];
101+
}
102+
103+
public static void main(String[] args) {
104+
int n = 10;
105+
System.out.println("Fibonacci number at position " + n + " is: " + fibonacci_dp(n));
106+
}
107+
}
108+
```
109+
### C++ Implementation
110+
111+
```cpp
112+
#include <iostream>
113+
using namespace std;
114+
115+
int fibonacci_dp(int n) {
116+
// Base cases
117+
if (n <= 1) return n;
118+
119+
// Initialize an array to store Fibonacci numbers
120+
int dp[n + 1];
121+
122+
// Base values
123+
dp[0] = 0;
124+
dp[1] = 1;
125+
126+
// Calculate Fibonacci numbers from bottom-up
127+
for (int i = 2; i <= n; i++) {
128+
dp[i] = dp[i - 1] + dp[i - 2];
129+
}
130+
131+
return dp[n];
132+
}
133+
134+
int main() {
135+
int n = 10;
136+
cout << "Fibonacci number at position " << n << " is: " << fibonacci_dp(n) << endl;
137+
return 0;
138+
}
139+
```
140+
141+
### JavaScript Implementation
142+
143+
```javascript
144+
function fibonacci_dp(n) {
145+
// Base cases
146+
if (n <= 1) return n;
147+
148+
// Initialize an array to store Fibonacci numbers
149+
let dp = new Array(n + 1).fill(0);
150+
151+
// Base values
152+
dp[0] = 0;
153+
dp[1] = 1;
154+
155+
// Calculate Fibonacci numbers from bottom-up
156+
for (let i = 2; i <= n; i++) {
157+
dp[i] = dp[i - 1] + dp[i - 2];
158+
}
159+
160+
return dp[n];
161+
}
162+
163+
let n = 10;
164+
console.log("Fibonacci number at position " + n + " is: " + fibonacci_dp(n));
165+
```
166+
167+
## Complexity Analysis
168+
169+
- Time Complexity: $O(n)$, since we are iterating from 2 to n.
170+
- Space Complexity: $O(n)$, due to the array used for storing intermediate results. This can be optimized to $O(1)$ by only storing the last two values.
171+
172+
## Conclusion
173+
174+
Dynamic programming provides an efficient solution for computing Fibonacci numbers by reducing redundant calculations and storing intermediate results. This technique can be extended to solve other problems with overlapping subproblems and optimal substructure properties.

0 commit comments

Comments
 (0)