【数据结构】数组和字符串(六):特殊矩阵的压缩存储:稀疏矩阵——压缩稀疏列(Compressed Sparse Column,CSC)

本文主要是介绍【数据结构】数组和字符串(六):特殊矩阵的压缩存储:稀疏矩阵——压缩稀疏列(Compressed Sparse Column,CSC),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 4.2.1 矩阵的数组表示
  • 4.2.2 特殊矩阵的压缩存储
    • a. 对角矩阵的压缩存储
    • b~c. 三角、对称矩阵的压缩存储
    • d. 稀疏矩阵的压缩存储——三元组表
    • e. 压缩稀疏行(Compressed Sparse Row,CSR)矩阵
    • f. 压缩稀疏列(Compressed Sparse Column,CSC)矩阵
      • 结构体
      • 创建CSC矩阵
      • 元素设置
      • 初始化
      • 打印矩阵
      • 销毁CSC矩阵
      • 主函数
      • 代码整合

4.2.1 矩阵的数组表示

【数据结构】数组和字符串(一):矩阵的数组表示

4.2.2 特殊矩阵的压缩存储

  矩阵是以按行优先次序将所有矩阵元素存放在一个一维数组中。但是对于特殊矩阵,如对称矩阵、三角矩阵、对角矩阵和稀疏矩阵等, 如果用这种方式存储,会出现大量存储空间存放重复信息或零元素的情况,这样会造成很大的空间浪费。为节约存储空间和算法(程序)运行时间,通常会采用压缩存储的方法。

  • 对角矩阵:指除了主对角线以外的元素都为零的矩阵,即对 任意 i ≠ j (1≤ i , j ≤n),都有M(i, j)=0。由于只有主对角线上有非零元素,只需存储主对角线上的元素即可。
  • 三角矩阵:指上三角或下三角的元素都为零的矩阵。同样地,只需存储其中一部分非零元素,可以节省存储空间。
  • 对称矩阵:指矩阵中的元素关于主对角线对称的矩阵。由于对称矩阵的非零元素有一定的规律,可以只存储其中一部分元素,从而减少存储空间。
  • 稀疏矩阵:指大部分元素为零的矩阵。传统的按行优先次序存储方法会浪费大量空间来存储零元素,因此采用压缩存储的方法更为合适。常见的压缩存储方法有:压缩稠密行(CSR)、压缩稠密列(CSC)、坐标列表(COO)等。

a. 对角矩阵的压缩存储

【数据结构】数组和字符串(二):特殊矩阵的压缩存储:对角矩阵——一维数组

b~c. 三角、对称矩阵的压缩存储

【数据结构】数组和字符串(三):特殊矩阵的压缩存储:三角矩阵、对称矩阵——一维数组

d. 稀疏矩阵的压缩存储——三元组表

【数据结构】数组和字符串(四):特殊矩阵的压缩存储:稀疏矩阵——三元组表

e. 压缩稀疏行(Compressed Sparse Row,CSR)矩阵

【数据结构】数组和字符串(五):特殊矩阵的压缩存储:稀疏矩阵——压缩稀疏行(CSR)

f. 压缩稀疏列(Compressed Sparse Column,CSC)矩阵

  压缩稀疏列(Compressed Sparse Column,CSC)以列为主要组织方式,将矩阵按列进行存储。它包含三个主要数组:

  1. 列指针数组(Column Pointer Array):该数组的长度为矩阵的列数加一(cols+1),每个元素存储对应列中第一个非零元素在元素数组中的索引位置。最后一个元素存储非零元素的总数以及元素数组的长度。

  2. 行索引数组(Row Index Array):该数组的长度等于非零元素的数量,每个元素存储对应非零元素所在的行索引。

  3. 元素数组(Element Array):该数组的长度等于非零元素的数量,每个元素存储非零元素的值以及它所在的列索引。

  通过这种方式,CSC格式将稀疏矩阵的非零元素按列进行存储,并通过列指针数组和行索引数组提供了对非零元素在矩阵中位置的快速访问。

结构体

typedef struct {int row;int col;int value;
} Element;typedef struct {int rows;int cols;int num_elements;Element* elements;int* col_ptr;int* row_indices;
} CSCMatrix;
  • Element 结构体表示矩阵中的一个元素,包含三个成员变量:row(行索引)、col(列索引)和 value(元素值)。

  • CSCMatrix 结构体表示一个CSC矩阵,包含了矩阵的行数 rows、列数 cols、非零元素的个数 num_elements,以及三个指针成员变量 elementscol_ptrrow_indices

创建CSC矩阵

CSCMatrix createCSCMatrix(int rows, int cols, int num_elements) {CSCMatrix matrix;matrix.rows = rows;matrix.cols = cols;matrix.num_elements = num_elements;matrix.elements = (Element*)malloc(num_elements * sizeof(Element));matrix.col_ptr = (int*)malloc((cols + 1) * sizeof(int));matrix.row_indices = (int*)malloc(num_elements * sizeof(int));memset(matrix.col_ptr, 0, (cols + 1) * sizeof(int));return matrix;
}
  • createCSCMatrix 函数用于创建一个CSC矩阵。
  • 接受矩阵的行数、列数和非零元素的个数作为参数,并返回创建的CSC矩阵。
  • 在函数内部,通过动态内存分配分别为 elementscol_ptrrow_indices 分配内存空间,并将 col_ptr 数组的所有元素初始化为0,最后返回创建的矩阵。

元素设置

void setElement(CSCMatrix* matrix, int row, int col, int value) {if (col < 0 || col >= matrix->cols) {printf("Invalid column index.\n");return;}int index = matrix->col_ptr[col];matrix->elements[index].row = row;matrix->elements[index].col = col;matrix->elements[index].value = value;matrix->row_indices[index] = row;matrix->col_ptr[col]++;  // 递增索引值
}

  setElement 函数可用于设置(修改)CSC矩阵中某个位置的元素值。

  • 接受一个指向CSC矩阵的指针 matrix,以及要设置的元素的行索引、列索引和值作为参数。
  • 在函数内部,首先检查列索引是否有效,如果无效则打印错误信息并返回。
  • 然后,根据列索引找到对应列的起始位置,将元素的行索引、列索引和值分别赋给对应的矩阵元素,并更新 row_indices 数组和 col_ptr 数组中的值。

初始化


void initializeCSCMatrix(CSCMatrix* matrix, int* values, int* row_indices, int* col_indices, int num_elements) {for (int i = 0; i < num_elements; i++) {matrix->elements[i].value = values[i];matrix->elements[i].row = row_indices[i];matrix->elements[i].col = col_indices[i];matrix->row_indices[i] = row_indices[i];matrix->col_ptr[col_indices[i]]++;}int sum = 0;for (int i = 0; i <= matrix->cols; i++) {int temp = matrix->col_ptr[i];matrix->col_ptr[i] = sum;sum += temp;}
}

  initializeCSCMatrix 函数用于初始化CSC矩阵的数据。

  • 接受一个指向CSC矩阵的指针 matrix,以及包含非零元素的值、行索引和列索引的数组,以及非零元素的个数作为参数。
  • 通过遍历非零元素数组,将值、行索引和列索引分别赋给对应的矩阵元素,并更新 row_indices 数组和 col_ptr 数组中的值。col_ptr 数组的每个元素表示对应列的非零元素在 elements 数组中的起始位置,通过累加非零元素的个数来计算每列的结束位置。

打印矩阵

void printCSCMatrix(CSCMatrix matrix) {printf("CSC Matrix:\n");printf("Rows: %d, Cols: %d, Num Elements: %d\n", matrix.rows, matrix.cols, matrix.num_elements);printf("Values: ");for (int i = 0; i < matrix.num_elements; i++) {printf("%d ", matrix.elements[i].value);}printf("\n");printf("Column Pointer: ");for (int i = 0; i <= matrix.cols; i++) {printf("%d ", matrix.col_ptr[i]);}printf("\n");printf("Row Indices: ");for (int i = 0; i < matrix.num_elements; i++) {printf("%d ", matrix.row_indices[i]);}printf("\n");
}void printMatrixForm(CSCMatrix matrix) {printf("Matrix Form:\n");for (int i = 0; i < matrix.rows; i++) {for (int j = 0; j < matrix.cols; j++) {int value = 0;for (int k = matrix.col_ptr[j]; k < matrix.col_ptr[j + 1]; k++) {if (matrix.elements[k].row == i) {value = matrix.elements[k].value;break;}}printf("%d ", value);}printf("\n");}
}
  • printCSCMatrix 函数用于打印CSC矩阵的信息:它接受一个CSC矩阵作为参数,并打印矩阵的行数、列数、非零元素的个数以及 elementscol_ptrrow_indices 数组的内容。
  • printMatrixForm 函数用于以矩阵形式打印CSC矩阵。它接受一个CSC矩阵作为参数,并按矩阵的行数和列数遍历矩阵元素,通过遍历 col_ptr 数组和 row_indices 数组来获取每个位置的元素值,并打印出矩阵的形式。

销毁CSC矩阵

void destroyCSCMatrix(CSCMatrix* matrix) {free(matrix->elements);free(matrix->col_ptr);free(matrix->row_indices);matrix->elements = NULL;matrix->col_ptr = NULL;matrix->row_indices = NULL;
}

主函数


int main() {int rows = 3;int cols = 9;int num_elements = 5;CSCMatrix matrix = createCSCMatrix(rows, cols, num_elements);int row_indices[] = {0, 0, 0, 0, 1};int col_indices[] = {3, 5, 7, 8, 7};int values[] =      {2, 1, 3, 1, 4};initializeCSCMatrix(&matrix, values, row_indices, col_indices, num_elements);printCSCMatrix(matrix);printMatrixForm(matrix);destroyCSCMatrix(&matrix);return 0;
}

在这里插入图片描述

代码整合

#include <stdio.h>
#include <stdlib.h>
#include <string.h>typedef struct {int row;int col;int value;
} Element;typedef struct {int rows;int cols;int num_elements;Element* elements;int* col_ptr;int* row_indices;
} CSCMatrix;CSCMatrix createCSCMatrix(int rows, int cols, int num_elements) {CSCMatrix matrix;matrix.rows = rows;matrix.cols = cols;matrix.num_elements = num_elements;matrix.elements = (Element*)malloc(num_elements * sizeof(Element));matrix.col_ptr = (int*)malloc((cols + 1) * sizeof(int));matrix.row_indices = (int*)malloc(num_elements * sizeof(int));memset(matrix.col_ptr, 0, (cols + 1) * sizeof(int));return matrix;
}void setElement(CSCMatrix* matrix, int row, int col, int value) {if (col < 0 || col >= matrix->cols) {printf("Invalid column index.\n");return;}int index = matrix->col_ptr[col];matrix->elements[index].row = row;matrix->elements[index].col = col;matrix->elements[index].value = value;matrix->row_indices[index] = row;matrix->col_ptr[col]++;  // 递增索引值
}void printCSCMatrix(CSCMatrix matrix) {printf("CSC Matrix:\n");printf("Rows: %d, Cols: %d, Num Elements: %d\n", matrix.rows, matrix.cols, matrix.num_elements);printf("Values: ");for (int i = 0; i < matrix.num_elements; i++) {printf("%d ", matrix.elements[i].value);}printf("\n");printf("Column Pointer: ");for (int i = 0; i <= matrix.cols; i++) {printf("%d ", matrix.col_ptr[i]);}printf("\n");printf("Row Indices: ");for (int i = 0; i < matrix.num_elements; i++) {printf("%d ", matrix.row_indices[i]);}printf("\n");
}void printMatrixForm(CSCMatrix matrix) {printf("Matrix Form:\n");for (int i = 0; i < matrix.rows; i++) {for (int j = 0; j < matrix.cols; j++) {int value = 0;for (int k = matrix.col_ptr[j]; k < matrix.col_ptr[j + 1]; k++) {if (matrix.elements[k].row == i) {value = matrix.elements[k].value;break;}}printf("%d ", value);}printf("\n");}
}void destroyCSCMatrix(CSCMatrix* matrix) {free(matrix->elements);free(matrix->col_ptr);free(matrix->row_indices);matrix->elements = NULL;matrix->col_ptr = NULL;matrix->row_indices = NULL;
}void initializeCSCMatrix(CSCMatrix* matrix, int* values, int* row_indices, int* col_indices, int num_elements) {for (int i = 0; i < num_elements; i++) {matrix->elements[i].value = values[i];matrix->elements[i].row = row_indices[i];matrix->elements[i].col = col_indices[i];matrix->row_indices[i] = row_indices[i];matrix->col_ptr[col_indices[i]]++;}int sum = 0;for (int i = 0; i <= matrix->cols; i++) {int temp = matrix->col_ptr[i];matrix->col_ptr[i] = sum;sum += temp;}
}int main() {int rows = 3;int cols = 9;int num_elements = 5;CSCMatrix matrix = createCSCMatrix(rows, cols, num_elements);int row_indices[] = {0, 0, 0, 0, 1};int col_indices[] = {3, 5, 7, 8, 7};int values[] =      {2, 1, 3, 1, 4};initializeCSCMatrix(&matrix, values, row_indices, col_indices, num_elements);printCSCMatrix(matrix);printMatrixForm(matrix);destroyCSCMatrix(&matrix);return 0;
}

这篇关于【数据结构】数组和字符串(六):特殊矩阵的压缩存储:稀疏矩阵——压缩稀疏列(Compressed Sparse Column,CSC)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/289580

相关文章

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

Java使用正则提取字符串中的内容的详细步骤

《Java使用正则提取字符串中的内容的详细步骤》:本文主要介绍Java中使用正则表达式提取字符串内容的方法,通过Pattern和Matcher类实现,涵盖编译正则、查找匹配、分组捕获、数字与邮箱提... 目录1. 基础流程2. 关键方法说明3. 常见场景示例场景1:提取所有数字场景2:提取邮箱地址4. 高级

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I

redis数据结构之String详解

《redis数据结构之String详解》Redis以String为基础类型,因C字符串效率低、非二进制安全等问题,采用SDS动态字符串实现高效存储,通过RedisObject封装,支持多种编码方式(如... 目录一、为什么Redis选String作为基础类型?二、SDS底层数据结构三、RedisObject

Python 字符串裁切与提取全面且实用的解决方案

《Python字符串裁切与提取全面且实用的解决方案》本文梳理了Python字符串处理方法,涵盖基础切片、split/partition分割、正则匹配及结构化数据解析(如BeautifulSoup、j... 目录python 字符串裁切与提取的完整指南 基础切片方法1. 使用切片操作符[start:end]2

MyBatis的xml中字符串类型判空与非字符串类型判空处理方式(最新整理)

《MyBatis的xml中字符串类型判空与非字符串类型判空处理方式(最新整理)》本文给大家介绍MyBatis的xml中字符串类型判空与非字符串类型判空处理方式,本文给大家介绍的非常详细,对大家的学习或... 目录完整 Hutool 写法版本对比优化为什么status变成Long?为什么 price 没事?怎

Java中数组与栈和堆之间的关系说明

《Java中数组与栈和堆之间的关系说明》文章讲解了Java数组的初始化方式、内存存储机制、引用传递特性及遍历、排序、拷贝技巧,强调引用数据类型方法调用时形参可能修改实参,但需注意引用指向单一对象的特性... 目录Java中数组与栈和堆的关系遍历数组接下来是一些编程小技巧总结Java中数组与栈和堆的关系关于

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的