Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Introduction to Matrix
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Introduction to Matrices

A matrix is a two-dimensional data structure used for storing elements in a grid-like format with rows and columns. Matrices are commonly used in graph problems, dynamic programming, image processing, pathfinding algorithms, and game development. They provide an efficient way to represent and manipulate structured data.

Why Matrices Are Important in DSA

  1. Efficient Representation: Matrices are used to store graphs, dynamic programming states, and tabular data efficiently.
  2. Fast Element Access: Since matrices use row and column indices, accessing an element is an O(1) operation.
  3. Useful for Many Algorithms: Many algorithms, such as flood fill, shortest path (BFS/DFS), and adjacency matrices, use matrices as their core structure.
  4. Memory Efficiency: Matrices use contiguous memory in most programming languages, improving cache performance and reducing access time.

A matrix of size m × n has m rows and n columns. It is usually represented as:

A =  ⎡ 1  2  3 ⎤  
     ⎢ 4  5  6 ⎥  
     ⎣ 7  8  9 ⎦  

Memory Representation of a Matrix

Matrices are stored in memory as arrays of arrays (row-major order) or as a single contiguous block (column-major order). The way elements are stored affects how efficiently they can be accessed and modified.

Row-Major vs. Column-Major Storage

  1. Row-Major Order (Used in Python, C, C++, Java)

    • The elements are stored row by row in memory.
    • Example storage for a 3 × 3 matrix:
      [1, 2, 3, 4, 5, 6, 7, 8, 9]
      
  2. Column-Major Order (Used in languages like Fortran and MATLAB)

    • The elements are stored column by column in memory.
    • Example storage:
      [1, 4, 7, 2, 5, 8, 3, 6, 9]
      

Accessing Elements in a Matrix

Each element in a matrix is accessed using two indices:

  • First Index (i) → Represents the row number (0-based index).
  • Second Index (j) → Represents the column number (0-based index).

For example, in the following 3 × 3 matrix:

Image
  • A[0][0] = 1 (First row, first column)
  • A[1][2] = 6 (Second row, third column)
  • A[2][1] = 8 (Third row, second column)

Time Complexity of Accessing an Element

  • Since matrices are stored in contiguous memory, retrieving an element using indices directly points to its memory location.
  • Time Complexity: O(1)
  • Space Complexity: O(1) (No extra space is required)

Defining Matrices in Different Programming Languages

The way matrices are declared and initialized varies between programming languages. Below are implementations in commonly used languages.

1. Python

Python represents matrices using lists of lists.

A = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]

Alternative using NumPy:

import numpy as np A = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ])

2. Java

Java uses 2D arrays for matrix representation.

int[][] A = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

3. C++

C++ provides static and dynamic matrix declarations.

Static Matrix Declaration

int A[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

Dynamic Matrix (Using Vectors)

#include <vector> vector<vector<int>> A = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

4. JavaScript

JavaScript allows arrays of arrays for matrix representation.

let A = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ];

5. C#

C# also uses 2D arrays similar to Java.

int[,] A = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

6. Go

Go provides multi-dimensional arrays for matrices.

var A = [3][3]int{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, }

Matrices are a crucial data structure in DSA, offering structured storage and fast access times. Understanding memory representation, indexing, and how to define matrices in different languages is essential for solving problems related to graphs, dynamic programming, and game development.

Let's solve some coding problems to see matrix data structure in action.

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible