Intro

Matrix is a 2-dimensional Array representing a collection of numbers arranged in an order of rows and columns

zero_matrix = [[0 for _ in range(len(matrix[0]))] for _ in range(len(matrix))]

copied_matrix = [row[:] for row in matrix]

Square Matrix has the number of columns = the number of rows

Sorted Matrix has ****elements sorted in increasing/decreasing order

Binary Matrix has ****elements only equal to 0 or 1

Sparse Matrix stores only elements different than 0

Special Matrix has matrix[i][j] = i * j


Upper triangular = upper either diagonal elements

Lower triangular = lower either diagonal elements


Transposing a Matrix achieved by interchanging its rows into columns or columns into rows

transposed_matrix = zip(*matrix)

Relationships in a Matrix

Frame 4.png

https://cp-algorithms.com/linear_algebra/linear-system-gauss.html

https://cp-algorithms.com/linear_algebra/determinant-gauss.html

https://cp-algorithms.com/linear_algebra/determinant-kraut.html

https://cp-algorithms.com/linear_algebra/rank-matrix.html

Usage

Comparison

Advantages

Disadvantages

Problems

Corner Cases

Implementation

Time Complexity
Operations Worst case
search() O(1)
append() O(1)
insert() O(n)
remove() O(n*m)
Space Complexity
Implementation Worst case
Matrix as 2 Arrays O(n*m)

n is the number of rows

m in the number of columns

Algorithms

Patterns

Code

Row-wise traversal

Column-wise traversal

input : mat = [[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]]

# accessing element row wise
    for i in range(row):
        for j in range(col):
            ... arr[i][j] ...

output : row-wise: 1 2 3 4 5 6 7 8 9

input : mat = [[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]]

# accessing element row wise
    for i in range(row):
        for j in range(col):
            ... arr[j][i] ...

output : col-wise: 1 4 7 2 5 8 3 6 9

Clock-wise = Spiral traversal

Frame 1.png

Resources