-
Notifications
You must be signed in to change notification settings - Fork 820
/
SparseMatrixMultiplication.java
73 lines (68 loc) · 1.8 KB
/
SparseMatrixMultiplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* (C) 2024 YourCompanyName */
package array;
/**
* Created by gouthamvidyapradhan on 03/12/2017.
*
* <p>Given two sparse matrices A and B, return the result of AB.
*
* <p>You may assume that A's column number is equal to B's row number.
*
* <p>Example:
*
* <p>A = [ [ 1, 0, 0], [-1, 0, 3] ]
*
* <p>B = [ [ 7, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 1 ] ]
*
* <p>| 1 0 0 | | 7 0 0 | | 7 0 0 | AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 | | 0 0 1 |
*
* <p>Solution: Skip the rows and columns which you already know would result in zero after
* multiplication.
*/
public class SparseMatrixMultiplication {
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
int[][] A = {{1, 0, 0, 1}};
int[][] B = {{7, 0, 0}, {0, 0, 0}, {0, 0, 1}, {0, 0, 1}};
int[][] C = new SparseMatrixMultiplication().multiply(A, B);
}
public int[][] multiply(int[][] A, int[][] B) {
boolean[] AH = new boolean[A.length]; // Metadata for matrix A
boolean[] BH = new boolean[B[0].length]; // Metadata for matrix A
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A[0].length; j++) {
if (A[i][j] != 0) {
AH[i] = true;
break;
}
}
}
for (int i = 0; i < B[0].length; i++) {
for (int j = 0; j < B.length; j++) {
if (B[j][i] != 0) {
BH[i] = true;
break;
}
}
}
int[][] C = new int[A.length][B[0].length];
for (int i = 0; i < C.length; i++) {
if (AH[i]) {
for (int j = 0; j < C[0].length; j++) {
if (BH[j]) {
int sum = 0;
for (int k = 0; k < A[0].length; k++) {
sum += A[i][k] * B[k][j];
}
C[i][j] = sum;
}
}
}
}
return C;
}
}