稀疏矩阵的基本操作

###稀疏矩阵的基本操作

假设在 mn m ∗ n 的矩阵中,有 t t 个非零元素,令 δ=tmn δ = t m ∗ n ,称 δ δ 稀疏矩阵因子,当 δ δ <= <= 0.05时,称为稀疏矩阵。
对于稀疏矩阵采用三元组进行存储表示,即:行下标,列下标,非零元。

下面是矩阵的转置和快速转置,有点绕人,建议对照书本来理解代码

操作系统:Mac OS
IDE: Xcode

具体实现代码如下:

#include 
#include 
#include 

#define MAXSIZE 12500
typedef int ElemType;

typedef struct {
    int i;//行下标
    int j;//列下标
    ElemType elem;//非零元
}Triple;

typedef struct {
    Triple* triple;
    int mu,nu,tu;//矩阵行数,列数,非零元数
}Matrix;

typedef Triple* TriplePtr;
typedef Matrix* MatrixPtr;

//初始化矩阵
MatrixPtr initMatrix(int row, int col, int valueCount) {
    MatrixPtr matrix = (MatrixPtr)malloc(sizeof(Matrix));
    matrix -> triple = (TriplePtr)calloc(sizeof(Triple), MAXSIZE);
    matrix -> mu = row;
    matrix -> nu = col;
    matrix -> tu = valueCount;
    return matrix;
}

//输入三元组对矩阵增加非零元
void insertElemToMatrix(MatrixPtr matrix) {
    int row,col,value;
    printf("请输入有效数据(三元组):\n");
    for (int i = 1; i <= matrix -> tu; i++) {
        scanf("%d,%d,%d",&row, &col, &value);
        matrix -> triple[i].i = row;
        matrix -> triple[i].j = col;
        matrix -> triple[i].elem = value;
        getchar();
    }
}

//矩阵的转置
MatrixPtr transposeMatrix(MatrixPtr matrix) {
    int q = 1;
    MatrixPtr Tmatrix = initMatrix(matrix -> nu, matrix -> mu, matrix -> tu);
    for (int row = 1; row <= matrix -> mu; row++) {
        for (int p = 1; p <= matrix -> tu; p++) {
            if (matrix -> triple[p].i == row) {
                Tmatrix -> triple[q].i = matrix -> triple[p].j;
                Tmatrix -> triple[q].j = matrix -> triple[p].i;
                Tmatrix -> triple[q].elem = matrix -> triple[p].elem;
                q++;
            }
        }
    }
    return Tmatrix;
}

//快速转置
MatrixPtr fastTransposeMatrix(MatrixPtr matrix) {
    MatrixPtr Tmatrix = initMatrix(matrix -> nu, matrix -> mu, matrix -> tu);
    int col,q;
    int num[matrix -> nu],cpot[matrix -> nu];
    for (col = 1; col <= matrix -> nu; col++) { num[col] = 0; } //初始化num数组
    for (int t = 1; t <= matrix -> tu; t++) { ++num[matrix -> triple[t].j]; } //求矩阵M中的每一列的非零元数
    cpot[1] = 1;
    for (col = 2; col <= matrix -> nu; col++) { cpot[col] = cpot[col - 1] + num[col - 1]; }
    for (int p = 1; p <= matrix -> tu; p++) {
        col = matrix -> triple[p].j;
        q = cpot[col];
        Tmatrix -> triple[q].i = matrix -> triple[p].j;
        Tmatrix -> triple[q].j = matrix -> triple[p].i;
        Tmatrix -> triple[q].elem = matrix -> triple[p].elem;
        cpot[col]++;
    }
    return Tmatrix;
}

//遍历矩阵
void traverseMatrix(MatrixPtr matrix, void (* visit)(ElemType data)) {
    int index = 1;
    for (int i = 1; i <= matrix -> mu; i++) {
        for (int j = 1; j <= matrix -> nu; j ++) {
            if (matrix -> triple[index].i == i && matrix -> triple[index].j == j) {
                visit(matrix -> triple[index++].elem);
            } else {
                visit(0);
            }
        }
        printf("\n");
    }
}

//打印方法
void printMatrix(ElemType data) {
    printf("%4d",data);
}

int main(int argc, const char * argv[]) {
    MatrixPtr matrix = initMatrix(6, 5, 3);
    insertElemToMatrix(matrix);
    printf("打印矩阵如下:\n");
    traverseMatrix(matrix, printMatrix);

    printf("-------------------------\n");
    MatrixPtr TMatrix = transposeMatrix(matrix);
    printf("转置后矩阵如下:\n");
    traverseMatrix(TMatrix, printMatrix);


    printf("-------------------------\n");
    MatrixPtr fastTMatrix = fastTransposeMatrix(matrix);
    printf("快速转置后矩阵如下:\n");
    traverseMatrix(fastTMatrix, printMatrix);

    return 0;
}

你可能感兴趣的:(稀疏矩阵的基本操作)