Graph
Compressed Sparse Row

📐Compressed Sparse Row

NEW

Also known as: CSR, Compressed Row Storage

A sparse matrix storage format that stores nonzero elements grouped by row using value, column index, and row pointer arrays.

DESCRIPTION

Compressed Sparse Row (CSR) stores sparse matrices by compressing rows. It uses three arrays: data for nonzero values, column indices for each value, and a row pointer array marking where each row begins. CSR supports efficient row slicing and is often optimal for sparse matrix–vector multiplication. It’s ubiquitous in graph analytics, PDE solvers, and ML pipelines. Libraries like SciPy, cuSPARSE, and SuiteSparse provide high-performance CSR operations. Choosing CSR vs CSC depends on algorithmic access patterns. Conversion functions enable interoperability with other formats.

ALTERNATIVE DEFINITIONS

SciPy

A csr_matrix is a sparse matrix in Compressed Sparse Row format.

Concise library definition naming the format.

Source

Wikipedia

In compressed sparse row (CSR) format, a matrix is represented by values, column indices, and row pointer arrays that store nonzero entries row by row.

Explicitly notes the three arrays and row-wise storage.

Source

Sparse Matrix COO Format SciPy NumPy Matrix-Vector Multiplication Graph Adjacency Matrix Linear Algebra Block Sparse SpMV

COMPETING TERMS

Compressed Sparse Column

KNOWLEDGE AREAS

Mathematics Data Science AI Platform & Tooling

TARGET AUDIENCE

Data Scientist AI Engineer MLOps Engineer Software Developer

EXTERNAL SOURCES

TOP QUESTIONS

What is Compressed Sparse Row (CSR) format?

CSR stores nonzeros row by row using values, column indices, and row pointers. It’s efficient for row access and many SpMV operations.

Category: Concepts

When should I prefer CSR over CSC?

Choose CSR when you need row slicing or standard SpMV patterns. CSC may be better for column-oriented factorization tasks.

Category: Advice

How are the CSR arrays structured?

data holds nonzero values, indices hold column indices, and indptr marks the start of each row in data/indices arrays.

Category: Concepts

Is CSR good for dynamic assembly?

Not typically. Use coordinate (COO) format to assemble and then convert to CSR for computation.

Category: Advice

What are common mistakes with CSR?

Leaving duplicate entries, unsorted indices, or inconsistent pointers. Clean and sort indices to ensure performance.

Category: Mistakes

How does CSR impact memory footprint?

It reduces storage by keeping only nonzeros and compact indices, yielding large savings for very sparse matrices.

Category: Benefits

Can CSR be used on GPUs?

Yes, libraries like cuSPARSE and cuBLAS support CSR operations with high throughput.

Category: Tools

How do I convert CSR to dense?

Libraries provide toarray or todense methods that expand nonzeros to a full matrix, though at significant memory cost.

Category: Tools

What algorithms favor CSR?

Iterative solvers (CG, GMRES), graph traversals, and ML preprocessing often favor CSR.

Category: Examples