Give 2-step Map Reduce algorithm to multiply two large matrices.

 

M is a matrix with element mi,j in row i and column j.
N is a matrix with element nj,k in row j and column k.
P is a matrix = MN with element Pi,k in row i and column k, where Pi,k = ∑j mi,jnj,k
2-step Map Reduce

First Iteration
Record Reader 1: (M, i, j, mi,j), (N, j, k, nj,k)
Mapper 1: For each matrix element  mi,j emit the key-value pair (j, (M, i, mi,j))
For each matrix element  nj,k emit the key-value pair (j, (N, k, nj,k))
Shuffle 1: [j, (M, i, mi,j), (N, k, nj,k)]
Reducer 1: For each key j, for each value that comes from M, say (M, i, mi,j) and each value that comes from N, say (N, k, nj,k) emit the key-value pair (j, (i, k, mi,j nj,k)).
The output of this reduce function is used as the input to the Map function in the next iteration.

Second Iteration
Mapper 2: For each key-value pair (j, (i, k, mi,j nj,k)) emit the key-value pair ((i, k), p),  where p = mi,j nj,k
Shuffle 2: ( (i, k), [p1, p2, ...pn])
Reducer 2: For each key (i, k) emit the key-value pair (i, k) where p is the sum of the list of values associated with this key and is the value of the element in row i and column k of the matrix P  = MN.
i.e, ( (i, k), Pi, k)