Compressed Row Sparse Matrix (CSR) Format Conversion
Decomposition & Spectral Methods DS practice problem on Onlearn.
Difficulty: easy.
Topics: Understanding Implement Compressed Row Sparse Matrix (CSR) Format Conversion, CSR Format Specification, Dense Matrix Iteration, Non-zero Value Extraction, Row Pointer Accumulation, Column Index Storage, Memory Footprint Reduction, Linear Algebra, Data Structures and Algorithms, Computational Efficiency, Numerical Computing, Applied Mathematics, Sparse Matrix Representations, Memory Optimization Techniques, Matrix Data Handling, Algorithm Design Patterns, Data Transformation & Encoding.
Task: Convert a Dense Matrix to Compressed Row Sparse (CSR) Format Your task is to implement a function that converts a given dense matrix into the Compressed Row Sparse (CSR) format, an efficient storage representation for sparse matrices. The CSR format only stores non zero elements and their positions, significantly reducing memory usage for matrices with a large number of zeros. Write a function compressed row sparse matrix(dense matrix) that takes a 2D list dense matrix as input and returns a tuple containing three lists: Values array : List of all non zero elements in row major order. Column indices array : Column index for each non zero element in the values array. Row pointer array : Cumulative number of non zero elements per row, indicating the start of each row in the values array.