稀疏矩阵乘法


title: 稀疏矩阵乘法
date: 2020-11-09 19:31:44
tags: 稀疏矩阵运算
categories: 数据结构


在本算法中,两个稀疏矩阵的特性都有用到

规定

规定以下变量名称,本文讲述 矩阵A × 矩阵B = 矩阵C 的运算过程

需要用到的存储结构有:

  1. 矩阵A,矩阵 B 的原始二维数组(2个)
  2. 矩阵A,矩阵B 的三元组数组(2个)
  3. 存储 矩阵A,矩阵B 每行有多少个非零元的数组(2个,分别存A、B矩阵)
  4. 存储 矩阵B每行首个非零元在三元组数组中的位置的数组(1个)
  5. 需要开一个中间数组用于存矩阵C的每一行 (这个中间数组的长度等于B的列数,它只负责在一次循环中记录矩阵C的原始的一行,注意不是稀疏矩阵表示的一样,而是原始矩阵C的一行)
  6. 矩阵C 的三元组数组,将稀疏矩阵还原以后的 C的二维数组

综上,本算法需要 5 种,9 个数组

思路

首先,将上述1 - 4 中的 7 个数组均求出来

一切准备妥当后,且听我慢慢道来

遍历A的三元组数组, 将位于同一行的每个非零元分别与 该非零元的列数 对应到B中的行数,对B中行数是 非零元列数的每个非零元依次求乘法运算并存到中间数组的对应下标位置处,当遍历完A的一行的所有非零元之后,也就求出了C的一行(存在中间数组中,这个中间数组存的不是稀疏的,而是原始矩阵C的一行),然后再对C进行压缩处理,因为在这个过程中,可能原来不是非零元,在计算完之后成了非零元,压缩完之后存到C的三元组数组中

代码

此代码仅仅是将每次内层for循环得到的temp数组(即C矩阵的每一行)打印了出来,还没有做对C矩阵的录入工作,和前面所说稍有出入,还缺个尾巴,但核心算法问题已经解决。

#include <stdio.h>
#include <stdlib.h>
/*
    矩阵 a    4 × 7   8个非零元
    0 8 0 0 6 0 0 
    0 1 0 3 0 0 0 
    7 0 0 2 0 4 0 
    0 0 8 0 0 0 5

    矩阵 b    7 × 3   6个非零元
    1  0  0
    0  0  2
    0  0  0
    0  9  0 
    3  0  0
    0  0  6
    0  4  0
*/

// 定义非零元的结构体(三元组的基本结构)
typedef struct member
{
    // 矩阵中非零元的 行,列,值
    int row, col, x;
    
} Member;

// 定义稀疏矩阵
typedef struct matrix
{
    // 三元组数组
    Member *arr;
    // 矩阵的行数、列数、非零元个数
    int rows, cols, counts;
    
} Matrix;

// 初始化矩阵A的三元组数组
void InitMatrixA(Matrix *);
// 初始化矩阵B的三元组数组
void InitMatrixB(Matrix *);
// 初始化一个三元组数组(在本算法中用于初始化矩阵C的压缩矩阵表示)
void InitMatrix(Matrix *);
// 以原始形式打印矩阵
void PrintMatrix(Matrix);

// 求存储了矩阵每一行的第一个非零元在三元组数组中的下标是多少的数组
int *getIndexArr(Matrix);
int *getCountArr(Matrix);
// 求每一行有多少个非零元
int *getNotZeroSuchRow(Matrix);
// 矩阵乘法
void Multiply(Matrix, Matrix, int *, int *, int *, int *);

int main(void)
{
    Matrix a, b;
    InitMatrixA(&a);
    int *arrA1 = getIndexArr(a);
    int *arrA2 = getNotZeroSuchRow(a);
    InitMatrixB(&b);
    int *arrB1 = getIndexArr(b);
    int *arrB2 = getNotZeroSuchRow(b);
    Multiply(a, b, arrA1, arrB1, arrA2, arrB2);
    return 0;
}

void InitMatrixA(Matrix *pMatrix)
{
    pMatrix->cols = 7;
    pMatrix->rows = 4;
    pMatrix->counts = 9;
    Member *p = (Member *)malloc(sizeof(Member) * pMatrix->counts + 1);
    p[1].row = 1; p[1].col = 2; p[1].x = 8;
    p[2].row = 1; p[2].col = 5; p[2].x = 6;
    p[3].row = 2; p[3].col = 2; p[3].x = 1;
    p[4].row = 2; p[4].col = 4; p[4].x = 3;
    p[5].row = 3; p[5].col = 1; p[5].x = 7;
    p[6].row = 3; p[6].col = 4; p[6].x = 2;
    p[7].row = 3; p[7].col = 6; p[7].x = 4;
    p[8].row = 4; p[8].col = 3; p[8].x = 8;
    p[9].row = 4; p[9].col = 7; p[9].x = 5;
    pMatrix->arr = p;
    printf("矩阵A:\n");
    PrintMatrix(*pMatrix);
}

void InitMatrixB(Matrix *pMatrix)
{
    pMatrix->rows = 7;
    pMatrix->cols = 3;
    pMatrix->counts = 6;
    Member *p = (Member *)malloc(sizeof(Member) * pMatrix->counts + 1);
    p[1].row = 1; p[1].col = 1; p[1].x = 1;
    p[2].row = 2; p[2].col = 3; p[2].x = 2;
    p[3].row = 4; p[3].col = 2; p[3].x = 9;
    p[4].row = 5; p[4].col = 1; p[4].x = 3;
    p[5].row = 6; p[5].col = 3; p[5].x = 6;
    p[6].row = 7; p[6].col = 2; p[6].x = 4;
    pMatrix->arr = p;
    printf("矩阵B:\n");
    PrintMatrix(*pMatrix);
}

void PrintMatrix(Matrix matrix)
{
    int index = 1;
    for (int i = 1; i <= matrix.rows; i++)
    {
        for (int j = 1; j <= matrix.cols; j++)
        {
            if (i == matrix.arr[index].row && j == matrix.arr[index].col)
                printf("%d ", matrix.arr[index++].x);
            else
                printf("0 ");
        }
        printf("\n");
    }
}

int *getIndexArr(Matrix matrix)
{
    // 要返回的目的数组
    int *p = (int *)malloc(sizeof(int) * matrix.rows);
    // 求每一行有多少个非0元的数组
    int arr[100] = {0};
    for (int i = 1; i <= matrix.counts; i++)
        arr[matrix.arr[i].row]++;
    p[1] = 1;
    for (int i = 2; i <= matrix.rows; i++)
        p[i] = p[i - 1] + arr[i - 1];
    return p;
}
int *getNotZeroSuchRow(Matrix matrix)
{
    int *p = (int *)malloc(sizeof(int) * (matrix.rows + 1));
    // 将每个数组元素初始化为0
    for (int i = 0; i < matrix.rows + 1; i++)
        p[i] = 0;
    for (int i = 1; i <= matrix.counts; i++)
        p[matrix.arr[i].row]++;
    return p;
}
void Multiply(Matrix a, Matrix b, int *arr1, int *brr1, int *arr2, int *brr2)
{
    int *temp = (int *)malloc(sizeof(int) * (b.cols + 1));
    for (int i = 0; i < b.cols + 1; i++)
        temp[i] = 0;
    int index = 1;
    printf("矩阵 C = A × B:\n");
    // arr和brr分别存储了矩阵A矩阵B每行首个非零元在三元组数组中的下标
    for (int i = 1; i <= a.rows; i++)
    {
        for (int j = 1; j <= arr2[i]; j++)
        {
            // a的三元组数组中每个元素的列数
            Member aMember = a.arr[index];
            int colOfSuchMemberA = aMember.col;
            // 去b矩阵的brr数组中找到行为上面得到的列数的第一个非零元的下标
            int indexOfNotZeroOfRowOfB = brr1[colOfSuchMemberA];
            for (int i = 1; i <= brr2[colOfSuchMemberA]; i++)
            {
                Member bMember = b.arr[indexOfNotZeroOfRowOfB + i - 1];
                temp[bMember.col] += bMember.x * aMember.x;
            }
            index++;
        }
        // 内层循环一次,代表C矩阵的一行已经被计算出来
        for (int i = 1; i <= b.cols; i++)
        {
            printf("%d ", temp[i]);
            temp[i] = 0;
        }
        printf("\n");
    }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值