Skip to main content

Sparse Matrix and Algorithm

Sparse Matrix is a special kind of matrix. In this matrix many of the elements are not important. They are equal to zero. Non- important elements do not require to be processed. Yes, Sparse Matrix is considered lightly. It is just given a passing reference. Empty operations are performed on Sparse Matrix elements because such elements are insignificant.

Criterion: I have assumed that if the number of zeros in a matrix are more than the 'result' obtained after dividing the total number of matrix elements by two, then the matrix is a Sparse Matrix. 

______________________________

______________________________

Sparse Matrix Specifications:

_____________________________

_____________________________

1 ) No. of rows = m

2 ) No. of columns = n

3 ) No. of elements = m × n

4 ) Enter matrix elements one by one

5 ) Half No. of elements hn = (m × n)/2

6 ) No. of zeroes = nz

7 )if ( nz >= hn )

 Display 'Sparse Matrix'

 else 

   Display 'Not a Sparse Matrix'

8) Stop

______________________________

_________________________

_________________________

Sparse Matrix Algorithm:

________________________

________________________

1 ) Start

2 ) Enter rows, m

3 ) Enter columns, n

4 ) No. of elements, ne = m × n

5 ) Enter matrix elements, No. = ne

6 ) No. of zeroes, nz = 0

7 ) for( rowindx = 0; rowindx < m;      

             ++rowindx )  {

              for( colindx = 0; colindx < n; 

                      ++colindx ) {

                        if(                        

                        matrix[rowindx] [colindx] == 0 )      {

                           ++nz;

              } /* end: if */

    } /* end for - colindx */

} /* end for - rowindx */

8 ) Half No. of elements, hn = ne / 2;

9 ) if( nz >= hn )

       Display 'Matrix is a Sparse Matrix'

         else

          Display 'Not a Sparse Matrix'


10 ) Stop

__________________________________

Tracing the Algorithm:

__________________________________

Sample Case I:

1 ) Start

2 ) Rows: 3 (  m )

3 ) Columns: 3 (  n )

4 ) ne = m × n = 3 × 3 = 9

5 ) Matrix Elements:

___________________________________

                 colI               col2         col3

rowI           4                    0             0

row2         5                    0              0

row3         0                    9              0

____________________________________

6 ) No. of zeroes, nz = 0

7 ) 

1 /a/ * ) 

____________________________________

1st outer loop visit, with rowindx = 0:-

__________________________________________

for( rowindx = 0; ( rowindx  < m )  --> ( (0 < 3) --> true ) ; ++rowindx → 1 )

 {

  _________________________________________


///////////////////////////////////////////////////////

2 /i/ * ) 1st time inner loop:


for( colindx = 0;  ( colindx < n ) --> ( 0 < 3 → true ); ++colindx -->1 ) {

    if(  ( matrix[rowindx][colindx] == 0 )

    -->   ( matrix[0][0] == 0 ) --> ( 4 == 0 → false ) ) {

                /* ++nz; not executed */

    } /* end: if */

} /* end for - colindx */

/////////////////////////////////////////////////////////

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition ( 4 == 0 ) → false. So,  ++nz is not executed. 

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

______________________________________

1 /a/ * ) rowindx = 0, continued:-

______________________________________

///////////////////////////////////////////////////////

2 /ii/ * ) 2nd time inner loop:


for( ; colindx < n   --> ( 1 < 3  → true ) ; ++colindx → 2 ) {

    if( ( matrix[rowindx][colindx] == 0 ) → (  matrix[0][1] == 0 ) →  ( 0 == 0 → true )  ) {

     ++nz; → 1

   } /* end: if */

}  /* end for - colindx */

///////////////////////////////////////////////////////


____________________________________

1 /a/ * )  rowindx = 0, continued :-

____________________________________

/////////////////////////////////////////////////////////

2 /iii/ *) 3rd time inner loop:


for( ; colindx < n  →  (  2 < 3   --> true ); ++colindx → 3 ) {

   if( ( matrix[0][2] == 0 ) --> ( 0 == 0  --> true ) ) {

              ++nz; --> 2 

         } /* end: if */

} /* end for - colindx */

///////////////////////////////////////////////////////

_______________________________

1 /a/ * ) rowindx = 0, continued:-

________________________________

///////////////////////////////////////////////////////

2 /iv/ * )  4th time inner loop:

for( ; colindx < n → ( 3 < 3 → false ); ) 

} /* end for - colindx * /

////////////////////////////////////////////////////////

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition checking ( 3 < 3  → false ) stops us from entering the inner loop for the 4th time. We move on  to the next pass of the  outer loop  with rowindx = 1.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

_________________________________________

2nd outer loop visit, with rowindex = 1:-

________________________________________

for(; rowindx < m --> ( 1 < 3  --> true ); ++rowindx → 2  ) {

__________________________________________

/////////////////////////////////////////////////////////

2 /i/ ^ ) 1st time inner loop: 


for( colindx = 0;  colindx < n  --> ( 0 < 3 → true ); ++colindx --> 1 ) {

         if( ( matrix[rowindx][colindx] == 0 )  ( matrix[1][0]== 0 ) -->  ( 5 == 0 → false) ) {

      /* ++nz; not executed */

   } /* end: if */

} /* end for - colindx */

////////////////////////////////////////////////////////

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition ( 5 == 0 ) → false. So, ++nz is not executed.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

______________________________________

1 /b/ ^ ) rowindx = 1, continued:-

______________________________________


///////////////////////////////////////////////////////

2 /ii/ ^ ) 2nd time inner loop: 


for(; colindx < n → ( 1 < 3 → true ); ++colindx → 2 ) {

     if( ( matrix[rowindx][colindx]  == 0 ) → ( matrix[1][1] == 0 ) → ( 0 == 0 → true) ) {

                                ++nz; → 3 

           } /* end: if */

} /* end for - colindx */

////////////////////////////////////////////////////////

_____________________________________

1 /b/ ^ ) rowindx = 1, continued:-

_____________________________________

///////////////////////////////////////////////////////

2 /iii/ ^ ) 3rd time inner loop:

for(; colindx < n → 2 < 3 → true; ++colindx -- > 3) {

   if( matrix[1][2] = 0 -->  ( 0 == 0 → true ) )

                      ++nz; → 4

  } /* end: if */

} /* end for - colindx */

////////////////////////////////////////////////////////

2 /iv/ ^ ) 4th time inner loop:

for(; colindx < 3 →  3 < 3 --> false; )


/////////////////////////////////////////////////////////

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition checking ( 3 < 3  → false ) stops us from entering the inner loop for the 4th time. We move on  to the next pass of the outer loop with rowindx = 2.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

_____________________________________

1 /c/ # )

3rd visit outer loop, with rowindx = 2:-

_____________________________________

for(;( rowindx < 3 ) = (  2 < 3  -> true ); ++rowindx -> 3 ) {

///////////////////////////////////////////////////////

2 /i/ # ) 1st time inner loop:

for(colindx = 0; colindx <  n → ( 0 < 3 → true ); ++colindx → 1)  {                 

                                                              if( (matrix[rowindx][colindx] == 0 )--> ( matrix[2][0]  == 0 ) → ( 0 == 0 --> true ) )                 {

                   ++nz; → 5 

   } /* end: if */

} /* end for - colindx */

/////////////////////////////////////////////////////////

_____________________________________

1 /c/ # ) rowindx = 2, continued:-

______________________________________

/////////////////////////////////////////////////////////

2 /ii/ # ) 2nd time inner loop: 

for(;colindx < n → ( 1 < 3 → true ); ++colindx → 2) {

       if( (matrix[rowidx][colindx] == 0 ) → ( matrix[2][1] == 0 ) → ( 9 == 0 --> false ) )

    /* ++nz; not executed */

       } /* end: if */

} /* end for - colindx */

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition ( 9 == 0 ) → false. So, ++nz is not executed.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

_____________________________________

1 /c/ # ) rowindx = 2, continued:-

___________________________________

/////////////////////////////////////////////////////////

2 /iii/ # ) 3rd time inner loop: 

for(; colindx < m → ( 2 < 3  → true); ++colindx → 3) {

      if( ( matrix[rowindx][colindx] ==0 ) --> ( matrix[2][2] == 0 ) → ( 0 == 0 → true) ) {

                  ++nz; → 6

     } /* end: if

} /* end for - colindx */

////////////////////////////////////////////////////////

______________________________________

1 /c/ # ) rowindx = 2, continued:-

______________________________________

/////////////////////////////////////////////////////////

2 /iv/ # ) 4th time inner loop: 


for(; colindx < n → ( 3 < 3 ) --> false;) {


} /* end for - colindx */

///////////////////////////////////////////////////////

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition checking ( 3 < 3 → false ) stops us from entering the inner loop for the 4th time. We move on to the next pass of the outer loop with rowindx = 3.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

__________________________________

1 /d/ % )

4th visit outer loop: Isn't possible as,


for(; ( rowindx < m ) -> ( ( 3  < 3 ) -> false ); )

} /* end for - rowindx */

______________________________________

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition ( 3 < 3 ) → false. Entering outer loop with loop index identifier 'rowindx' isn't possible.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

8 ) hn = ne / 2 = 9 / 2 = 4

9) if( nz > hn = ( 6 > 4 ) = true )

        Display 'Sparse Matrix'

10) Stop

☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆

Conclusion: In the sample matrix six out of nine elements are zeros. After processing the matrix has been correctly characterised as a .Sparse Matrix.

☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆


__________________________________

__________________________________

Sample Case II:

1 ) Start

2 ) Rows: 2 (  m )

3 ) Columns: 2 (  n )

4 ) n = m × n = 2 × 2 = 4

5 ) Matrix Elements:

___________________________________

                 colI               col2        

rowI           4                    5            

row2          9                    0            

____________________________________

6 ) No. of zeroes, nz = 0

7 ) 

________________________________________

1 /a/ * ) 

1st outer loop visit, with rowindx = 0:-

_________________________________________

for( rowindx = 0; ( rowindx  < m )  --> ( 0 < 2 --> true ) ; ++rowindx → 1 ) {

______________________________________///////////////////////////////////////////////////////

2 /i/ *) 1st time inner loop: 

for( ; colindx < n   --> ( 0 < 2  → true ) ; ++colindx → 1 ) {

    if( ( matrix[rowindx][colindx] == 0 ) → (  matrix[0][1] == 0 ) →  ( 4 == 0 → false ) ) {

        /* ++nz; not executed */

     } /* end: if */

} /* end for - colindx */

/////////////////////////////////////////////////////////

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition ( 4 == 0 ) → false. So, ++nz, within 'if' statement is not executed.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

____________________________________

1 /a/ * )  rowindx = 0, continued :-

____________________________________

///////////////////////////////////////////////////////

2 /ii/ * )  2nd time inner loop:

for( ; colindx < n → ( 1 < 2 → true ); ++colindx → 2; ) {

     if( ( matrix[rowindx][colindx] == 0; ) → ( matrix[0][1] == 0 ) → ( 5 == 0 → false ) {

             /* ++nz; not executed */

    } /* end: if */

} /* end for - colindx */

/////////////////////////////////////////////////////////

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition ( 5 == 0 → false ). So, ++nz, within 'if' statement, is not executed.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

_____________________________________

1 /a/ * ) rowindx = 0, continued:-

______________________________________

/////////////////////////////////////////////////////////

2 /iii/ * ) 3rd time inner loop:

for( ;colindx < n → ( 2 < 2 → false ); ) {

} /* end for - colindx */

/////////////////////////////////////////////////////////

Note: Condition checking ( 2 < 2  → false ) stops us from entering the inner loop for the 3rd time. We move on  to the next pass of the  outer loop  with rowindx = 1.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

_____________________________________

1 /b/ ^ ) 

2nd outer loop visit, with rowindex = 1:-

for(; rowindx < m --> ( 1 < 2  --> true ); ++rowindx → 2  ) {

_________________________________,_____

////////////////////////////////////////////////////////

2 /i/ ^ ) 1st time inner loop: 

 

for( colindx = 0;  colindx < n  --> ( 0 < 2 → true ); ++colindx --> 1 ) {

         if( ( matrix[rowindx][colindx] == 0 )  ( matrix[1][0]== 0 ) -->  ( 9 == 0 → false) ) {

      /* ++nz; not executed */

   } /* end: if */

} /* end for - colindx */

/////////////////////////////////////////////////////////

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition ( 9 == 0 ) → false. So, ++nz, within 'if' statement, is not executed.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

_________________________________

1 /b/ ^ ) rowindx = 1, continued:

_________________________________

/////////////////////////////////////////////////////////

2 /ii/ ^ ) 2nd time inner loop: 

for(; colindx < n → ( 1 < 2 → true ); ++colindx → 2 ) {

     if( ( matrix[rowindx][colindx]  == 0 ) → ( matrix[1][1] == 0 ) → ( 0 == 0 → true) )  {

                                ++nz; → 1

     } /* end: if */

}  /* end for - colindx */


/////////////////////////////////////////////////////////

_______________________________

1 /b/ ^ ) rowindx = 1,continued:

______________________________

///////////////////////////////////////////////////////

2 /iii/ ^ ) 3rd time inner loop:

for( ; colindx < n → 2 < 2 → false; ) {


} /* end for - colindx */

/////////////////////////////////////////////////////////

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition checking ( 2 < 2  → false ) stops us from entering the inner loop for the 3rd time. We move on  to the next pass of the outer loop with rowindx = 2.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

_____________________________________

1 /c/ # )

3rd visit outer loop, with rowindx = 2: 

for(;( rowindx < 2 ) = (  2 < 2 -> false ); )


} /* end for - rowindx */

______________________________________

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note: Condition ( 2 < 2 ) → false. Entering outer loop with loop index identifier 'rowindx' isn't possible.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

8 ) hn = ne / 2 = 4 / 2 = 2

9) if( nz > hn = ( 1 > 2 ) = false )

        Display 'Not a Sparse Matrix'

10) Stop

☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆

Conclusion: In the sample matrix as three out of four elements are non-zero, hence the matrix is not a Sparse Matrix. 

☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆























Comments

Popular posts from this blog

Local-Area Networks

Local-Area Networks ( LANs ) are restricted to a small area. Such Networks may be established within a building. Computers that are part of Local-Area Networks are Personal Computers ( PCs ) or microcomputers. These Computers are connected to each other. Simple communication lines connect these Pers Networks bring people together. When people join  hands teamwork becomes possible. The structural composition of LANs simplifies teamwork. In other words, LAN hardware supports teams having similar processing requirements. LAN Computers readily support personalization of tasks allocated to professionals.  Office staff can work together and share information. This improves the efficiency of the entire workforce. It has been noted that here all the Networked Computers are Personal Computers. They are of equal capacity. Mostly LAN users have similar tasks. If situations allow, a user is independent to access any Computer to serve his purpose. LAN users can exercise the option of secu...

Computers - An Introduction

Computers are man made machines. They are made to work very fast. Working fast they can solve  big and small problems.  Modern science has given us these computers. They are very popular. Computers affect and change lives. They keep us happy.The happiness grows. Computers are quick and smart. Great problem solvers and simplifiers are our computers. They can do our work without stopping. Focus is on doing only what is needed. Not needed, not done.  Computers are  life-less servants. They cannot think or make decisions on their own. They are empowered to work. They are given each and every thing so that they carry out our orders. Orders are respected by computers. The manner in which computers work is wholly dependent on man.  The quality of service provided by computers is remarkable. They are loved by human beings. Computers are known for smart and quick service. Computers do not give wrong results. Correct answers are always given. Questions which are difficult...

Euclid's Algorithm

The Greatest Common Divisor ( GCD ) of  two numbers is a number. The number ( GCD ) causes division of the two numbers. This number ( GCD ) is the greatest number that is possible. This divisor partially forms the two numbers. Adding  the divisor successively may result in formation of the two numbers. One can note that when the divisor is the greatest, the division of the dividend is the sharpest. The division operation is very effective. When the Greatest Common Divisor of two numbers is considered, we think of a number that can cause division of both the numbers. Here the two numbers are unequal. The Greatest Common Divisor causes divisions of the two numbers differently. This is clear from the two different quotient values.  The GCD value is very interesting to note. It relates very well with the two numbers. The GCD of two even numbers is also an even number. The GCD of two odd numbers is an odd number. So, we can say that GCD is an abstraction of the two numbers in ...