0% found this document useful (0 votes)
4 views

dse_assign05

This document outlines an assignment to implement a C/C++ program for performing operations on sparse matrices, including addition, multiplication, and transposition. It specifies the data structures to be used, grading criteria, and provides details on the expected functionality of the operations. Students must also submit a source code file and an experimental report analyzing the time complexity of their implementations.

Uploaded by

umarhassham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

dse_assign05

This document outlines an assignment to implement a C/C++ program for performing operations on sparse matrices, including addition, multiplication, and transposition. It specifies the data structures to be used, grading criteria, and provides details on the expected functionality of the operations. Students must also submit a source code file and an experimental report analyzing the time complexity of their implementations.

Uploaded by

umarhassham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Operations on Sparse Matrices

1. Overview

In this assignment you will implement a C/C++ program taking two sparse matrices and performing basic
operations such as add, multiply and transpose of the matrices in their compact storage form. The result should
consist of three sparse matrices, one obtained by adding the two input matrices, one by multiplying the
two matrices and one obtained by transposing the second matrix. The sparse matrix is stored in the
sequential form. For those students who are programming in C++, please note that the use of the Standard
Template Library (STL) is NOT allowed.

The grading breakdown is as follows:

 Implementation of functions: 40%


 Tests: 20%
 Design and coding style: 10% (see the style guidelines “DSE coding style guidelines.pdf”)
 Experiment report: 30%

2. Getting started

Download dse_assign05.zip, which contains the skeleton code and the test data required for the assignment.

The Triple and RLSMatrix data type

The Triple data type represents an entry of the matrix or a node in the sequential list.

#define MaxSize 20000 /* Maximum number of non-zero entries in the sparse matrix */
typedef int ElemType;
typedef struct
{
int i, j;// row index and column index
ElemType elem;// element value located at (row,col)
}Triple;

The SMatrix data type represents a sparse matrix.

typedef struct
{
Triple data[MaxSize];
int mu, nu, tu;// row number, column number, number of non-zero entries
int *rpos;// index of the first non-zero element of each row within the triple list, or the i-th value indicates
the number of non-zero elements in the matrix less then row i
int *cpos;// index of the first non-zero element of each column within the triple list, or the j-th value
indicates the number of non-zero elements in the matrix less then column j
}SMatrix,*PSMatrix;

The Operation Specifications

A set of functions is defined to allow a program to perform the add, multiply and transpose operations on sparse
matrices. These functions mainly include:

void TransposeSMatrix(SMatrix M,PSMatrix pT);


void AddSMatrix(SMatrix M, SMatrix N, PSMatrix pT);
void MultiplySMatrix(SMatrix M, SMatrix N, PSMatrix pQ);

Here are brief descriptions of the expected behaviors of these functions.

void TransposeSMatrix(SMatrix M, PSMatrix pT): Store the transpose of the sparse matrix M into pT. To
Transpose a matrix, we can simply change every column value to the row value and vice-versa, however, in this
case, the resultant matrix won’t be sorted as we require. Hence, we initially determine the number of elements
less than the current element’s column being inserted in order to get the exact index of the resultant matrix
where the current element should be placed. This is done by maintaining an array cpos[] whose j-th value
indicates the number of elements in the matrix less than the column j. Refer to the lecture slides for theoretical
course for more algorithm details.

void AddSMatrix(SMatrix M, SMatrix N, PSMatrix pT): Store the addition of the sparse matrices M and N into
pT. To Add the matrices, we simply traverse through both matrices element by element and insert the smaller
element (one with smaller row and col value) into the resultant matrix. If we come across an element with the
same row and column value, we simply add their values and insert the added data (non-zero) into the resultant
matrix. Refer to the lecture slides for algorithm details.

void MultiplySMatrix(SMatrix M, SMatrix N, PSMatrix pQ): Store the multiplication of the sparse matrices M
and N into pQ. One way to Multiply the matrices, we process one row of M in one time. Any row value equal to
x in the first matrix and column value equal to y in the second matrix will contribute towards result[x][y]. This
is obtained by multiplying all such elements having corresponding column value in M and row value in N and
adding only those with the row as x in first matrix and column as y in the second matrix to get the result[x][y].
This is done by maintaining an array rpos[] whose i-th value indicates the number of elements in the matrix less
then row i, and a temporary array ctemp[] to store the adding results for the entire row of pQ. Refer to the
lecture slides for theoretical course for more algorithm details. You can try other algorithms except the
O(m*n*l) native algorithm, where M∈Rm*l, N∈Rl*n.

3. Tasks

Your main task is to implement all of the required functions as described above, and then test these functions
using the test instances provided in the tests.txt file.

Here are brief descriptions of the input and output of the test instances.
Input:

 First input the row number and column number of the first matrix;
 Then input the triple list for non-zero elements of the first matrix. Each line contains row index, column
index (both starting from 1) and value. The triple (0, 0, 0) indicates the end;
 Then input the row number and column number of the second matrix;
 Then input the triple list for non-zero elements of the second matrix. Each line contains row index, column
index (both starting from 1) and value. The triple (0, 0, 0) indicates the end.

Output:

 Result of addition, multiplication and transpose on the second matrix. Also print out the dimension of the
resultant matrix.

Sample input:

44
1 2 10
1 4 12
335
4 1 15
4 2 20
000
44
138
2 4 23
339
4 1 20
4 2 -20
000

Sample output:

Result of Addition:(4×4)
1 2 10
138
1 4 12
2 4 23
3 3 14
4 1 35

Result of Multiplication:(4×4)
1 1 240
1 2 -240
1 4 230
3 3 45
4 3 120
4 4 460

Result of Transpose on the second matrix:(4×4)


1 4 20
2 4 -20
318
339
4 2 23

4. Submission

Submit the completed source code file main.c and the experimental report, of which the template has also
been provided. NOTICE!!! In the experimental report, try to analyze the time complexity of all the three
functions mentioned above and use the Big O notation.

You might also like