commit ae1ec205f9366d8dadfa29a4e8831baed2458e42
parent 4eaefeaf1c22d39724c52b8fd1014f820b263143
Author: William Lindholm <william_lindholm@outlook.com>
Date: Sun, 10 Dec 2023 23:39:16 +0100
Created 2d graphs for time complexity of problem 1.
Diffstat:
8 files changed, 70 insertions(+), 19 deletions(-)
diff --git a/pythonPlotting/2d_plot.png b/pythonPlotting/2d_plot.png
Binary files differ.
diff --git a/pythonPlotting/2d_plot_filtered.png b/pythonPlotting/2d_plot_filtered.png
Binary files differ.
diff --git a/pythonPlotting/2d_plot_with_regression.png b/pythonPlotting/2d_plot_with_regression.png
Binary files differ.
diff --git a/pythonPlotting/Figure_3_bucketSort2d.png b/pythonPlotting/Figure_3_bucketSort2d.png
Binary files differ.
diff --git a/pythonPlotting/Figure_3_insertionsSort2d.png b/pythonPlotting/Figure_3_insertionsSort2d.png
Binary files differ.
diff --git a/pythonPlotting/problem1_generatePlot.py b/pythonPlotting/problem1_generatePlot.py
@@ -1,7 +1,7 @@
import os
import matplotlib.pyplot as plt
-from mpl_toolkits.mplot3d import Axes3D
import numpy as np
+from scipy.optimize import curve_fit
# Function to read measurements from a file
def read_measurements(filename):
@@ -9,10 +9,14 @@ def read_measurements(filename):
with open(filename, 'r') as file:
for line in file:
parts = line.strip().split(',')
- if len(parts) == 3:
- measurements.append((int(parts[0]), int(parts[1]), float(parts[2])))
+ if len(parts) == 3 and int(parts[0]) == 1000: # Check if the first value is 1000
+ measurements.append((int(parts[1]), float(parts[2]))) # Ignore the first value
return measurements
+# Function for exponential model
+def exponential_model(x, a, b, c):
+ return a * np.exp(b * x) + c
+
# Get the current directory and construct the file path
current_dir = os.getcwd()
file_path = os.path.join(current_dir, 'problem1_data_insertionSort.txt')
@@ -21,32 +25,34 @@ file_path = os.path.join(current_dir, 'problem1_data_insertionSort.txt')
measurements = read_measurements(file_path)
# Unpacking the measurements
-max_values, array_sizes, times = zip(*measurements)
+array_sizes, times = zip(*measurements)
# Convert to numpy arrays for easier handling
-max_values = np.array(max_values)
array_sizes = np.array(array_sizes)
times = np.array(times)
-# Creating the 3D plot
-fig = plt.figure(figsize=(16, 12)) # Increase the figure size (width, height) in inches
-ax = fig.add_subplot(111, projection='3d')
+# Fit the exponential model to the data
+params, covariance = curve_fit(exponential_model, array_sizes, times)
-# Plotting
-scatter = ax.scatter(max_values, array_sizes, times, c=times, cmap='viridis', marker='o')
+# Create the 2D plot
+plt.figure(figsize=(16, 12))
-# Adding labels and title
-ax.set_xlabel('Max Integer Value')
-ax.set_ylabel('Array Length')
-ax.set_zlabel('Time (seconds)')
+# Plotting the original data
+plt.scatter(array_sizes, times, c='blue', marker='o', label='Original Data')
-# Adding a color bar
-color_bar = fig.colorbar(scatter, ax=ax, extend='both')
-color_bar.set_label('Sorting Time (seconds)')
+# Plotting the regression curve
+array_sizes_fit = np.linspace(min(array_sizes), max(array_sizes), 400)
+times_fit = exponential_model(array_sizes_fit, *params)
+plt.plot(array_sizes_fit, times_fit, color='red', label='Fitted Curve')
-plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
+# Adding labels, title, and legend
+plt.xlabel('Array Length')
+plt.ylabel('Time (seconds)')
+plt.title('Array Length vs Time with Exponential Regression')
+plt.legend()
-plt.savefig(os.path.join(current_dir, '3d_plot.png'), dpi=500)
+plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
+plt.savefig(os.path.join(current_dir, '2d_plot_with_regression.png'), dpi=500)
# Show the plot
plt.show()
\ No newline at end of file
diff --git a/pythonPlotting/problem1_generatePlot2d.py b/pythonPlotting/problem1_generatePlot2d.py
@@ -0,0 +1,44 @@
+import os
+import matplotlib.pyplot as plt
+import numpy as np
+
+# Function to read measurements from a file
+def read_measurements(filename):
+ measurements = []
+ with open(filename, 'r') as file:
+ for line in file:
+ parts = line.strip().split(',')
+ if len(parts) == 3 and int(parts[0]) == 9000: # Check if the first value is 1000
+ measurements.append((int(parts[1]), float(parts[2]))) # Ignore the first value
+ return measurements
+
+# Get the current directory and construct the file path
+current_dir = os.getcwd()
+file_path = os.path.join(current_dir, 'problem1_data_insertionSort.txt')
+
+# Read measurements from file
+measurements = read_measurements(file_path)
+
+# Unpacking the measurements
+array_sizes, times = zip(*measurements) # Only two values now
+
+# Convert to numpy arrays for easier handling
+array_sizes = np.array(array_sizes)
+times = np.array(times)
+
+# Creating the 2D plot
+plt.figure(figsize=(16, 12)) # Adjust figure size
+
+# Plotting
+plt.scatter(array_sizes, times, c='blue', marker='o') # Use a single color for simplicity
+
+# Adding labels and title
+plt.xlabel('Array Length')
+plt.ylabel('Time (seconds)')
+
+plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
+
+plt.savefig(os.path.join(current_dir, '2d_plot_filtered.png'), dpi=500)
+
+# Show the plot
+plt.show()
+\ No newline at end of file
diff --git a/pythonPlotting/problem1_generatePlot.py b/pythonPlotting/problem1_generatePlot3d.py