Data Structures
Matrix Representation
Time Complexity
Space Complexity
Sparse Matrix

Efficient time and space complexity data structure for dense and sparse matrix

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

Matrix representation and manipulation are fundamental to various computational tasks, ranging from scientific computing to machine learning. The choice of data structures for storing and computing with matrices can significantly affect the efficiency of these tasks, both in terms of time and space. This article dives into efficient data structures for representing dense and sparse matrices, discussing their time and space complexities and providing examples where relevant.

Dense Matrices

Dense matrices are those with most of their elements being non-zero. A naive representation of dense matrices uses a 2D array or a similar structure.

Data Structures for Dense Matrices

  1. 2D Arrays:
    • Description: A straightforward representation of dense matrices is a 2D array, where each element corresponds to an element in the matrix.
    • Time Complexity: Accessing and modifying an element in a dense matrix is O(1)O(1), as it involves direct indexing.
    • Space Complexity: It requires O(m×n)O(m \times n) space for an m×nm \times n matrix, since all elements (including zeros) are stored.

Example

Consider a 3x3 matrix for simplicity:

  • Description: This format stores non-zero elements and their indices.
  • Time Complexity: Access can range from O(1)O(1) to O(n)O(n) depending on the operation, indexing is generally more complex than dense storage.
  • Space Complexity: Only non-zero elements are stored, alongside row and column indices, often leading to significantly reduced space.
  • Description: Similar to CSR but focused on column-wise storage.
  • Time Complexity: Access complexity similar to CSR but optimized for column operations.
  • Space Complexity: Efficient as it stores non-zero values, column pointers, and row indices.
  • Description: Stores entries in a dictionary format with coordinate pairs as keys.
  • Time Complexity: Allows fast insertion but is less efficient for arithmetic operations.
  • Space Complexity: Highly space efficient, storing only non-zero elements.
  • `Values`: [3, 1, 4, 5]
  • `Column Indices`: [0, 2, 1, 2]
  • `Row Pointers`: [0, 2, 3, 4]

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms