-
Notifications
You must be signed in to change notification settings - Fork 43
/
sparse-matrix-multiplication.py
83 lines (71 loc) · 2.16 KB
/
sparse-matrix-multiplication.py
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
"""
311. Sparse Matrix Multiplication
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:
A = [
[ 1, 0, 0],
[-1, 0, 3]
]
B = [
[ 7, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 1 ]
]
| 1 0 0 | | 7 0 0 | | 7 0 0 |
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
| 0 0 1 |
Even direct multiplying gives better result. this question is expecting you to use hash table.
"""
# V0
# TODO : OPTIMIZE THE PROCESS DUE TO THE SPARSE-MATRIX CONDITION
class Solution(object):
def multiply(self, A, B):
m, n, l = len(A), len(A[0]), len(B[0])
res = [[0 for _ in range(l)] for _ in range(m)]
for i in range(m):
for k in range(n):
# not necessary (if A[i][k])
if A[i][k]:
for j in range(l):
res[i][j] += A[i][k] * B[k][j]
return res
# V1
# http://www.voidcn.com/article/p-muhtjhos-qp.html
# https://www.cnblogs.com/grandyang/p/5282959.html
# IDEA : MAXTRIX OP
class Solution(object):
def multiply(self, A, B):
"""
:type A: List[List[int]]
:type B: List[List[int]]
:rtype: List[List[int]]
"""
result = []
BT = zip(*B) # transfer B,get the column of B
length = len(B)
for row in A:
if not any(row):
result.append([0] * length)
continue
result.append([sum(map(lambda (x, y): x*y, zip(row, col)))
if any(col) else 0 for col in BT])
return result
# V2
# Time: O(m * n * l), A is m x n matrix, B is n x l matrix
# Space: O(m * l)
class Solution(object):
def multiply(self, A, B):
"""
:type A: List[List[int]]
:type B: List[List[int]]
:rtype: List[List[int]]
"""
m, n, l = len(A), len(A[0]), len(B[0])
res = [[0 for _ in range(l)] for _ in range(m)]
for i in range(m):
for k in range(n):
if A[i][k]:
for j in range(l):
res[i][j] += A[i][k] * B[k][j]
return res