Python Program to Check if a given matrix is sparse or not (original) (raw)

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.

  1. Initialize a variable to store the number of zeros in the matrix to 0.
  2. 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.
  3. Calculate the total number of elements in the matrix.
  4. 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.")

`

Output

Yes, 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