Sparse Matrix Multiplication

Given two sparse matrices A and B, find A x B.

Example

I: A = [
  [ 1, 0, 0],
  [-1, 0, 3]
]

B = [
  [7, 0, 0],
  [0, 0, 0],
  [0, 0, 1]
]

O: Y = [
  [ 7, 0, 0],
  [-7, 0, 3]
]

Analysis

We come up the brute force algorithm pretty easily by following the matrix multiplication rule. To compute YijY_{ij}, we need to multiply elements in ith row from A and jth column from B and sum them up.

Code

The brute force solution only beats 4% of the solutions. But by adding the check for 0 element, it beats 60% of the solution.

Reference

Last updated