Sparse metrics, Data Structure & Algorithms

Assignment Help:

Q. Define the sparse metrics and also explain the representation of a 4X4 matrix using linked list.        

Ans:

A matrix in which number of zero entries is quite higher than the number of non zero entries is called the sparse matrix. The natural method or technoque of expressing matrices in memory as two-dimensional arrays may not be appropriate for sparse matrices. One can save the space by storing only nonzero entries. For example matrix A (3*3 matrix) which is represented below

 

0    2      0

5   0     0

0   6     9

can be written in sparse matrix form as:

3   3     4

0    1      2

1   0   5

2   2   6

2   3   9

In this the first row represent the dimension of matrix and last column tells us about the total number of non zero values; from the second row onwards it is giving the location and value of non zero number.

Representation of a 4*4 matrix using linked list is given below:

#define MAX1 4

#define MAX2 4

struct cheadnode           /* structure for col

headnode */

{

int colno ;

struct node *down ;

struct cheadnode *next ;

} ;

struct rheadnode          /* structure for row

headnode */

{

int rowno ;

struct node * right ;

struct rheadnode *next ;

} ;

struct node                  /* structure for node to

store element */

{

int row ; int col ; int val ;

struct node *right ;

struct node *down ;

} ;

struct spmat                /* structure for special headnode */

{

struct rheadnode *firstrow ; struct cheadnode *firstcol ; int noofrows ;

int noofcols ;

} ;

struct sparse

{

int *sp ;

int row  ;

struct spmat *smat ;

struct cheadnode *chead[MAX2] ; struct rheadnode *rhead[MAX1] ; struct node *nd ;

} ;

void initsparse ( struct sparse *p )           /*

initializes structure elements */

{

int i ;

for ( i = 0 ; i < MAX1 ; i++ )            /* create row headnodes */

p -> rhead[i] = ( struct rheadnode * ) malloc (

sizeof ( struct rheadnode ) ) ;

for ( i = 0 ; i < MAX1 - 1 ; i++ ) /* initialize and

link row headnodes together */

{

p -> rhead[i] -> next = p -> rhead[i + 1] ;

p -> rhead[i] -> right = NULL ;

p -> rhead[i] -> rowno = i ;

}

p -> rhead[i] -> right = NULL ;

p -> rhead[i] -> next = NULL ;

for ( i = 0 ; i < MAX1 ; i++ )          /* create col headnodes */

p -> chead[i] = ( struct cheadnode * ) malloc (

sizeof ( struct cheadnode ) ) ;

for ( i = 0 ; i < MAX2 - 1 ; i++ )               /*

initialize and link col headnodes together */

{

p -> chead[i] -> next = p -> chead[i + 1] ;

p -> chead[i] -> down = NULL ;

p -> chead[i] -> colno = i ;

}

p -> chead[i] -> down = NULL ;

p -> chead[i] -> next = NULL ;

/* create and initialize special headnode */

p -> smat = ( struct spmat * ) malloc ( sizeof (

struct spmat ) ) ;

p -> smat -> firstcol = p -> chead[0] ;

p -> smat -> firstrow = p -> rhead[0] ;

p -> smat -> noofcols = MAX2 ;

p -> smat -> noofrows = MAX1 ;

}

void create_array ( struct sparse *p )    /* creates, dynamically the matrix of size MAX1 x MAX2 */

{

int n, i ;

p -> sp = ( int * ) malloc ( MAX1 * MAX2 * sizeof (

int ) ) ;

for ( i = 0 ; i < MAX1 * MAX2 ; i++ )        /*

get the element and store it */

{

printf ( "Enter element no. %d:", i ) ;

scanf ( "%d", &n ) ;

* ( p -> sp + i ) = n ;

}

}


Related Discussions:- Sparse metrics

Sparse matrix, How sparse matrix stored in the memory of a computer?

How sparse matrix stored in the memory of a computer?

Write an algorithm of value in tax using pseudocode, A town contains a tota...

A town contains a total of 5000 houses. Every house owner has to pay tax based on value of the house. Houses over $200 000 pay 2% of their value in tax, houses over $100 000 pay 1.

Explain best - fit method, Best - Fit Method: - This method obtains the sma...

Best - Fit Method: - This method obtains the smallest free block whose  size is greater than or equal to get such a block by traversing the whole free list follows.

Creation of a circular linked list, Program: Creation of a Circular linked ...

Program: Creation of a Circular linked list ALGORITHM (Insertion of an element into a Circular Linked List) Step 1        Begin Step 2      if the list is empty or new

Determine yiq colour model, Determine YIQ Colour Model Whereas an RGB m...

Determine YIQ Colour Model Whereas an RGB monitor requires separate signals for the red, green, and blue components of an image, a television monitor uses a single composite si

Complexity of an algorithm, An algorithm is a sequence of steps to solve a ...

An algorithm is a sequence of steps to solve a problem; there may be more than one algorithm to solve a problem. The choice of a particular algorithm depends upon following cons

Algorithms and flowcharts, write an algorithm and draw a flowchart to calcu...

write an algorithm and draw a flowchart to calculate the perimeter and area of a circle

Write procedure to the insert a node into the linked list, Q. Write a proce...

Q. Write a procedure to the insert a node into the linked list at a particular position and draw the same by taking an example?

Explain the sum of subset problem, a. Explain the sum of subset problem. Ap...

a. Explain the sum of subset problem. Apply backtracking to solve the following instance of sum of subset problem: w= (3, 4, 5, 6} and d = 13. Briefly define the method using a sta

Time converstion, how to convert 12 hour format into 24 hour format using c...

how to convert 12 hour format into 24 hour format using c program

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd