5. DEVELOP A MAPREDUCE PROGRAM TO IMPLEMENT MATRIX MULTIPLICATION

Multiplication
Python Program:
r1,c1=map(int,input("Enter rows,columns of Matrix A: ").split())
print("Enter Matrix A:")
A=[]
for i in range(r1):
A.append(list(map(int,input().split())))
r2,c2=map(int,input("Enter rows,columns of Matrix B: ").split())
if c1!=r2:
print("Not Possible: columns of A must equal rows of B")
exit()
print("Enter Matrix B:")
B=[]
for i in range(r2):
B.append(list(map(int,input().split())))
# MAP
mapped=[]
for i in range(r1):
for j in range(c2):
for k in range(c1):
mapped.append(((i,j),A[i][k]*B[k][j]))
# SHUFFLE AND SORT
grouped={}
for key,value in mapped:
if key not in grouped:
grouped[key]=[]
grouped[key].append(value)
# REDUCE
result={}
for key,values in grouped.items():
result[key]=sum(values)
print("Output:")
for i in range(r1):
for j in range(c2):
print(i,j,result[(i,j)])
Input:
Enter rows,columns of Matrix A: 2 2
Enter Matrix A:
1 2
3 4
Enter rows,columns of Matrix B: 2 2
Enter Matrix B:
5 6
7 8
Output:
0 0 19
0 1 22
1 0 43
1 1 50
