-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy path311. Sparse Matrix Multiplication.java
executable file
·114 lines (90 loc) · 3.01 KB
/
311. Sparse Matrix Multiplication.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
M
tags: Hash Table
time: O(mnk), where `m = A.row`, `n = B.col`, `k = A.col = B.row`
space: O(1) extra
给两个matrics, 做乘积. 注意, 是sparse matrix (特点: 很多0).
#### Hash Table
- Recall matric multiplication rules: result[i][j] = sum(A-row[i] * B-col[j])
- `sparse matric: lots positions are zero`
- 平白地写matric multiplication 没有意义, 重点就是optimization:
- `optimization`: for A-zero-row, and B-zero-col, there is no need to calculate, just return 0.
- 1. Find A-zero-rows and store in setA, same for setB
- 2. during multiplication, reduce time complexity.
- Base: O(mnk), where `m = A.row`, `n = B.col`, `k = A.col = B.row`
#### Matrices
- 乘法规则: result[i][j] = sum(A-row[i] * B-col[j])
- A column size == B row size. 并且: 计算顺序是iterate over A column size
```
/*
Given two sparse matrices A and B, return the result of AB.
You may assume that A's column number is equal to B's row number.
Example:
Input:
A = [
[ 1, 0, 0],
[-1, 0, 3]
]
B = [
[ 7, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 1 ]
]
Output:
| 1 0 0 | | 7 0 0 | | 7 0 0 |
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
| 0 0 1 |
*/
// Faster solution
class Solution {
public int[][] multiply(int[][] A, int[][] B) {
if (validate(A, B)) {
return new int[][]{};
}
// iterate over A, B, create setA, setB for reduce row/col
int m = A.length, n = B[0].length, index = B.length;
int[][] rst = new int[m][n];
// base loop, reduce
for (int i = 0; i < m; i++) {
for (int ind = 0; ind < index; ind++) { // index = A col number or B row number
if (A[i][ind] == 0) continue;
for (int j = 0; j < n; j++) {
if (B[ind][j] == 0) continue;
rst[i][j] += A[i][ind] * B[ind][j];
}
}
}
return rst;
}
private boolean validate(int[][] A, int[][] B) {
if (A == null || B == null) return true;
if (A[0].length != B.length) return true;
return false;
}
}
// Original solution, a bit slower
class Solution {
public int[][] multiply(int[][] A, int[][] B) {
if (validate(A, B)) return new int[][]{};
// iterate over A, B
int aRow = A.length, bCol = B[0].length, bRow = B.length;
int[][] rst = new int[aRow][bCol];
// base loop, reduce
for (int i = 0; i < aRow; i++) {
for (int j = 0; j < bCol; j++) {
rst[i][j] += multiple(A, B, i, j);
}
}
return rst;
}
private int multiple(int[][] A, int[][] B, int row, int col) {
int sum = 0;
for (int i = 0; i < B.length; i++) sum += A[row][i] * B[i][col];
return sum;
}
private boolean validate(int[][] A, int[][] B) {
if (A == null || B == null) return true;
if (A[0].length != B.length) return true;
return false;
}
}
```