diff --git a/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Divide_and_Conquer/Strassen's_Matrix_Multiplication.py b/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Divide_and_Conquer/Strassen's_Matrix_Multiplication.py new file mode 100644 index 0000000000..c57a523d1c --- /dev/null +++ b/Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Divide_and_Conquer/Strassen's_Matrix_Multiplication.py @@ -0,0 +1,51 @@ +# Python code to perform 2x2 matrix multiplication using Strassen's method +import numpy as np + +def main(): + x = np.zeros((2, 2), dtype=int) + y = np.zeros((2, 2), dtype=int) + z = np.zeros((2, 2), dtype=int) + + print("Enter the elements of the first matrix (2x2):") + for i in range(2): + for j in range(2): + x[i][j] = int(input()) + + print("Enter the elements of the second matrix (2x2):") + for i in range(2): + for j in range(2): + y[i][j] = int(input()) + + print("\nThe first matrix is:") + for i in range(2): + for j in range(2): + print(f"{x[i][j]}\t", end="") + print() + + print("\nThe second matrix is:") + for i in range(2): + for j in range(2): + print(f"{y[i][j]}\t", end="") + print() + + m1 = (x[0][0] + x[1][1]) * (y[0][0] + y[1][1]) + m2 = (x[1][0] + x[1][1]) * y[0][0] + m3 = x[0][0] * (y[0][1] - y[1][1]) + m4 = x[1][1] * (y[1][0] - y[0][0]) + m5 = (x[0][0] + x[0][1]) * y[1][1] + m6 = (x[1][0] - x[0][0]) * (y[0][0] + y[0][1]) + m7 = (x[0][1] - x[1][1]) * (y[1][0] + y[1][1]) + + z[0][0] = m1 + m4 - m5 + m7 + z[0][1] = m3 + m5 + z[1][0] = m2 + m4 + z[1][1] = m1 - m2 + m3 + m6 + + print("\nResultant matrix:") + for i in range(2): + for j in range(2): + print(f"{z[i][j]}\t", end="") + print() + +if __name__ == "__main__": + main() diff --git a/Project-Structure.md b/Project-Structure.md index c8530754d4..bf907501e8 100644 --- a/Project-Structure.md +++ b/Project-Structure.md @@ -45,6 +45,7 @@ * Branch And Bound * [8 Puzzle](Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Branch_and_Bound/8_puzzle.py) * Divide And Conquer + * [Strassen'S Matrix Multiplication](Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Divide_and_Conquer/Strassen's_Matrix_Multiplication.py) * [Binary Search](Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Divide_and_Conquer/binary_search.py) * [Merge Sort](Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Divide_and_Conquer/merge_sort.py) * [Min Max](Algorithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Divide_and_Conquer/min_max.py)