Multiplication of sparse matrix

Multiplication of sparse matrix

Topic information

Data compression is a technique to improve transmission and storage efficiency.
This experiment requires the realization of an algorithm for the product of two sparse matrices. The number of non-zero elements in the sparse matrix is ​​less than 100.

enter

The number of rows, columns, and the number of nonzero elements of the first sparse matrix (all three numbers are greater than 0), the
number of rows, columns, and the number of nonzero elements of the second sparse matrix of the triplet (three numbers Are greater than 0), the triples are
entered in the sparse matrix triplet table in the main sequence of rows

Output

The number of
rows and columns of the product matrix The number of
non-zero elements (all three numbers are greater than 0)
triples

Test sample

3
4
4
1 1 3
1 4 5
2 2 -1
3 1 2
4
2
4
1 2 2
2 1 1
3 1 -2
3 2 4
3
2
3
1,2,6
2,1,-1
3,2,4

answer

#include<stdio.h>

typedef struct
{
    
    
    int i, j;
    int e;
} Node;
typedef struct
{
    
    
    Node nodes[105];
    int rpos[105];//各行第一个非零元的位置表
    int m, n, t;
} Matrix;

int main()
{
    
    
    //freopen("/Users/zhj/Downloads/test.txt", "r", stdin);
    Matrix A, B;

    scanf("%d%d%d", &A.m, &A.n, &A.t);
    for (int i = 1; i <= A.t; i++)
    {
    
    
        scanf("%d%d%d", &A.nodes[i].i, &A.nodes[i].j, &A.nodes[i].e);
    }
    int num[1000];
    for (int col = 1; col <= A.m; col++)
    {
    
    
        num[col] = 0;
    }
    for (int i = 1; i <= A.t; i++)
    {
    
    
        num[A.nodes[i].i]++;
    }
    A.rpos[1] = 1;
    for (int col = 2; col <= A.m; col++)
    {
    
    
        A.rpos[col] = A.rpos[col - 1] + num[col - 1];
    }

    scanf("%d%d%d", &B.m, &B.n, &B.t);
    for (int i = 1; i <= B.t; i++)
    {
    
    
        scanf("%d%d%d", &B.nodes[i].i, &B.nodes[i].j, &B.nodes[i].e);
    }
    for (int col = 1; col <= B.m; col++)
    {
    
    
        num[col] = 0;
    }
    for (int i = 1; i <= B.t; i++)
    {
    
    
        num[B.nodes[i].i]++;
    }
    B.rpos[1] = 1;
    for (int col = 2; col <= B.m; col++)
    {
    
    
        B.rpos[col] = B.rpos[col - 1] + num[col - 1];
    }

    Matrix Q;
    Q.m = A.m;
    Q.n = B.n;
    Q.t = 0;//创建答案矩阵

    if (A.t * B.t != 0)
    {
    
    //Q是非零矩阵
        for (int arow = 1; arow <= A.m; arow++)
        {
    
    //处理A的每一行
            int ctemp[105] = {
    
    0};
            Q.rpos[arow] = Q.t + 1;

            int tp;//tp是下一行元素在nodes表中位置
            if (arow < A.m)
            {
    
    
                tp = A.rpos[arow + 1];
            }
            else
            {
    
    
                tp = A.t + 1;
            }
            for (int p = A.rpos[arow]; p < tp; p++)
            {
    
    //对当前行中每一个非零元,既从当前在nodes表中位置找到下一行元素在nodes表的位置
                int brow = A.nodes[p].j;//此为A表中的纵向位置值,在B表中找相应的行号即可

                int t;//t仍然为下一行的位置
                if (brow < B.m)
                {
    
    
                    t = B.rpos[brow + 1];
                }
                else
                {
    
    
                    t = B.t + 1;
                }
                for (int q = B.rpos[brow]; q < t; q++)
                {
    
    
                    int ccol = B.nodes[q].j;//Q中的纵坐标是以B元素中的j来说话的
                    ctemp[ccol] += A.nodes[p].e * B.nodes[q].e;
                }
            }

            for (int ccol = 1; ccol <= Q.n; ccol++)
            {
    
    //压缩存储该行的非零元
                if (ctemp[ccol])
                {
    
    //如果此处有值的话
                    Q.t++;//Q的非零元素多了一个
                    Q.nodes[Q.t].i = arow;//行号为此时遍历的A的行号
                    Q.nodes[Q.t].j = ccol;//列号为此时正在进行压缩所遍历到有值的地方
                    Q.nodes[Q.t].e = ctemp[ccol];//累计的和拷贝过来
                }
            }
        }
    }

    printf("%d\n", Q.m);
    printf("%d\n", Q.n);
    printf("%d\n", Q.t);
    for (int i = 1; i <= Q.t; i++)
    {
    
    
        printf("%d,%d,%d\n", Q.nodes[i].i, Q.nodes[i].j, Q.nodes[i].e);
    }
    return 0;
}

idea

Insert picture description here
For each element in A A.nodes[p](p=1,2,...,A.t), find all satisfy the condition N A.nodes[p].j = B.nodes.ielements B.nodes[q], is obtained A.nodes[p].v, and B.nodes[q].vthe product.
Value of each element of the product matrix Q is accumulated and this product A.nodes[p].v * B.nodes[q], v is only Q[i][j]a part of. For ease of operation, each coping Set a cumulative sum variable for each element, and its initial value is zero, then sweep the array A, find the product of the corresponding element and add it to the appropriate cumulative sum variable

Guess you like

Origin blog.csdn.net/zhj12399/article/details/109332227