forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse_mul.py
More file actions
50 lines (40 loc) · 1.46 KB
/
sparse_mul.py
File metadata and controls
50 lines (40 loc) · 1.46 KB
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
"""
Sparse Matrix Multiplication
Given two sparse matrices A and B, return their product A * B.
Skips zero elements for efficiency. A's column count must equal
B's row count.
Reference: https://leetcode.com/problems/sparse-matrix-multiplication/
Complexity:
Time: O(m * n * p) worst case, better with sparsity
Space: O(m * p)
"""
from __future__ import annotations
def sparse_multiply(
mat_a: list[list[int]], mat_b: list[list[int]]
) -> list[list[int]] | None:
"""Multiply two sparse matrices, skipping zero elements.
Args:
mat_a: First matrix of size m x n.
mat_b: Second matrix of size n x p.
Returns:
Product matrix of size m x p, or None if either input is None.
Raises:
Exception: If the matrices have incompatible dimensions.
Examples:
>>> sparse_multiply([[1, 0, 0], [-1, 0, 3]], [[7, 0, 0], [0, 0, 0], [0, 0, 1]])
[[7, 0, 0], [-7, 0, 3]]
"""
if mat_a is None or mat_b is None:
return None
rows_a, cols_a = len(mat_a), len(mat_a[0])
cols_b = len(mat_b[0])
if len(mat_b) != cols_a:
raise Exception("A's column number must be equal to B's row number.")
result = [[0] * cols_b for _ in range(rows_a)]
for i, row in enumerate(mat_a):
for k, elem_a in enumerate(row):
if elem_a:
for j, elem_b in enumerate(mat_b[k]):
if elem_b:
result[i][j] += elem_a * elem_b
return result