




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of a sparse matrix, its representation in tuple form, and the transpose, addition, and time complexities of these operations. It also includes algorithms for converting a sparse matrix to tuple form, finding the transpose of a matrix in tuple form, and adding two matrices represented in tuple form.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





A sparse matrix is a matrix which has many zero entities or majority elements are zeros. col 0 col 1 col 2 col 3 col 4 row 0 5 0 0 3 0 row 1 0 0 2 0 9 row 2 0 0 0 7 0 row 3 0 4 0 0 8 When the majority elements are zeros we can represent arrays in another form called tuple form. In tuple form each row will contain three colums(or three elements). In this form the first row elements are the number of rows in the matrix, next the number of columns and the number of nonzero terms in the matrix. The next row will be the details of the first nonzero term in the matrix ie. the row position, the column position and the value of the nonzero term. Likewise all nonzero terms in the matrix will be represented in the tuple form. The elements A[0,1] and A[0,2] contain the number of rows and columns of the matrix. A[0,3] contains the number of nonzero terms. row column elements col 0 col 1 col 2 col 3 col 4 row 0 5 0 0 3 0 row 1 0 0 2 0 9 row 2 0 0 0 7 0 row 3 0 4 0 0 8
A lot of space can be saved by representing a sparse matrix in tuple form. The following matrix when represted in tuple form will save 20-12 = 8 size. col 0 col 1 col 2 col 3 col 4 row 0 5 0 0 0 0 row 1 0 0 0 2 0 row 2 0 0 0 0 0 row 3 0 0 4 0 0 row column elements 4 5 3 0 0 5 1 3 2 3 2 4 The total space required to store the above matrix in normal form is 4 X 5 = 20. When represented in tuple form the matrix only requires 4 X 3 = 12 spaces. In real situations the space saving will be much higher. Write an algorithm to convert a sparse matrix into tuple form and calculate the time complexity. void sparsematrix(d[][], u[][3], v, w) { int k,l,t,n; // d[][] is the normal matrix // u[][] is the matrix in tuple form // v – the number of rows // w – the number of columns u[0][0]= v; u[0][1]=w; t=0; n=1;
row column elements row column elements 4 5 7 5 4 7 0 0 5 0 0 5 0 3 3 1 3 4 1 2 2 2 1 2 1 4 9 3 0 3 2 3 7 3 2 7 3 1 4 4 1 9 3 4 8 4 3 8 Write an algorithm to find the transpose of a matrix represented in tuple form and find the time complexity. void transpose(sp[][3], trans[][3]) { int i,j,k=1; trans[0][0] = sp[0][1]; trans[0][1] = sp[0][0]; trans[0][2] = sp[0][2]; for (i=0; i<sp[0][1]; i++) for (j=1; j<=sp[0][2]; j++) if (sp[j][1]==i) { trans[k][0] = sp[j][1]; trans[k][1] = sp[j][0]; trans[k][2] = sp[j][2]; k++; } }
The time complexity of sparse matrix transpose calculation is O(i*j) where i is the number of columns and j is the number of nonzero terms.
col 0 col 1 col 2 col 3 col 4 col 0 col 1 col 2 col 3 col 4 row 0 5 0 0 0 0 row 0 0 2 0 0 6 row 1 0 0 0 7 0 + row 1 0 0 0 0 0 row 2 0 0 0 0 3 row 2 0 0 0 0 - row 3 0 8 0 0 0 row 3 9 4 0 0 0 col 0 col 1 col 2 col 3 col 4 row 0 5 2 0 0 6 row 1 0 0 0 7 0 row 2 0 0 0 0 0 row 3 9 12 0 0 0 The above matrices in tuple form is as follows 4 5 4 4 5 5 4 5 6 0 0 5 0 1 2 0 0 5 1 3 7 + 0 4 6 = 0 1 2 2 4 3 2 4 -3 0 4 6 3 1 8 3 0 9 1 3 7 3 1 4 3 0 9 3 1 12 For matrix addition in tuple form first checks that if the number of rows in first matrix is equal to the number of rows in the second matrix , if they are same then the number of
{ s=g[i][2] + h[j][2]; if(s!=0) { e[k][0]=g[i][0]; e[k][1]=g[i][1]; e[k][2]=s; k++; } i++; j++; } else if (g[i][1]<h[j][1]) {e[k][0]=g[i][0]; e[k][1]=g[i][1]; e[k][2]=g[i][2]; i++;k++; } else if (g[i][1]>h[j][1]) {e[k][0]=h[j][0]; e[k][1]=h[j][1]; e[k][2]=h[j][2]; j++;k++; } } else if(g[i][0]<h[j][0]) {e[k][0]=g[i][0]; e[k][1]=g[i][1]; e[k][2]=g[i][2]; i++;k++; } else if(g[i][0]>h[j][0]) {e[k][0]=h[j][0]; e[k][1]=h[j][1]; e[k][2]=h[j][2]; j++;k++;
if(i<r1+1) { for(m=I; m<r1+1; m++) {e[k][0]=g[m][0]; e[k][1]=g[m][1]; e[k][2]=g[m][2]; k++; } } if(j<r2+1) { for(m=j; m<r2+1; m++) {e[k][0]=h[m][0]; e[k][1]=h[m][1]; e[k][2]=h[m][2]; k++; } } e[0][2]=k-1; } } The time complexity of the algorithm is O(m+n) where m and n are the number of nonzero terms in the first matrix and second matrix respectively.