Skip to content

Commit f769f76

Browse files
committed
Matrix in DSA documentation added
1 parent 11e7d65 commit f769f76

File tree

4 files changed

+292
-0
lines changed

4 files changed

+292
-0
lines changed

docs/dsa/Matrix/_category_.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"label": "Matrix",
3+
"position": 9,
4+
"link": {
5+
"type": "generated-index",
6+
"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."
7+
}
8+
}
9+

docs/dsa/Matrix/image.png

20.1 KB
Loading

docs/dsa/Matrix/matrix.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
id: matrix-in-dsa
3+
title: Matrix Data Structure
4+
sidebar_label: Matrix
5+
sidebar_position: 1
6+
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. "
7+
tags: [dsa, data-structures, Matrix]
8+
---
9+
10+
**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
11+
12+
## Components of Matrix Data Structure
13+
- Size: A matrix has a specific size, defined by its number of rows and columns.
14+
- Element: A matrix’s row and column indices serve to identify each entry, which is referred to as an element.
15+
- Operations: Scalar multiplication and the operations of addition, subtraction, and multiplication on matrices are also supported.
16+
- 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.
17+
- Inverse: If a square matrix has an inverse, it may be used to solve linear equation systems and carry out other linear algebraic operations.
18+
- Transpose: By flipping a matrix along its main diagonal and switching the rows and columns, you may create the transpose of the matrix.
19+
- 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
20+
21+
## Applications of Matrix Data Structure
22+
- 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.
23+
- Optimization: Matrices are used in optimization problems, such as linear programming, to represent the constraints and objective functions of the problem.
24+
- Statistics: Matrices are used in statistics to represent data and to perform operations such as correlation and regression.
25+
- Signal Processing: Matrices are used in signal processing to represent signals and to perform operations such as filtering and transformation.
26+
- Network Analysis: Matrices are used in network analysis to represent graphs and to perform operations such as finding the shortest path between two nodes.
27+
- Quantum Mechanics: Matrices are used in quantum mechanics to represent states and operations in quantum systems.
28+
29+
30+
31+
## Representation of Matrix Data Structure:
32+
33+
![alt text](image.png)
34+
35+
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]).
36+
37+
## Declaration of Matrix Data Structure :
38+
39+
Declaration of a Matrix or two-dimensional array is very much similar to that of a one-dimensional array, given as follows.
40+
``` python
41+
# Defining number of rows and columns in matrix
42+
number_of_rows = 3
43+
number_of_columns = 3
44+
# Declaring a matrix of size 3 X 3, and initializing it with value zero
45+
rows, cols = (3, 3)
46+
arr = [[0]*cols]*rows
47+
print(arr)
48+
```
49+
50+
## Initializing Matrix Data Structure:
51+
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:
52+
53+
``` python
54+
# Initializing a 2-D array with values
55+
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
56+
```
57+
58+
## Operations on Matrix Data Structure:
59+
60+
We can perform a variety of operations on the Matrix Data Structure. Some of the most common operations are:
61+
62+
- Access elements of Matrix
63+
- Traversal of a Matrix
64+
- Searching in a Matrix
65+
- Sorting a Matrix
66+
67+
## 1. Access elements of Matrix Data Structure:
68+
69+
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.
70+
71+
```python
72+
# Initializing a 2-D array with values
73+
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
74+
75+
# Accessing elements of 2-D array
76+
print("First element of first row:", arr[0][0])
77+
print("Third element of second row:", arr[1][2])
78+
print("Second element of third row:", arr[2][1])
79+
```
80+
81+
## 2. Traversal of a Matrix Data Structure:
82+
We can traverse all the elements of a matrix or two-dimensional array by using two for-loops.
83+
84+
``` python
85+
arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
86+
```
87+
```python
88+
# Traversing over all the rows
89+
for i in range(0, 3):
90+
# Traversing over all the columns of each row
91+
for j in range(0, 4):
92+
print(arr[i][j], end=" ")
93+
print("")
94+
```
95+
96+
Output
97+
``` python
98+
1 2 3 4
99+
5 6 7 8
100+
9 10 11 12
101+
```
102+
## 3. Searching in a Matrix Data Structure:
103+
104+
We can search an element in a matrix by traversing all the elements of the matrix.
105+
106+
Below is the implementation to search an element in a matrix:
107+
108+
```python
109+
# Python code for above approach
110+
def searchInMatrix(arr, x):
111+
# m=4,n=5
112+
for i in range(0, 4):
113+
for j in range(0, 5):
114+
if(arr[i][j] == x):
115+
return 1
116+
return
117+
118+
x = 8
119+
arr = [[0, 6, 8, 9, 11],
120+
[20, 22, 28, 29, 31],
121+
[36, 38, 50, 61, 63],
122+
[64, 66, 100, 122, 128]]
123+
if(searchInMatrix(arr, x)):
124+
print("YES")
125+
else:
126+
print("NO")
127+
128+
# This code is contributed by dhairyagothi.
129+
```
130+
131+
Output
132+
```python
133+
YES
134+
```
135+
136+
## 4. Sorting Matrix Data Structure:
137+
We can sort a matrix in two-ways:
138+
139+
- Sort the matrix row-wise
140+
- Sort the matrix column-wise
141+
142+
## Advantages of Matrix Data Structure:
143+
- It helps in 2D Visualization.
144+
- It stores multiple elements of the same type using the same name.
145+
- It enables access to items at random.
146+
- Any form of data with a fixed size can be stored.
147+
- It is easy to implement.
148+
149+
## Disadvantages of Matrix Data Structure:
150+
- Space inefficient when we need to store very few elements in the matrix.
151+
- The matrix size should be needed beforehand.
152+
- Insertion and deletion operations are costly if shifting occurs.
153+
- Resizing a matrix is time-consuming.

docs/dsa/Matrix/problems-matrix.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
id: matrix-problems
3+
title: Matrix Practice Problems
4+
sidebar_label: Matrix Practice Problems
5+
sidebar_position: 2
6+
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. "
7+
tags: [dsa, data-structures, Matrix ]
8+
---
9+
10+
## Sort the given matrix
11+
12+
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’.
13+
14+
**Examples:**
15+
```
16+
Input : mat[][] = { {5, 4, 7},
17+
{1, 3, 8},
18+
{2, 9, 6} }
19+
Output : 1 2 3
20+
4 5 6
21+
7 8 9
22+
```
23+
## Solution
24+
```python
25+
# Python program for the above approach
26+
# driver code
27+
v = [[5,4,7], [1,3,8], [2,9,6]]
28+
n = len(v)
29+
30+
x = []
31+
for i in range(n):
32+
for j in range(n):
33+
x.append(v[i][j])
34+
35+
x.sort()
36+
k = 0
37+
for i in range(n):
38+
for j in range(n):
39+
v[i][j] = x[k]
40+
k += 1
41+
42+
print("Sorted Matrix will be: ")
43+
for i in range(n):
44+
for j in range(n):
45+
print(v[i][j], end=" ")
46+
print("")
47+
48+
# THIS CODE IS CONTRIBUTED BY Dhairya Gothi(dhairyagothi)
49+
```
50+
51+
## Output
52+
Sorted Matrix Will be:
53+
```
54+
1 2 3
55+
4 5 6
56+
7 8 9
57+
```
58+
**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).
59+
**Auxiliary Space:** O(n*n), For vector.
60+
61+
## Program for scalar multiplication of a matrix
62+
63+
Given a matrix and a scalar element k, our task is to find out the scalar product of that matrix.
64+
65+
**Examples:**
66+
```
67+
Input : mat[][] = {{2, 3}
68+
{5, 4}}
69+
k = 5
70+
Output : 10 15
71+
25 20
72+
We multiply 5 with every element.
73+
74+
Input : 1 2 3
75+
4 5 6
76+
7 8 9
77+
k = 4
78+
Output : 4 8 12
79+
16 20 24
80+
28 32 36
81+
The scalar multiplication of a number k(scalar), multiply it on every entry in the matrix. and a matrix A is the matrix kA.
82+
```
83+
84+
```python
85+
# Python 3 program to find the scalar
86+
# product of a matrix
87+
88+
# Size of given matrix
89+
N = 3
90+
91+
def scalarProductMat( mat, k):
92+
93+
# scalar element is multiplied
94+
# by the matrix
95+
for i in range( N):
96+
for j in range( N):
97+
mat[i][j] = mat[i][j] * k
98+
99+
# Driver code
100+
if __name__ == "__main__":
101+
102+
mat = [[ 1, 2, 3 ],
103+
[ 4, 5, 6 ],
104+
[ 7, 8, 9 ]]
105+
k = 4
106+
107+
scalarProductMat(mat, k)
108+
109+
# to display the resultant matrix
110+
print("Scalar Product Matrix is : ")
111+
for i in range(N):
112+
for j in range(N):
113+
print(mat[i][j], end = " ")
114+
print()
115+
116+
# This code is contributed by dhairya
117+
```
118+
## Output:
119+
```
120+
Scalar Product Matrix is :
121+
4 8 12
122+
16 20 24
123+
28 32 36
124+
```
125+
126+
127+
**ime Complexity:** O(n2),
128+
129+
**Auxiliary Space:** O(1), since no extra space has been taken.
130+

0 commit comments

Comments
 (0)