Data structure -- c language implementation of sparse matrix transpose (column order increasing algorithm) (super detailed notes / experimental report)

Data structure - c language implementation of sparse matrix transpose (column order increasing algorithm) (super detailed notes / experimental report)

Knowledge review

Sparse matrix refers to a matrix in which most elements are zero. Intuitively, when the number of non-zero elements is less than 30% of the total elements, such a matrix is a sparse matrix.

Triple table representation of sparse matrix

For the compressed storage of sparse matrix, the method of storing only non-zero elements is adopted. Because the non-zero elements in the coefficient matrix a i j a_{ij} The distribution of aij ^ is irregular. Therefore, while storing the value of non-zero element, we must also store the position information of the row number and column number of the non-zero element in the matrix, which is the triplet table representation of sparse matrix.

Experimental topic

  1. The sparse matrix adopts the storage structure of triple sequential table
  2. It can realize transpose by increasing in column order
  3. Triples before and after transpose of output matrix

Experimental purpose

  1. Understand the fundamentals of compressed storage
  2. Understand the compressed storage method of triple table of sparse matrix
  3. Familiar with the implementation of sparse matrix operation represented by triple table

Experimental requirements

  1. Triple representation of sparse matrix and implementation of transpose of matrix represented by triple
  2. Implementation of sparse matrix correlation operation represented by triples

Experiment contents and steps

1. Demand analysis

 The user inputs a triple table, and the program transposes it and outputs it.

2. Outline design

 Design a function to realize transpose operation, and let the user input and output the results in the main function.

3. Detailed design

Import the library and define the basic types of triples and triples tables

#include<stdio.h>
#define MAXSIZE 20
typedef struct
{
    int row,col,val;//Row table and column subscripts and values of non-zero elements
}TriNode;

typedef struct
{
    TriNode data[MAXSIZE+1];//Triple table of non-zero elements. data[0] not used
    int m,n,len;//The number of rows, columns and non-zero elements of the matrix
}TriTable;

Function of increasing transpose of sparse matrix column order

//Sparse matrix column order increasing transpose
void Transpose(TriTable A,TriTable *B)s
{
    int i,j,k;
    B->m=A.n;
    B->n=A.m;
    B->len=A.len;//The number of rows and columns are exchanged, and the length is equal
//    printf("%d %d %d\n",B->m,B->n,B->len);
    if(B->len>0)
    {
        j=1;//j is the auxiliary counter, which records the subscript value of the transposed triplet in triplet B
        for(k=1;k<=A.n;k++)
        {
            for(i=1;i<=A.len;i++)
            {
                if(A.data[i].col==k)
                {
                    B->data[j].col=A.data[i].row;
                    B->data[j].row=A.data[i].col;
                    B->data[j].val=A.data[i].val;
                    j++;
                }
            }
        }
    }
}

Main function part

  • This is to let the user input the relevant information of the matrix that needs to be transposed, then call the function transposed by the matrix, and output the transposed matrix.

4. Commissioning analysis

Problems encountered and Solutions

  • At first, it was found that the triplet with the maximum number of columns of the matrix could not be transposed. Later, it was found that < = in the condition judgment was written incorrectly<

Spatiotemporal analysis of algorithm

The algorithm has realized compressed storage, so the space complexity is saved compared with ordinary storage.
The time consumption of the algorithm is mainly in the double loop, and its time complexity is O ( A . n ∗ A . l e n ) O(A.n*A.len) O(A.n∗A.len). In the worst case, when all the elements in the matrix are non-zero elements, the time complexity is O ( A . m ∗ A . n 2 ) O(A.m*A.n^2) O(A.m * A.n2) if the classical algorithm is used to realize matrix transpose, the algorithm time complexity is O ( A . m ∗ A . n ) O(A.m*A.n) O(A.m∗A.n). Therefore, triple table representation

experimental result

There is no function written in the experimental guidance. I wrote a transposed function in combination with the textbook and experimental guidance, which makes the portability of the program stronger and prepares for a positioning and rapid transposition later!

Experimental summary

The sequence string needs to pay attention to a lot of details. Repeat it a lot and refine it into steel!

Finally, the complete code is attached

#include<stdio.h>
#define MAXSIZE 20
typedef struct
{
    int row,col,val;//Row table and column subscripts and values of non-zero elements
}TriNode;

typedef struct
{
    TriNode data[MAXSIZE+1];//Triple table of non-zero elements. data[0] not used
    int m,n,len;//The number of rows, columns and non-zero elements of the matrix
}TriTable;

//Sparse matrix column order increasing transpose
void Transpose(TriTable A,TriTable *B)s
{
    int i,j,k;
    B->m=A.n;
    B->n=A.m;
    B->len=A.len;//The number of rows and columns are exchanged, and the length is equal
//    printf("%d %d %d\n",B->m,B->n,B->len);
    if(B->len>0)
    {
        j=1;//j is the auxiliary counter, which records the subscript value of the transposed triplet in triplet B
        for(k=1;k<=A.n;k++)
        {
            for(i=1;i<=A.len;i++)
            {
                if(A.data[i].col==k)
                {
                    B->data[j].col=A.data[i].row;
                    B->data[j].row=A.data[i].col;
                    B->data[j].val=A.data[i].val;
                    j++;
                }
            }
        }
    }
}

int main()
{
    TriTable s;
    printf("Please enter the number of rows, columns and non-zero elements of the sparse matrix(Separated by spaces): ");
//    scanf("%d",&s.m);
//    scanf("%d",&s.n);
//    scanf("%d",&s.len);
    scanf("%d%d%d",&s.m,&s.n,&s.len);
//    printf("%d%d%d",s.m,s.n,s.len);
    if(s.len==0)
        return 0;
    int val;
    val=s.len;
    int i;
    for(i=1;i<=val;i++)
    {
        printf("The first%d Elements<Row number, column number, value>: ",i);
        scanf("%d%d%d",&s.data[i].row,&s.data[i].col,&s.data[i].val);
    }
//    printf("%d",s.data[2].val);
    printf("Triple before transpose<Row number, column number, value>: \n");
    for(i=1;i<=s.len;i++)
    {
        printf("%d ,%d ,%d \n",s.data[i].row,s.data[i].col,s.data[i].val);
    }
    TriTable t;
    Transpose(s,&t);
    printf("Transposed triplet<Row number, column number, value>: \n");
    for(i=1;i<=t.len;i++)
    {
        printf("%d ,%d ,%d \n",t.data[i].row,t.data[i].col,t.data[i].val);
    }
    return 0;
}

Tags: C Algorithm data structure

Posted on Thu, 28 Oct 2021 19:00:34 -0400 by dannyb785