Advertisement
1988coder

311. Sparse Matrix Multiplication

Jan 18th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.util.HashMap;
  2.  
  3. /**
  4.  * First convert the input matrix to sparse format (into HashMap of HashMaps).
  5.  * Then calculate the product and the convert back the result to 2D matrix.
  6.  *
  7.  * Time Complexity: Convert A[][] to sparseA -> O(rA*cA). Convert B[][] to
  8.  * sparseB -> O(rB*cB). Product -> O(rA*cA*cB). Convert sparseC to C[][] ->
  9.  * O(rA*cB). Thus total time Complexity -> O(rA*cA*cB).
  10.  *
  11.  * Space Complexity: If input/output is in 2D array -> O(rA*cA + rB*cB + rA*cB).
  12.  * If input/output is in sparse format -> O(1).
  13.  *
  14.  * In Facebook Dot Product of Sparse Vector is asked.
  15.  * https://www.cnblogs.com/EdwardLiu/p/6399867.html
  16.  */
  17. class Solution {
  18.     public int[][] multiply(int[][] A, int[][] B) throws IllegalArgumentException {
  19.         if (A == null || B == null) {
  20.             return new int[0][0];
  21.         }
  22.         if (A[0].length != B.length) {
  23.             throw new IllegalArgumentException("Number of columns in A should be equal to number of rows in B");
  24.         }
  25.  
  26.         int rowA = A.length;
  27.         HashMap<Integer, HashMap<Integer, Integer>> sparseA = getSparseMatrix(A);
  28.  
  29.         int colB = B[0].length;
  30.         HashMap<Integer, HashMap<Integer, Integer>> sparseB = getSparseMatrix(B);
  31.  
  32.         HashMap<Integer, HashMap<Integer, Integer>> sparseC = new HashMap<>();
  33.  
  34.         for (int xA : sparseA.keySet()) {
  35.             for (int yA : sparseA.get(xA).keySet()) {
  36.                 if (!sparseB.containsKey(yA)) {
  37.                     continue;
  38.                 }
  39.                 for (int yB : sparseB.get(yA).keySet()) {
  40.                     int val = sparseA.get(xA).get(yA) * sparseB.get(yA).get(yB);
  41.                     if (!sparseC.containsKey(xA)) {
  42.                         sparseC.put(xA, new HashMap<>());
  43.                     }
  44.                     int preVal = sparseC.get(xA).getOrDefault(yB, 0);
  45.                     sparseC.get(xA).put(yB, preVal + val);
  46.                 }
  47.             }
  48.         }
  49.  
  50.         int[][] C = new int[rowA][colB];
  51.         for (int xC : sparseC.keySet()) {
  52.             for (int yC : sparseC.get(xC).keySet()) {
  53.                 C[xC][yC] = sparseC.get(xC).get(yC);
  54.             }
  55.         }
  56.  
  57.         return C;
  58.     }
  59.  
  60.     private HashMap<Integer, HashMap<Integer, Integer>> getSparseMatrix(int[][] M) {
  61.         HashMap<Integer, HashMap<Integer, Integer>> sparseM = new HashMap<>();
  62.         if (M == null || M.length == 0 || M[0].length == 0) {
  63.             return sparseM;
  64.         }
  65.  
  66.         for (int i = 0; i < M.length; i++) {
  67.             for (int j = 0; j < M[0].length; j++) {
  68.                 if (M[i][j] != 0) {
  69.                     if (!sparseM.containsKey(i)) {
  70.                         sparseM.put(i, new HashMap<>());
  71.                     }
  72.                     sparseM.get(i).put(j, M[i][j]);
  73.                 }
  74.             }
  75.         }
  76.  
  77.         return sparseM;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement