Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program To Determine If a Given Matrix is a Sparse Matrix
A matrix is a rectangular array of numbers arranged in rows and columns. A sparse matrix is one that contains significantly more zero elements than non-zero elements typically when more than half of the elements are zero.
What is a Sparse Matrix?
Consider this example matrix ?
[0, 0, 3, 0, 0] [0, 1, 0, 0, 6] [1, 0, 0, 9, 0] [0, 0, 2, 0, 0]
This 4×5 matrix has 20 total elements but only 6 non-zero values. Since 14 out of 20 elements are zero (70%), this qualifies as a sparse matrix.
To determine if a matrix is sparse, we check if the number of zero elements exceeds half the total elements ?
if zero_count > (total_elements / 2):
# Matrix is sparse
Method 1: Using Nested Loops
We can iterate through all elements and count zeros ?
def is_sparse_matrix(matrix, rows, cols):
zero_count = 0
# Count zeros using nested loops
for i in range(rows):
for j in range(cols):
if matrix[i][j] == 0:
zero_count += 1
# Check if zeros exceed half the total elements
return zero_count > (rows * cols) // 2
# Test matrix
matrix = [[0, 0, 3],
[0, 0, 0],
[1, 8, 0]]
print("Original matrix:")
for row in matrix:
print(row)
rows, cols = len(matrix), len(matrix[0])
if is_sparse_matrix(matrix, rows, cols):
print("Result: This is a sparse matrix")
else:
print("Result: This is not a sparse matrix")
Original matrix: [0, 0, 3] [0, 0, 0] [1, 8, 0] Result: This is a sparse matrix
Method 2: Using count() Method
A more pythonic approach using the built-in count() method ?
def is_sparse_matrix_optimized(matrix, rows, cols):
zero_count = 0
# Count zeros in each row using count()
for row in matrix:
zero_count += row.count(0)
return zero_count > (rows * cols) // 2
# Test with different matrix
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print("Original matrix:")
for row in matrix:
print(row)
rows, cols = len(matrix), len(matrix[0])
if is_sparse_matrix_optimized(matrix, rows, cols):
print("Result: This is a sparse matrix")
else:
print("Result: This is not a sparse matrix")
Original matrix: [1, 2, 3] [4, 5, 6] [7, 8, 9] Result: This is not a sparse matrix
Method 3: Using SciPy Library
SciPy provides built-in support for sparse matrices ?
from scipy.sparse import issparse, csr_matrix
# Original matrix
matrix = [[0, 0, 3],
[0, 0, 0],
[1, 8, 0]]
# Convert to sparse matrix format
sparse_matrix = csr_matrix(matrix)
print("Original matrix in sparse format:")
print(sparse_matrix)
print("\nNon-zero elements:")
print(sparse_matrix.toarray())
# Check if object is a sparse matrix
if issparse(sparse_matrix):
print("Result: This is a sparse matrix object")
else:
print("Result: This is not a sparse matrix object")
Original matrix in sparse format: (0, 2) 3 (2, 0) 1 (2, 1) 8 Non-zero elements: [[0 0 3] [0 0 0] [1 8 0]] Result: This is a sparse matrix object
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| Nested Loops | O(m×n) | Educational purposes |
| count() Method | O(m×n) | Pythonic approach |
| SciPy | O(1) for checking | Scientific computing |
Note: SciPy's issparse() checks if an object is a sparse matrix instance, not whether the matrix mathematically qualifies as sparse based on zero density.
Conclusion
A matrix is sparse when more than half its elements are zero. Use nested loops for basic checking, count() for cleaner code, or SciPy for scientific applications with built-in sparse matrix support.
