Lab 5 Assignemt
Lab 5 Assignemt
4
Name: MANAS PATI Roll no: 10
Class: TY-IT-C Batch: 2
Date of Performance: 10/03/2025
___________________________________________________________________________
Problem Statement- Compare dynamic programming and divide and conquer using
Fibonacci sequence.
Write Up
Code-
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
fprintf(fp, "n,Recursive,Memoization,Bottom-Up\n");
// Memoized Fibonacci
for (int i = 0; i <= n; i++) memo[i] = -1;
start = get_time_ns();
result = memoized_fib(n, memo);
end = get_time_ns();
double memoized_time = end - start;
// Bottom-Up Fibonacci
start = get_time_ns();
result = bottom_up_fib(n);
end = get_time_ns();
double bottom_up_time = end - start;
// Save to CSV
fprintf(fp, "%d,%.9f,%.9f,%.9f\n", n, recursive_time,
memoized_time, bottom_up_time);
fclose(fp);
printf("\nResults saved to fibonacci_times.csv\n");
}
int main() {
measure_fibonacci();
return 0;
}
import pandas as pd
import matplotlib.pyplot as plt
# Graph Labels
plt.xlabel("n (Fibonacci Number)")
plt.ylabel("Execution Time (seconds)")
plt.title("Fibonacci Execution Time Analysis")
plt.legend()
plt.yscale("log") # Log scale for better visibility
plt.grid(True)
Output-
n,Recursive,Memoization,Bottom-Up
1,0.000000,0.000000,0.000000
2,0.000000,0.000000,0.000000
3,0.000000,0.000000,0.000000
4,0.000000,0.000000,0.000000
5,0.000000,0.000000,0.000000
6,0.000000,0.000000,0.000000
7,0.000000,0.000000,0.000000
8,0.000000,0.000000,0.000000
9,0.000000,0.000000,0.000000
10,0.000000,0.000000,0.000000
11,0.000000,0.000000,0.000000
12,0.000000,0.000000,0.000000
13,0.000000,0.000000,0.000000
14,0.000000,0.000000,0.000000
15,0.000000,0.000000,0.000000
16,0.000000,0.000000,0.000000
17,0.000000,0.000000,0.000000
18,0.000000,0.000000,0.000000
19,0.000000,0.000000,0.000000
20,0.000000,0.000000,0.000000
21,0.000000,0.000000,0.000000
22,0.000000,0.000000,0.000000
23,0.000000,0.000000,0.000000
24,0.000000,0.000000,0.000000
25,0.001000,0.000000,0.000000
26,0.001000,0.000000,0.000000
27,0.001000,0.000000,0.000000
28,0.002000,0.000000,0.000000
29,0.003000,0.000000,0.000000
30,0.004000,0.000000,0.000000
31,0.008000,0.000000,0.000000
32,0.013000,0.000000,0.000000
33,0.020000,0.000000,0.000000
34,0.034000,0.000000,0.000000
35,0.054000,0.000000,0.000000
36,0.089000,0.000000,0.000000
37,0.144000,0.000000,0.000000
38,0.231000,0.000000,0.000000
39,0.376000,0.000000,0.000000
40,0.609000,0.000000,0.000000