Sparse Matrix Fast Transpose

Source: Internet
Author: User
Tags mongodb postgresql

Topic Description

Sparse matrix storage is not suitable for storing each element in a two-dimensional array, which would waste a lot of storage space. So you can use a one-dimensional array to store the non 0 elements. The element type of this one-dimensional array is a ternary group, consisting of the position of a non 0 element in the sparse matrix (row and column number pairs) and the value of the tuple. The matrix transpose is to swap the elements of the matrix row and column. In reference to the practice in Algorithm 5.1, mu and nu respectively represent the number of rows and columns of sparse matrices, and it is not difficult to find their time complexity O (muxnu). When the number of non 0 Yuan Tu and muxnu same order of magnitude, the algorithm 5.1 time complexity will rise to O (Muxnu 2). Therefore, a fast sparse matrix transpose algorithm is needed. Now you can implement a fast algorithm to transpose sparse matrices. The following is a description of the algorithm for fast-transpose sparse matrices:

input



The first line entered is two integers r and C (r<200, c<200, R*c <= 12500), representing a number of rows and columns of sparse matrices containing many 0. Next there are r rows, each with a C integer, separated by a space, representing the elements of the sparse matrix.



Output



The output is the transpose matrix of the sparse matrix read. Output has a C row, each row has r integer, after each integer output a space. Note that line-end output wraps.



Sample Input


6 7
0 12 9 0 0 0 0
0 0 0 0 0 0 0
-3 0 0 0 0 14 0
0 0 24 0 0 0 0
0 18 0 0 0 0 0
15 0 0 -7 0 0 0


Sample Output


 
0 0 -3 0 0 15 
12 0 0 0 18 0 
9 0 0 24 0 0 
0 0 0 0 0 -7 
0 0 0 0 0 0 
0 0 14 0 0 0 
0 0 0 0 0 0 
 


Tips




Tip: This algorithm uses only two auxiliary vectors than algorithm 5.1. For the time complexity of this algorithm, it is not difficult to find that there are 4 parallel single loops in the algorithm, and the cyclic times are Nu and tu respectively, so the total time complexity is O (nu+tu). However, when the number of non 0 elements of the sparse matrix is the same as that of the muxnu, the time complexity is O (MUXNU) and the time complexity of the classical algorithm is the same. Please be aware of why the transpose algorithm is used to transpose the columns from small to large. In fact, only one loop can complete the transpose without having to deal with the columns from small to large, after the transpose of the matrix, although the content is correct, but the order of the elements has changed, so that in subsequent various processing operations will increase the complexity. In this case, if you do not follow the column from small to large order processing will lead to output difficulties, greatly increasing the complexity of the output. Summary: Sparse matrix is a very important part of matrix application, because of its sparse and special properties, we can get a special algorithm faster than the traditional matrix algorithm. This will also be reflected in the topics later in this chapter.





#include<stdio.h>
#define MAXSIZE 12500
int num[MAXSIZE+1],cpot[MAXSIZE+1];
typedef struct
{
    int i,j;//The row number and column number of non-zero elements
    int e;//The value of non-zero elements
}Triple;
typedef struct
{
    Triple data[MAXSIZE+1];//Non-zero element triples, data[0] is not used
    int mu,nu,tu;//The number of rows, columns, and non-zero elements of the matrix
}TSMatrix;

TSMatrix TransposeSMatrix(TSMatrix M, TSMatrix T);
TSMatrix CreatSMatrix(TSMatrix M);

TSMatrix CreatSMatrix(TSMatrix M)
{
    int p,q,k,a;
    M.tu=0,k=1;
    for( p=1;p<=M.mu;p++)
        for( q=1;q<=M.nu;q++)
        {
            scanf("%d",&a);
            if(a!=0)
            {
                M.data[k].i=p;
                M.data[k].j=q;
                M.data[k].e=a;
                k++;
                M.tu++;
            }
         }
         return M;
}
TSMatrix TransposeSMatrix(TSMatrix M, TSMatrix T)
{
    int col,t,p,q;
    T.mu=M.nu; T.nu=M.mu; T.tu=M.tu;
    if(T.tu)
    {
        for(col=1;col<=M.nu;++col) num[col]=0;
        for(t=1;t<=M.tu;t++) ++num[M.data[t].j];
        cpot[1]=1;
        for(col=2;col<=M.tu;col++) cpot[col]=cpot[col-1]+num[col-1];
        for(p=1;p<=M.tu;p++)
        {
            col=M.data[p].j;
            q=cpot[col];
            T.data[q].i=M.data[p].j;
            T.data[q].j=M.data[p].i;
            T.data[q].e=M.data[p].e;
            ++cpot[col];
        }

    }
    return T;
}
int main()
{
    int k=1,p,q;
    TSMatrix M,T;
    scanf("%d%d",&M.mu,&M.nu);
    M=CreatSMatrix(M);
    T=TransposeSMatrix(M,T);
    for(p=1;p<=T.mu;p++)
    {
        for(q=1;q<=T.nu;q++)
        {
           if(T.data[k].i==p&&T.data[k].j==q)
           {
                printf("%d ",T.data[k].e);
                k++;
           }
           else
                printf("0 ");
        }
        printf("\n");
    }
    return 0;
} 



Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

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.