TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Determine Whether a Given Matrix is a Sparse Matrix

Program to Determine Whether a Given Matrix is an Sparse Matrix on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Program to determine whether a given matrix is a sparse matrix

Explanation

In this program, we need to check whether the given matrix is the sparse matrix.

Sparse Matrix

A matrix is said to be sparse matrix if most of the elements of that matrix are 0. It implies that it contains very less non-zero elements.

To check whether the given matrix is the sparse matrix or not, we first count the number of zero elements present in the matrix. Then calculate the size of the matrix. For the matrix to be sparse, count of zero elements present in an array must be greater than size/2.

Program to determine whether a given matrix is a sparse matrix

Number of zeroes present in above matrix is 6 and size of the matrix is 3 * 3 = 9. Since, 6 > 4.5 that means, most elements of given array are zero. Hence, the above matrix is a sparse matrix.

Algorithm

  1. Declare and initialize a two-dimensional array a.
  2. Calculate the number of rows and columns present in the given array and store it in variables rows and cols respectively.
  3. Loop through the array and count the number of zeroes present in the given array and store in the variable count.
  4. Calculate the size of the array by multiplying the number of rows with many columns of the array.
  5. If the count is greater than size/2, given matrix is the sparse matrix. That means, most of the elements of the array are zeroes.
  6. Else, the matrix is not a sparse matrix.

Solution

Python

#Initialize matrix a
a = [  
        [4, 0, 0],
        [0, 5, 0],
        [0, 0, 6]
    ];
 
count = 0;
 
#Calculates number of rows and columns present in given matrix
rows = len(a);
cols = len(a[0]);
 
#Calculates the size of the array

size = rows * cols;
 
#Count all zero element present in matrix
for i in range(0, rows):
    for j in range(0, cols):
        if(a[i][j] == 0):
            count = count + 1;
 
if(count > (size/2)):
    print("Given matrix is a sparse matrix");
else:
    print("Given matrix is not a sparse matrix");

Output:

Given matrix is a sparse matrix

C

#include <stdio.h>
 
int main()
{
    int rows, cols, size, count = 0;
        
    //Initialize matrix a
    int a[][3] = {   
                    {4, 0, 0},
                    {0, 5, 0},
                    {0, 0, 6}
                };
    
    //Calculates number of rows and columns present in given matrix
    rows = (sizeof(a)/sizeof(a[0]));
    cols = (sizeof(a)/sizeof(a[0][0]))/rows;
    
    //Calculates the size of array
    size = rows * cols;
 
    //Count all zero element present in matrix
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < cols; j++){
          if(a[i][j] == 0)
            count++;
        }
    }
    
    if(count > (size/2))
        printf("Given matrix is a sparse matrix");
    else
        printf("Given matrix is not a sparse matrix");
        
    return 0;
}

Output:

Given matrix is a sparse matrix

JAVA

public class SparseMatrix
{
    public static void main(String[] args) {
        int rows, cols, size, count = 0;
        
        //Initialize matrix a
        int a[][] = {   
                        {4, 0, 0},
                        {0, 5, 0},
                        {0, 0, 6}
                    };
          
          //Calculates number of rows and columns present in given matrix
          rows = a.length;
        cols = a[0].length;
        
        //Calculates the size of array
        size = rows * cols;
        
        //Count all zero element present in matrix
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < cols; j++){
                if(a[i][j] == 0)
                    count++;
                }
            }
            
        if(count > (size/2))
            System.out.println("Given matrix is a sparse matrix");
        else
            System.out.println("Given matrix is not a sparse matrix");
    }
}

Output:

Given matrix is a sparse matrix

C#

using System;
                    
public class SparseMatrix
{
    public static void Main()
    {
        int rows, cols, size, count = 0;
        
        //Initialize matrix a
        int[,] a = {   
                        {4, 0, 0},
                        {0, 5, 0},
                        {0, 0, 6}
                   };
          
          //Calculates number of rows and columns present in given matrix
          rows = a.GetLength(0);
        cols = a.GetLength(1);
        
        //Calculates the size of array
        size = rows * cols;
        
        //Count all zero element present in matrix
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < cols; j++){
                if(a[i,j] == 0)
                    count++;
                }
            }
            
        if(count > (size/2))
            Console.WriteLine("Given matrix is a sparse matrix");
        else
            Console.WriteLine("Given matrix is not a sparse matrix");
    }
}

Output:

Given matrix is a sparse matrix

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//Initialize matrix a
$a = array(   
            array(4, 0, 0),
            array(0, 5, 0),
            array(0, 0, 6)
           );
 
$count = 0;
    
//Calculates number of rows and columns present in given matrix
$rows = count($a);
$cols = count($a[0]);
 
//Calculates the size of array
$size = $rows * $cols;
 
//Count all zero element present in matrix
for($i = 0; $i < $rows; $i++){
    for($j = 0; $j < $cols; $j++){
      if($a[$i][$j] == 0)
        $count++;
    }
}
 
if($count > ($size/2))
    print("Given matrix is a sparse matrix");
else
    print("Given matrix is not a sparse matrix");
?>
</body>
</html>

Output:

Given matrix is a sparse matrix

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf