0% found this document useful (0 votes)
2 views5 pages

Lab 5 Assignemt

The document presents an assignment comparing dynamic programming and divide and conquer techniques using the Fibonacci sequence. It includes code implementations for recursive, memoized, and bottom-up approaches, along with a performance analysis that measures execution time for each method. The results are saved in a CSV file and visualized using a plot to illustrate the differences in execution time across the methods.

Uploaded by

manasdp271
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Lab 5 Assignemt

The document presents an assignment comparing dynamic programming and divide and conquer techniques using the Fibonacci sequence. It includes code implementations for recursive, memoized, and bottom-up approaches, along with a performance analysis that measures execution time for each method. The results are saved in a CSV file and visualized using a plot to illustrate the differences in execution time across the methods.

Uploaded by

manasdp271
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment No.

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

Runtime Time Complexity Analysis

Code-
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define MAX_N 40 // Maximum value for Fibonacci calculations

// Function to get time in seconds (Windows version)


double get_time_ns() {
LARGE_INTEGER freq, start;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
return (double)start.QuadPart / freq.QuadPart;
}

// Recursive Fibonacci (Exponential time complexity: O(2^n))


long long recursive_fib(int n) {
if (n <= 1) return n;
return recursive_fib(n - 1) + recursive_fib(n - 2);
}

// Memoization (Top-Down DP) (Time Complexity: O(n))


long long memoized_fib(int n, long long *memo) {
if (n <= 1) return n;
if (memo[n] != -1) return memo[n]; // Return cached result
return memo[n] = memoized_fib(n - 1, memo) + memoized_fib(n - 2,
memo);
}

// Bottom-Up Fibonacci (O(n) time complexity)


long long bottom_up_fib(int n) {
if (n <= 1) return n;
long long a = 0, b = 1, c;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}

// Function to measure execution time for all Fibonacci methods


void measure_fibonacci() {
FILE *fp = fopen("fibonacci_times.csv", "w");
if (!fp) {
printf("Error opening file!\n");
return;
}

fprintf(fp, "n,Recursive,Memoization,Bottom-Up\n");

for (int n = 1; n <= MAX_N; n++) {


double start, end;
long long result;
long long memo[MAX_N + 1];
// Recursive Fibonacci
start = get_time_ns();
result = recursive_fib(n);
end = get_time_ns();
double recursive_time = end - start;

// 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);

// Display output for verification


printf("n=%d -> Recursive: %.6f s, Memoization: %.6f s, Bottom-
Up: %.6f s\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

# Read the CSV file


df = pd.read_csv("fibonacci_times.csv")
# Plot execution time for each method
plt.figure(figsize=(10, 6))
plt.plot(df["n"], df["Recursive"], label="Recursive (O(2^n))",
marker="o", linestyle="dotted", color="red")
plt.plot(df["n"], df["Memoization"], label="Memoization (O(n))",
marker="s", linestyle="dashdot", color="green")
plt.plot(df["n"], df["Bottom-Up"], label="Bottom-Up (O(n))",
marker="^", linestyle="solid", color="blue")

# 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)

# Show the plot


plt.show()

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

You might also like