LAB_1
LAB_1
LAB_1
Code:
import numpy as np
import time
import matplotlib.pyplot as plt
return C
def calculate_operations(n):
return 2 * n**3 - n**2
start_time = time.perf_counter()
for n in range(start_n, end_n):
A = np.random.rand(n, n)
B = np.random.rand(n, n)
result = matrix_multiply(A, B)
end_time = time.perf_counter()
print(f"{n}\t{operations}\t\t\t{execution_time:.6f}")
# Generate plots
n_values, operation_values, time_values = zip(*data)
plt.figure(figsize=(10, 5))
Output:
Graph of n vs no. of operations and n vs execution time
Table of n , no. of operations and execution time.
Asymptotic values:
f(n) = 2n3-n2
Big-θ :
c 1∗g ( n ) ≤ f ( n ) ≤ c 2∗g ( n )
3 2
c 1∗g ( n ) ≤ 2n −n ≤ c 2∗g (n)
3 3 2 3 3
1∗n ≤ 2 n −n ≤ 2 n +n
3 3 2 3
1∗n ≤ 2 n −n ≤ 3∗n
Therefore, f ( n )=θ(n3) where n ≥ 1, c1 = 1, c2 = 3
Big-O:
f ( n ) ≤ c∗g ( n )
3 2
2 n −n ≤ c∗g (n)
3 2 3 3
2 n −n ≤ 2 n +n
3 2 3
2 n −n ≤ 3 n
Therefore, f ( n )=Ο(n 3) where n ≥ 1, c=3
Big- Ω:
c∗g ( n ) ≤ f ( n )
3 2
c∗g ( n ) ≤ 2 n −n
2 3 2
1∗n ≤ 2 n −n
2 3 2
1∗n ≤ 2 n −n
Therefore, f ( n )=Ω(n2) where n ≥ 1, c = 1