Skip to content

Commit 8fb93a3

Browse files
committed
Added DP Algorithms - LCS, LIS, fibonacci seq
1 parent 771d6ef commit 8fb93a3

File tree

7 files changed

+608
-0
lines changed

7 files changed

+608
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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. <br>
12+
The Fibonacci sequence is defined as:
13+
$[ F(n) = F(n-1) + F(n-2) ]$
14+
with the base cases:
15+
$[ F(0) = 0, F(1) = 1 ]$
16+
17+
18+
## Problem Statement
19+
20+
Given an integer (n), compute the (n)-th Fibonacci number.
21+
22+
### Intuition
23+
24+
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.
25+
26+
## Naive Recursive Approach
27+
The naive approach to calculating the Fibonacci sequence uses a simple recursive function which calls recursivly and has exponential time complexity.
28+
29+
```python
30+
def fibonacci_recursive(n):
31+
if n <= 1:
32+
return n
33+
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)
34+
```
35+
36+
## Dynamic Programming Approach
37+
38+
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
39+
40+
### Bottom-Up Approach
41+
42+
We iteratively compute the Fibonacci numbers from the bottom up, storing intermediate results.
43+
44+
## Pseudocode for Fibonacci using DP
45+
46+
#### Initialize:
47+
48+
```markdown
49+
dp[0] = 0
50+
dp[1] = 1
51+
52+
Loop from 2 to n:
53+
dp[i] = dp[i-1] + dp[i-2]
54+
55+
return dp[n]
56+
```
57+
## Implementing Fibonacci using DP
58+
59+
### Python Implementation
60+
61+
```python
62+
def fibonacci_dp(n):
63+
# Base cases
64+
if n <= 1:
65+
return n
66+
67+
# Initialize an array to store Fibonacci numbers
68+
dp = [0] * (n + 1)
69+
70+
# Base values
71+
dp[0] = 0
72+
dp[1] = 1
73+
74+
# Calculate Fibonacci numbers from bottom-up
75+
for i in range(2, n + 1):
76+
dp[i] = dp[i-1] + dp[i-2]
77+
78+
return dp[n]
79+
```
80+
81+
### Java Implementation
82+
83+
```java
84+
public class Fibonacci {
85+
public static int fibonacci_dp(int n) {
86+
// Base cases
87+
if (n <= 1) return n;
88+
89+
// Initialize an array to store Fibonacci numbers
90+
int[] dp = new int[n + 1];
91+
92+
// Base values
93+
dp[0] = 0;
94+
dp[1] = 1;
95+
96+
// Calculate Fibonacci numbers from bottom-up
97+
for (int i = 2; i <= n; i++) {
98+
dp[i] = dp[i - 1] + dp[i - 2];
99+
}
100+
101+
return dp[n];
102+
}
103+
104+
public static void main(String[] args) {
105+
int n = 10;
106+
System.out.println("Fibonacci number at position " + n + " is: " + fibonacci_dp(n));
107+
}
108+
}
109+
110+
```
111+
### C++ Implementation
112+
113+
```cpp
114+
#include <iostream>
115+
using namespace std;
116+
117+
int fibonacci_dp(int n) {
118+
// Base cases
119+
if (n <= 1) return n;
120+
121+
// Initialize an array to store Fibonacci numbers
122+
int dp[n + 1];
123+
124+
// Base values
125+
dp[0] = 0;
126+
dp[1] = 1;
127+
128+
// Calculate Fibonacci numbers from bottom-up
129+
for (int i = 2; i <= n; i++) {
130+
dp[i] = dp[i - 1] + dp[i - 2];
131+
}
132+
133+
return dp[n];
134+
}
135+
136+
int main() {
137+
int n = 10;
138+
cout << "Fibonacci number at position " << n << " is: " << fibonacci_dp(n) << endl;
139+
return 0;
140+
}
141+
```
142+
143+
### JavaScript Implementation
144+
145+
```javascript
146+
function fibonacci_dp(n) {
147+
// Base cases
148+
if (n <= 1) return n;
149+
150+
// Initialize an array to store Fibonacci numbers
151+
let dp = new Array(n + 1).fill(0);
152+
153+
// Base values
154+
dp[0] = 0;
155+
dp[1] = 1;
156+
157+
// Calculate Fibonacci numbers from bottom-up
158+
for (let i = 2; i <= n; i++) {
159+
dp[i] = dp[i - 1] + dp[i - 2];
160+
}
161+
162+
return dp[n];
163+
}
164+
165+
let n = 10;
166+
console.log("Fibonacci number at position " + n + " is: " + fibonacci_dp(n));
167+
```
168+
169+
## Complexity Analysis
170+
171+
- Time Complexity: $O(n)$, since we are iterating from 2 to n.
172+
- 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.
173+
174+
## Conclusion
175+
176+
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)