Data structure--c Language description Fifth (2) sparse matrix transpose algorithm __c#

Source: Internet
Author: User

This paper mainly discusses the sparse matrix of the transpose algorithm, because the sparse matrix refers to 30% of the content of the matrix is not 0 elements, this time we do not need to use an array of MXN to store this matrix, which is more waste of storage space, while the implementation of the transpose algorithm overhead is also relatively large, We can use the form of ternary to store the 0 elements in the matrix, and the ternary group refers to three quantities to describe the non 0 elements in the array as line numbers, column numbers, and data. This will save storage space. No more say, first code.

#include <stdio.h> #define MAXSIZE 1000 #define TRUE 1 #define FALSE 0 typedef int ELETYPE;
    typedef struct {int row;
    int col;
Eletype e;

}triple;
    typedef struct {Triple data[maxsize+1];
    int m;
    int n;
int length;

}tsmatrix;
void Inita (Tsmatrix *a);

int Transposetsmatrix (Tsmatrix A, Tsmatrix *b);
    int main () {Tsmatrix A;
    Tsmatrix B;

    int i;
    Inita (&a);  if (Transposetsmatrix (A, &b)) {for (i = 1; I <= a.length; i++) {printf ("B.data[%d].row =
            %d\n ", I, B.data[i].row);
            printf ("B.data[%d].col =%d\n", I, B.data[i].col);
        printf ("B.data[%d].length =%d\n", I, B.DATA[I].E);
    }} void Inita (Tsmatrix *a) {a->m = 6;
    A->n = 7;
    A->length = 8;
    A->data[1].row = 1;
    A->data[1].col = 2;
    A-&GT;DATA[1].E = 12;
    A->data[2].row = 1;
    A->data[2].col = 3;
    A-&GT;DATA[2].E = 9;
    A->data[3].row = 3;A->data[3].col = 1;
    A-&GT;DATA[3].E =-3;
    A->data[4].row = 3;
    A->data[4].col = 6;
    A-&GT;DATA[4].E = 14;
    A->data[5].row = 4;
    A->data[5].col = 3;
    A-&GT;DATA[5].E = 24;
    A->data[6].row = 5;
    A->data[6].col = 2;
    A-&GT;DATA[6].E = 18;
    A->data[7].row = 6;
    A->data[7].col = 1;
    A-&GT;DATA[7].E = 15;
    A->data[8].row = 6;
    A->data[8].col = 4;
A-&GT;DATA[8].E =-7;
    int Transposetsmatrix (Tsmatrix A, Tsmatrix *b) {int I, J;
    int count = 1;
    B->m = A.N;
    B->n = a.m;

    B->length = A.length;

    if (b->length <= 0) return FALSE;
                for (i = 1; I <= A.N. i++) {for (j = 1; J <= A.length; j) {if (A.data[j].col = i) {
                B->data[count].row = i;
                B->data[count].col = A.data[j].row;
                B-&GT;DATA[COUNT].E = A.DATA[J].E;
            count++; }} return TRUE; }
Conclusion: This algorithm is based on the behavior of the main sequence, so the original array as a loop, traversing the entire matrix, select the number of columns to conform to the established numbers, if it fits then put the data into the corresponding transpose matrix. Not to sleep, and tomorrow to go home, the night again with the fast algorithm.

———————————————————————————————————————————— Split Line 2016.6.29

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.