0% found this document useful (0 votes)
15 views3 pages

DAA Assignment

The document defines functions for generating random matrices, adding, subtracting and multiplying matrices using the standard and Strassen's algorithms. It measures and plots the execution times of multiplying matrices of increasing sizes using both algorithms, showing that Strassen's algorithm performs better for larger matrices.

Uploaded by

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

DAA Assignment

The document defines functions for generating random matrices, adding, subtracting and multiplying matrices using the standard and Strassen's algorithms. It measures and plots the execution times of multiplying matrices of increasing sizes using both algorithms, showing that Strassen's algorithm performs better for larger matrices.

Uploaded by

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

import random

import time
import matplotlib.pyplot as plt
def generate_random_matrix(rows, cols):
return [random.randint(1, 10) for_ in range(cols)] for _ in range(rows)def
add_matrices (A, B):
return [[AOU] +BO) for j in range(len(A(0) for i in range(len(A))]def
subtract_matrices (A, B):
return [[AO0)- BQ] for j in range(len(A[0]))for i in range(len(A)]def
split_matrix(matrix):
n= len(matrix)
m=nl/2
a11= (matrix[ij[:m] for i in range(m)] a12 =
(matrix(l[m:] for i in range(m)] a21 =
[matrix(j[:m] for i in range(m, n)]a22 =
[matrix(][m] for iin range(m, n)]return
a11, a12, a21, a22
def combine_matrices(a11, a12, a21, a22):n
= len(a11)
result =[O] *(2* n) for _in range(2* n)]for i
in range(n):
for jin range(n):
result[i]i] =a11[]]
result(i]| + n] = a12[jG)
result(ji + n]i] =a21(])
result[i + n][j + n] = a22(i]]
return result
def standard_matrix_multiply(A, B):if
len(A[0]) l= len(B):
raise ValueError("Matrix dimensions are not compatible for mutiplication")result
=([0for_ in range(len(B(O) for _in range(len(A))
for i in range(len(A)):
for j in range(len(B[0]):
for k in range(len(B):
result[]0] += A[)[k]* B[(KJü)
return result
def strassen_matrix_multiply(A, B):if
len(A) == 1 and len(A[O]) == 1:
return [[AJOJO] * B[OJ[O)
a11, a12, a21, a22 = split_matrix(A)
b11, b12, b21,b22 = split_matrix(B)
p1 =strassen_matrix_multiply(a11, subtract_matrices(b12, b22)) p2 =
strassen_matrix_multiply(add_matrices(a11, a12), b22)
p3 = strassen_matrix_multiply(add_matrices(a21, a22), b11)
p4 = strassen_matrix_multiply(a22, subtract_matrices (b21, b11))
p5 = strassen_matrix_multiply(add_matrices(a11, a22), add_matrices(b11, b22))p6 =
strassen_matrix_multiply(subtract_matrices(a12, a22), add_matrices (b21,
b22)
p7 = strassen_matrix_multiply(subtract_matrices(a11, a21), add_matrices(b11, b12))c11
= subtract_matrices(add_matrices(add_matrices(p5, p4), p6), p2)
c12 = add_matrices(p1, p2)
c21 = add_matrices(p3, p4)
c22 = subtract_matrices(subtract_matrices(add_matrices(p5, p1), p3). p7)
return combine_matrices(c11, c12, c21, c22)
# Measure execution times
matrix_sizes = (2, 4, 8, 16, 32]
standard_times =
strassen_times =0
for n in matrix _sizes:
A = generate_random_matrix(n, n) B
generate_random_matrix(n, n)
start_time = time.time()
standard_result = standard_matrix_multiply(A, B)
standard_elapsed_time = time.time() - start_time
standard_times.append(standard _elapsed_time)
start_time = time.time()
strassen_result = strassen_matrix_multiply(A, B)
strassen_elapsed_time = time.time)- start_time
strassen_times.append(strassen_elapsed _time)
# Create the graph
plt.plot(matrix_sizes, standard_times, label="Standard Multiplication")
plt.plot(matrix_sizes, strassen_times, label="Strassen's Multiplication")
plt.xlabel("Matrix Size (n)")
plt.ylabel("Execution Time (seconds)")
plt.legend()
plt.show()
Output:
Standard Multiplication
Strassen's Multiplication
0.30
(seconds)
Time
Execution
0.25

0.20

0.15

0.10

0.05

0.00

5 10 15 20 25 30
Matrix Size (n)

You might also like