Skip to content

Commit

Permalink
Merge pull request #958 from shuvojitss/pr-2
Browse files Browse the repository at this point in the history
Added Strassen's Matrix Multiplication in divide and conquer
  • Loading branch information
UTSAVS26 authored Nov 4, 2024
2 parents e547a27 + 0fd3b89 commit 6b7d49a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6b7d49a

Please sign in to comment.