Skip to content

Matrix in DSA documentation added #1143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/dsa/Matrix/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"label": "Matrix",
"position": 9,
"link": {
"type": "generated-index",
"description": "In data structures and algorithms (DSA), a matrix is a two-dimensional array consisting of rows and columns. It is often used to represent a grid-like structure or a table of values. Matrices are commonly used in various algorithms and mathematical operations, such as matrix multiplication, graph algorithms, image processing, and more. They provide a convenient way to organize and manipulate data in a structured manner."
}
}

Binary file added docs/dsa/Matrix/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
153 changes: 153 additions & 0 deletions docs/dsa/Matrix/matrix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
id: matrix-in-dsa
title: Matrix Data Structure
sidebar_label: Matrix
sidebar_position: 1
description: "A matrix is a two-dimensional data structure consisting of rows and columns, where each element is identified by its row and column index. It is commonly used in various fields, including mathematics, computer science, and data analysis, to represent and manipulate structured data. "
tags: [dsa, data-structures, Matrix]
---

**Matrix Data Structure** is a two-dimensional array arranged in rows and columns. It is commonly used to represent mathematical matrices and is fundamental in various fields like mathematics, computer graphics, and data processing. Matrices allow for efficient storage and manipulation of data in a structured format

## Components of Matrix Data Structure
- Size: A matrix has a specific size, defined by its number of rows and columns.
- Element: A matrix’s row and column indices serve to identify each entry, which is referred to as an element.
- Operations: Scalar multiplication and the operations of addition, subtraction, and multiplication on matrices are also supported.
- Determinant: A square matrix’s determinant is a scalar number that may be used to solve systems of linear equations and carry out other linear algebraic operations.
- Inverse: If a square matrix has an inverse, it may be used to solve linear equation systems and carry out other linear algebraic operations.
- Transpose: By flipping a matrix along its main diagonal and switching the rows and columns, you may create the transpose of the matrix.
- Rank: In many applications, including the solution of linear equations and linear regression analysis, the rank of a matrix—a measure of its linearly independent rows or columns—is utilized

## Applications of Matrix Data Structure
- Linear Algebra: Matrices are widely used in linear algebra, a branch of mathematics that deals with linear equations, vector spaces, and linear transformations. Matrices are used to represent linear equations and to solve systems of linear equations.
- Optimization: Matrices are used in optimization problems, such as linear programming, to represent the constraints and objective functions of the problem.
- Statistics: Matrices are used in statistics to represent data and to perform operations such as correlation and regression.
- Signal Processing: Matrices are used in signal processing to represent signals and to perform operations such as filtering and transformation.
- Network Analysis: Matrices are used in network analysis to represent graphs and to perform operations such as finding the shortest path between two nodes.
- Quantum Mechanics: Matrices are used in quantum mechanics to represent states and operations in quantum systems.



## Representation of Matrix Data Structure:

![alt text](image.png)

As you can see from the above image, the elements are organized in rows and columns. As shown in the above image the cell x[0][0] is the first element of the first row and first column. The value in the first square bracket represents the row number and the value inside the second square bracket represents the column number. (i.e, x[row][column]).

## Declaration of Matrix Data Structure :

Declaration of a Matrix or two-dimensional array is very much similar to that of a one-dimensional array, given as follows.
``` python
# Defining number of rows and columns in matrix
number_of_rows = 3
number_of_columns = 3
# Declaring a matrix of size 3 X 3, and initializing it with value zero
rows, cols = (3, 3)
arr = [[0]*cols]*rows
print(arr)
```

## Initializing Matrix Data Structure:
In initialization, we assign some initial value to all the cells of the matrix. Below is the implementation to initialize a matrix in different languages:

``` python
# Initializing a 2-D array with values
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
```

## Operations on Matrix Data Structure:

We can perform a variety of operations on the Matrix Data Structure. Some of the most common operations are:

- Access elements of Matrix
- Traversal of a Matrix
- Searching in a Matrix
- Sorting a Matrix

## 1. Access elements of Matrix Data Structure:

Like one-dimensional arrays, matrices can be accessed randomly by using their indices to access the individual elements. A cell has two indices, one for its row number, and the other for its column number. We can use arr[i][j] to access the element which is at the ith row and jth column of the matrix.

```python
# Initializing a 2-D array with values
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Accessing elements of 2-D array
print("First element of first row:", arr[0][0])
print("Third element of second row:", arr[1][2])
print("Second element of third row:", arr[2][1])
```

## 2. Traversal of a Matrix Data Structure:
We can traverse all the elements of a matrix or two-dimensional array by using two for-loops.

``` python
arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
```
```python
# Traversing over all the rows
for i in range(0, 3):
# Traversing over all the columns of each row
for j in range(0, 4):
print(arr[i][j], end=" ")
print("")
```

Output
``` python
1 2 3 4
5 6 7 8
9 10 11 12
```
## 3. Searching in a Matrix Data Structure:

We can search an element in a matrix by traversing all the elements of the matrix.

Below is the implementation to search an element in a matrix:

```python
# Python code for above approach
def searchInMatrix(arr, x):
# m=4,n=5
for i in range(0, 4):
for j in range(0, 5):
if(arr[i][j] == x):
return 1
return

x = 8
arr = [[0, 6, 8, 9, 11],
[20, 22, 28, 29, 31],
[36, 38, 50, 61, 63],
[64, 66, 100, 122, 128]]
if(searchInMatrix(arr, x)):
print("YES")
else:
print("NO")

# This code is contributed by dhairyagothi.
```

Output
```python
YES
```

## 4. Sorting Matrix Data Structure:
We can sort a matrix in two-ways:

- Sort the matrix row-wise
- Sort the matrix column-wise

## Advantages of Matrix Data Structure:
- It helps in 2D Visualization.
- It stores multiple elements of the same type using the same name.
- It enables access to items at random.
- Any form of data with a fixed size can be stored.
- It is easy to implement.

## Disadvantages of Matrix Data Structure:
- Space inefficient when we need to store very few elements in the matrix.
- The matrix size should be needed beforehand.
- Insertion and deletion operations are costly if shifting occurs.
- Resizing a matrix is time-consuming.
130 changes: 130 additions & 0 deletions docs/dsa/Matrix/problems-matrix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
---
id: matrix-problems
title: Matrix Practice Problems
sidebar_label: Matrix Practice Problems
sidebar_position: 2
description: "A matrix is a two-dimensional data structure consisting of rows and columns, where each element is identified by its row and column index. It is commonly used in various fields, including mathematics, computer science, and data analysis, to represent and manipulate structured data. "
tags: [dsa, data-structures, Matrix ]
---

## Sort the given matrix

Given a n x n matrix. The problem is to sort the given matrix in strict order. Here strict order means that the matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row ‘i’, where ```1 <= i <= n-1```, the first element of row ‘i’ is greater than or equal to the last element of row ‘i-1’.

**Examples:**
```
Input : mat[][] = { {5, 4, 7},
{1, 3, 8},
{2, 9, 6} }
Output : 1 2 3
4 5 6
7 8 9
```
## Solution
```python
# Python program for the above approach
# driver code
v = [[5,4,7], [1,3,8], [2,9,6]]
n = len(v)

x = []
for i in range(n):
for j in range(n):
x.append(v[i][j])

x.sort()
k = 0
for i in range(n):
for j in range(n):
v[i][j] = x[k]
k += 1

print("Sorted Matrix will be: ")
for i in range(n):
for j in range(n):
print(v[i][j], end=" ")
print("")

# THIS CODE IS CONTRIBUTED BY Dhairya Gothi(dhairyagothi)
```

## Output
Sorted Matrix Will be:
```
1 2 3
4 5 6
7 8 9
```
**Time Complexity:** O(n2log2n), O(n*n) for traversing, and O(n2log2n) for sorting the vector x, which has a size of n2. So overall time complexity is O(n2log2n).
**Auxiliary Space:** O(n*n), For vector.

## Program for scalar multiplication of a matrix

Given a matrix and a scalar element k, our task is to find out the scalar product of that matrix.

**Examples:**
```
Input : mat[][] = {{2, 3}
{5, 4}}
k = 5
Output : 10 15
25 20
We multiply 5 with every element.

Input : 1 2 3
4 5 6
7 8 9
k = 4
Output : 4 8 12
16 20 24
28 32 36
The scalar multiplication of a number k(scalar), multiply it on every entry in the matrix. and a matrix A is the matrix kA.
```

```python
# Python 3 program to find the scalar
# product of a matrix

# Size of given matrix
N = 3

def scalarProductMat( mat, k):

# scalar element is multiplied
# by the matrix
for i in range( N):
for j in range( N):
mat[i][j] = mat[i][j] * k

# Driver code
if __name__ == "__main__":

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

scalarProductMat(mat, k)

# to display the resultant matrix
print("Scalar Product Matrix is : ")
for i in range(N):
for j in range(N):
print(mat[i][j], end = " ")
print()

# This code is contributed by dhairya
```
## Output:
```
Scalar Product Matrix is :
4 8 12
16 20 24
28 32 36
```


**ime Complexity:** O(n2),

**Auxiliary Space:** O(1), since no extra space has been taken.

Loading