Python Program to Check if a given matrix is sparse or not
Last Updated :
21 Apr, 2023
A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse.
Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elements in the matrix,
Examples:
Input : 1 0 3
0 0 4
6 0 0
Output : Yes
There are 5 zeros. This count
is more than half of matrix
size.
Input : 1 2 3
0 7 8
5 0 7
Output: No
To check whether a matrix is a sparse matrix, we only need to check the total number of elements that are equal to zero. If this count is more than (m * n)/2, we return true.
Python3
# Python 3 code to check
# if a matrix is
# sparse.
MAX = 100
def isSparse(array, m, n):
counter = 0
# Count number of zeros
# in the matrix
for i in range(0, m):
for j in range(0, n):
if (array[i][j] == 0):
counter = counter + 1
return (counter > ((m * n) // 2))
# Driver Function
array = [[1, 0, 3],
[0, 0, 4],
[6, 0, 0]]
m = 3
n = 3
if (isSparse(array, m, n)):
print("Yes")
else:
print("No")
Time complexity: O(m*n) where m is no of rows and n is no of columns of matrix
Auxiliary Space: O(1)
Method #2:Using count() function
Python3
# Python 3 code to check
# if a matrix is
# sparse.
def isSparse(array, m, n):
counter = 0
# Count number of zeros
# in the matrix
for i in array:
counter += i.count(0)
return (counter > ((m * n) // 2))
# Driver Function
array = [[1, 0, 3],
[0, 0, 4],
[6, 0, 0]]
m = 3
n = 3
if (isSparse(array, m, n)):
print("Yes")
else:
print("No")
# this code is contributed by vikkycirus
Please refer complete article on Check if a given matrix is sparse or not for more details!
Approach#3: Using sum
The idea is to traverse through each element in the matrix. If an element is equal to 0, increment the counter for the number of zeros in the matrix. Check if the number of zeros in the matrix is greater than half of the total number of elements in the matrix, if so, the matrix is sparse.
- Initialize a variable to store the number of zeros in the matrix to 0.
- Traverse through each element in the matrix using two nested loops and check if an element is equal to 0, increment the counter for the number of zeros in the matrix.
- Calculate the total number of elements in the matrix.
- Check if the number of zeros in the matrix is greater than half of the total number of elements in the matrix, if so, print "Yes, the matrix is sparse." Otherwise, print "No, the matrix is not sparse."
Python3
# Example input matrix
matrix = [[1, 0, 3],
[0, 0, 4],
[6, 0, 0]]
# Counting the number of zeros in the
# matrix using list comprehension
num_zeros = sum(1 for row in matrix for num in row if num == 0)
# Checking if the matrix is sparse or not
if num_zeros > (len(matrix) * len(matrix[0])) / 2:
print("Yes, the matrix is sparse.")
else:
print("No, the matrix is not sparse.")
OutputYes, the matrix is sparse.
Time Complexity: O(n2), where n is the size of the matrix. We are traversing through each element in the matrix once.
Space Complexity: O(1). We are only using a constant amount of extra space to store the number of zeros in the matrix.
Similar Reads
Python Program to check if a matrix is symmetric A square matrix is said to be a symmetric matrix if the transpose of the matrix is the same as the given matrix. The symmetric matrix can be obtained by changing row to column and column to row. Examples: Input : 1 2 3 2 1 4 3 4 3Output : Yes Input : 3 5 8 3 4 7 8 5 3Output : No Method 1: A Simple s
3 min read
Python program to Convert a Matrix to Sparse Matrix Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory.Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indic
2 min read
Python Program to check if matrix is upper triangular Given a square matrix and the task is to check the matrix is in upper triangular form or not. A square matrix is called upper triangular if all the entries below the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 3, 5, 3}, {0, 4, 6, 2}, {0, 0, 2, 5}, {0, 0, 0, 6}}; Output : Matrix is in
2 min read
Python Program that filters out non-empty rows of a matrix Given Matrix, the following article shows how to filter all the Non-Empty rows of a matrix. In simpler terms, the codes provided below return a matrix after removing empty rows from it. Input : test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []] Output : [[4, 5, 6, 7], [9, 8, 1]] Explanation : All emp
4 min read
Python Program to check Involutory Matrix Given a matrix and the task is to check matrix is an involutory matrix or not. Involutory Matrix: A matrix is said to be an involutory matrix if the matrix multiplies by itself and returns the identity matrix. The involutory matrix is the matrix that is its own inverse. The matrix A is said to be an
3 min read