LAB_1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Assignment Number: 1

NAME: Md. Muaz Sayyed ROLLNO: 60


CLASS: TY-IT-A BATCH: 3
___________________________________________________________________
Problem Statement:
1. Square matrix multiplication
2. Calculate no. of arithmetic operations .
3. Make a table of n, no. of operations and execution time.
4. Write pseudo code for it.
5. Find asymptotic : Theta (θ), Big Oh (O) , Omega ( Ω )
Answer:
Pseudo C code:
function matrix_multiply(A, B):
n = length(A)
C = initialize_matrix_of_zeros(n)

for i from 0 to n-1:


for j from 0 to n-1:
for k from 0 to n-1:
C[i][j] += A[i][k] * B[k][j]
return C
end
end
end

Code:
import numpy as np
import time
import matplotlib.pyplot as plt

def matrix_multiply(A, B):


n = len(A)
C = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
for j in range(n):
for k in range(n):
C[i][j] += A[i][k] * B[k][j]

return C

def calculate_operations(n):
return 2 * n**3 - n**2

def generate_table_and_plot(start_n, end_n):


data = []

print("n\tNo. of Operations\tExecution Time (seconds)")


print("---------------------------------------------")

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

execution_time = (end_time - start_time)


operations = calculate_operations(n)

data.append((n, operations, execution_time))

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

# Plot for n vs operations


plt.subplot(1, 2, 1)
plt.plot(n_values, operation_values, label='Number of Operations')
plt.title('n vs Number of Operations')
plt.xlabel('n')
plt.ylabel('Number of Operations')
plt.legend()

# Plot for n vs execution time


plt.subplot(1, 2, 2)
plt.plot(n_values, time_values, label='Execution Time')
plt.title('n vs Execution Time')
plt.xlabel('n')
plt.ylabel('Execution Time (seconds)')
plt.legend()
plt.show()

# Set the range for n


start_n = 10
end_n = 100

# Generate table and plots


generate_table_and_plot(start_n, end_n)

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy