- 数据结构与算法
- DSA - 首页
- DSA - 概述
- DSA - 环境设置
- DSA - 算法基础
- DSA - 渐进分析
- 数据结构
- DSA - 数据结构基础
- DSA - 数据结构和类型
- DSA - 数组数据结构
- 链表
- DSA - 链表数据结构
- DSA - 双向链表数据结构
- DSA - 循环链表数据结构
- 栈和队列
- DSA - 栈数据结构
- DSA - 表达式解析
- DSA - 队列数据结构
- 搜索算法
- DSA - 搜索算法
- DSA - 线性搜索算法
- DSA - 二分搜索算法
- DSA - 插值搜索
- DSA - 跳跃搜索算法
- DSA - 指数搜索
- DSA - 斐波那契搜索
- DSA - 子列表搜索
- DSA - 哈希表
- 排序算法
- DSA - 排序算法
- DSA - 冒泡排序算法
- DSA - 插入排序算法
- DSA - 选择排序算法
- DSA - 归并排序算法
- DSA - 希尔排序算法
- DSA - 堆排序
- DSA - 桶排序算法
- DSA - 计数排序算法
- DSA - 基数排序算法
- DSA - 快速排序算法
- 图数据结构
- DSA - 图数据结构
- DSA - 深度优先遍历
- DSA - 广度优先遍历
- DSA - 生成树
- 树数据结构
- DSA - 树数据结构
- DSA - 树的遍历
- DSA - 二叉搜索树
- DSA - AVL树
- DSA - 红黑树
- DSA - B树
- DSA - B+树
- DSA - 伸展树
- DSA - 字典树
- DSA - 堆数据结构
- 递归
- DSA - 递归算法
- DSA - 使用递归的汉诺塔
- DSA - 使用递归的斐波那契数列
- 分治法
- DSA - 分治法
- DSA - 最大-最小问题
- DSA - Strassen矩阵乘法
- DSA - Karatsuba算法
- 贪心算法
- DSA - 贪心算法
- DSA - 旅行商问题(贪心法)
- DSA - Prim最小生成树
- DSA - Kruskal最小生成树
- DSA - Dijkstra最短路径算法
- DSA - 地图着色算法
- DSA - 分数背包问题
- DSA - 带截止日期的作业排序
- DSA - 最佳合并模式算法
- 动态规划
- DSA - 动态规划
- DSA - 矩阵链乘法
- DSA - Floyd Warshall算法
- DSA - 0-1背包问题
- DSA - 最长公共子序列算法
- DSA - 旅行商问题(动态规划法)
- 近似算法
- DSA - 近似算法
- DSA - 顶点覆盖算法
- DSA - 集合覆盖问题
- DSA - 旅行商问题(近似法)
- 随机化算法
- DSA - 随机化算法
- DSA - 随机化快速排序算法
- DSA - Karger最小割算法
- DSA - Fisher-Yates洗牌算法
- DSA有用资源
- DSA - 问答
- DSA - 快速指南
- DSA - 有用资源
- DSA - 讨论
矩阵或网格数据结构
什么是矩阵数据结构?
矩阵,也称为网格,是一种特殊的二维数组,其中元素按行和列排列。我们也可以说它是在另一个数组中嵌套的数组。矩阵的每个元素都可以通过行和列索引来识别。
通常,矩阵数据结构用于存储和操作二维结构,例如图形、地图、表格等。它还可以用于表示线性方程、变换、旋转和其他数学概念。
矩阵的声明和初始化
要声明一个矩阵,我们只需要指定矩阵的数据类型和名称,后跟两个方括号。这些方括号指定矩阵的行和列。
在所有编程语言中,矩阵的声明和初始化过程都非常相似。让我们看看在各种编程语言中创建矩阵的语法:
data_type array_name[rows][cols] = {elements separated by commas};
在 Python 编程语言中,不需要指定数据类型:
array_name[rows][cols] = {elements separated by commas};
矩阵的必要性
矩阵在各个领域都非常有用,包括数学、计算机科学、图形、机器人技术等等。全球各地的公司都以矩阵的形式存储其用户的信息,这有助于进行推荐和目标营销。
无论是我们最喜欢的电影还是游戏,都是借助矩阵计算构建的。许多科学创新和研究直接或间接地需要这些计算。
矩阵表示
我们可以将矩阵数据结构表示为一个表格,其中每个元素都存储在一个单元格中。下图说明了矩阵是如何表示的:
从上面的说明中,我们可以得出以下重要结论:
索引从 0 开始。
一个 5x3 维度的矩阵将有 15 个元素。
我们可以借助其行和列索引访问或定位任何元素。
矩阵的基本操作
我们可以通过对给定矩阵数据结构执行各种操作来操作它,例如旋转、加法、乘法等等。
以下是可以在给定矩阵上执行的基本操作:
- 访问 - 访问矩阵的特定行或列。
- 搜索 - 定位矩阵的特定元素。
- 排序 - 按特定顺序排列矩阵元素。
- 插入 - 在指定的索引处添加一行。
- 删除 - 从矩阵中删除一行。
矩阵 - 访问操作
在访问操作中,我们打印特定行或列的元素。
算法
以下是访问矩阵元素的算法:
1. Start 2. Declare and initialize a matrix. 3. Access the required row. 4. Print the result. 5. Stop
示例
在这里,我们看到了访问操作的实际实现,我们尝试打印一行的元素:
#include <stdio.h>
int main() {
// declaration and initialization of a 3x3 matrix
int matrix[3][3] = {{1, 2, 1}, {4, 5, 4}, {7, 8, 7}};
// accessing the second row
int* rowScnd = matrix[1];
// loop to print the result
printf("Accessing a row: ");
for (int i = 0; i < 3; i++) {
printf("%d ", rowScnd[i]);
}
}
#include <iostream>
using namespace std;
int main() {
// declaration and initialization of a 3x3 matrix
int matrix[3][3] = {{1, 2, 1}, {4, 5, 4}, {7, 8, 7}};
// accessing the second row
int* rowScnd = matrix[1];
// loop to print the result
cout<< "Accessing a row: ";
for (int i = 0; i < 3; i++) {
cout << rowScnd[i] << " ";
}
cout << endl;
}
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// declaration and initialization of a 3x3 matrix
int matrix[][] = {{1, 2, 1}, {4, 5, 4}, {7, 8, 7}};
// accessing the second row
int rowScnd[] = matrix[1];
// printing the result
System.out.println("Accessing a row: " + Arrays.toString(rowScnd));
}
}
# declaration and initialization of a 3x3 matrix
matrix = [[1, 2, 1], [4, 5, 4], [7, 8, 7]]
# accessing the second row
rowScnd = matrix[1]
# printing the result
print("Accessing a row:" )
print(rowScnd)
输出
Accessing a row: 4 5 4
矩阵 - 搜索操作
要搜索给定矩阵中指定的元素,我们需要遍历每一行和每一列,并将元素与我们要查找的值进行比较。
算法
以下算法演示了如何在给定矩阵中搜索元素:
1. Start 2. Declare and initialize a matrix. 3. Define the target element. 4. Use a for loop to search elements in the row. 5. Define another for loop to search elements in the column. 6. If found return the indices, otherwise return [-1, -1]. 7. Stop
示例
让我们看看在各种编程语言中搜索操作的实际示例:
#include <stdio.h>
#include <stdlib.h>
// Function to search element
int* srchmatrix(int matrix[3][3], int target) {
// array to hold the result
static int indX[2];
// Looping through each row
for (int i = 0; i < 3; i++) {
// Looping through each column
for (int j = 0; j < 3; j++) {
// Comparing the element with the targeted element
if (matrix[i][j] == target) {
// to return the row and column indices
int* indX = malloc(2 * sizeof(int));
indX[0] = i;
indX[1] = j;
return indX;
}
}
}
// return negative value if element not found
indX[0] = -1;
indX[1] = -1;
return indX;
}
int main() {
// declaration and initialization of a 3x3 matrix
int matrix[3][3] = {{1, 2, 1}, {4, 5, 4}, {7, 8, 7}};
// calling the function
int* indX = srchmatrix(matrix, 5);
// to print the result
printf("The specified element is at index: [%d, %d]\n", indX[0], indX[1]);
free(indX);
return 0;
}
#include <iostream>
using namespace std;
// Function to search element
int* srchmatrix(int matrix[3][3], int targtElem) {
// array to hold the result
static int indX[2];
// Looping through each row
for (int i = 0; i < 3; i++) {
// Looping through each column
for (int j = 0; j < 3; j++) {
// Comparing the element with the targeted element
if (matrix[i][j] == targtElem) {
// to return the row and column indices
indX[0] = i;
indX[1] = j;
return indX;
}
}
}
// return negative value if element not found
indX[0] = -1;
indX[1] = -1;
return indX;
}
int main() {
// declaration and initialization of a 3x3 matrix
int matrix[3][3] = {{1, 2, 1}, {4, 5, 4}, {7, 8, 7}};
// calling the function
int* indX = srchmatrix(matrix, 5);
// to print the result
cout << "The specified element is at index: [" << indX[0] << ", " << indX[1] << "]" << endl;
return 0;
}
public class Main {
// method to search element
public static int[] srchmatrix(int[][] matrix, int targtElem) {
// Looping through each row
for (int i = 0; i < matrix.length; i++) {
// Looping through each column
for (int j = 0; j < matrix[i].length; j++) {
// Comparing the element with the desired element
if (matrix[i][j] == targtElem) {
// to return the row and column indices
return new int[]{i, j};
}
}
}
// return negative value if element not found
return new int[]{-1, -1};
}
public static void main(String[] args) {
// declaration and initialization of a 3x3 matrix
int[][] matrix = {{1, 2, 1}, {4, 5, 4}, {7, 8, 7}};
// desired element we are looking for
int targtElem = 5;
// calling the method
int[] indX = srchmatrix(matrix, targtElem);
// printing the result
System.out.println("The specified element is at index: [" + indX[0] + ", " + indX[1] + "]");
}
}
# declaration and initialization of a 3x3 matrix
matrix = [[1, 2, 1], [4, 5, 4], [7, 8, 7]]
# method to search element
def searchMatrix(matrix, targtElem):
# Looping through each row
for i in range(len(matrix)):
# Looping through each column
for j in range(len(matrix[i])):
# Comparing the element with the desired element
if matrix[i][j] == targtElem:
# to return the row and column indices
return (i, j)
# Return negative value if element not found
return (-1, -1)
# desired element we are looking for
targtElem = 5
# calling the method
indX = searchMatrix(matrix, targtElem)
# printing the result
print(f"The specified element is at index: {indX}")
输出
The specified element is at index: [1, 1]
矩阵 - 排序操作
在排序操作中,我们按指定的顺序(例如升序或降序)排列给定矩阵的元素。
算法
按升序排列矩阵元素的算法如下:
1. Start 2. Declare and initialize a matrix. 3. Compare and sort each element of the specified row. 4. Print the result. 5. Stop
示例
在以下示例中,我们看到了排序操作的实际实现:
#include <stdio.h>
// function to sort the array
void araySort(int mat[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (mat[j] > mat[j+1]) {
// swapping mat[j] and mat[j+1]
int temp = mat[j];
mat[j] = mat[j+1];
mat[j+1] = temp;
}
}
}
}
int main() {
// declaration and initialization of 3x4 matrix
int matrix[3][4] = {{12, 10, 7, 36}, {20, 9, 8, 4}, {15, 73, 83, 13}};
// Sorting the first row
araySort(matrix[0], 4);
// Printing the result
printf("The matrix after sorting first row: \n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
// declaration and initialization of 3x4 matrix
int matrix[3][4] = {{12, 10, 7, 36}, {20, 9, 8, 4}, {15, 73, 83, 13}};
// Sorting the first row
sort(matrix[0], matrix[0] + 4);
// Printing the result
cout << "The matrix after sorting first row: " << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
import java.util.Arrays;
public class Main {
public static void main(String []args) {
// declaration and initialization of 3x4 matrix
int[][] matrix = {{12, 10, 7, 36}, {20, 9, 8, 4}, {15, 73, 83, 13}};
// Sorting the first row
Arrays.sort(matrix[0]);
// Printing the result
System.out.println("The matrix after sorting first row: " );
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
# declaration and initialization of 3x4 matrix
matrix = [[12, 10, 7, 36], [20, 9, 8, 4], [15, 73, 83, 13]]
# Sorting the first row
matrix[0].sort()
# Printing the result
print("The matrix after sorting first row: ")
for row in matrix:
print(' '.join(map(str, row)))
输出
The matrix after sorting first row: 7 10 12 36 20 9 8 4 15 73 83 13
矩阵 - 插入操作
在插入操作中,我们在矩阵的指定位置插入一行。
算法
以下是将一行插入给定矩阵的第二个位置的算法:
1. Start 2. Declare and initialize a matrix. 3. Define another matrix. 4. Copy the first row of original matrix to new matrix. 5. Insert the required row at second index. 6. Copy the remaining rows. 7. Print the result. 8. Stop
示例
下面的示例在不同的编程语言中实际说明了插入操作:
#include <stdio.h>
int main() {
// The original matrix
int matrix[2][3] = {{19, 14, 21}, {22, 91, 81}};
// Create a new matrix with an extra row
int newmatrix[3][3];
// Copy the first row of the original matrix to the new matrix
for (int j = 0; j < 3; j++) {
newmatrix[0][j] = matrix[0][j];
}
// Adding second row to the new matrix
int newRow[3] = {53, 63, 73};
for (int j = 0; j < 3; j++) {
newmatrix[1][j] = newRow[j];
}
// Copying the remaining rows of the original matrix to the new matrix
for (int i = 2; i < 3; i++) {
for (int j = 0; j < 3; j++) {
newmatrix[i][j] = matrix[i - 1][j];
}
}
// Printing the new matrix
printf("The new Matrix after adding a row: \n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", newmatrix[i][j]);
}
printf("\n");
}
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
// The original matrix
int matrix[2][3] = {{19, 14, 21}, {22, 91, 81}};
// Create a new matrix with an extra row
int newmatrix[3][3];
// Copy the first row of the original matrix to the new matrix
copy(begin(matrix[0]), end(matrix[0]), begin(newmatrix[0]));
// Adding second row to the new matrix
int newRow[3] = {53, 63, 73};
copy(begin(newRow), end(newRow), begin(newmatrix[1]));
// Copying the remaining rows of the original matrix to the new matrix
copy(begin(matrix[1]), end(matrix[1]), begin(newmatrix[2]));
// Printing the new matrix
cout << "The new Matrix after adding a row: " << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << newmatrix[i][j] << ' ';
}
cout << '\n';
}
return 0;
}
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// the original matrix
int[][] matrix = {{19, 14, 21}, {22, 91, 81}};
// Create a new matrix with an extra row
int[][] newmatrix = new int[matrix.length + 1][matrix[0].length];
// Copy the first row of the original matrix to the new matrix
for (int j = 0; j < matrix[0].length; j++) {
newmatrix[0][j] = matrix[0][j];
}
// adding second row to the new matrix
int[] newRow = {53, 63, 73};
for (int j = 0; j < newRow.length; j++) {
newmatrix[1][j] = newRow[j];
}
// Copying the remaining rows of the original matrix to the new matrix
for (int i = 2; i < newmatrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
newmatrix[i][j] = matrix[i - 1][j];
}
}
// Printing the new matrix
System.out.println("The new Matrix after adding a row: ");
for (int i = 0; i < newmatrix.length; i++) {
for (int j = 0; j < newmatrix[0].length; j++) {
System.out.print(newmatrix[i][j] + " ");
}
System.out.println();
}
}
}
# The original matrix
matrix = [[19, 14, 21], [22, 91, 81]]
# Create a new matrix with an extra row
newmatrix = matrix.copy()
# Adding second row to the new matrix
newRow = [53, 63, 73]
newmatrix.insert(1, newRow)
# Printing the new matrix
print("The new Matrix after adding a row: ")
for row in newmatrix:
print(' '.join(map(str, row)))
输出
The new Matrix after adding a row: 19 14 21 53 63 73 22 91 81
矩阵 - 删除操作
删除操作从矩阵中删除特定行。
算法
以下是对给定矩阵执行删除操作的算法:
1. Start 2. Declare and initialize a matrix. 3. Define another matrix. 4. Copy all elements except the row to be deleted. 5. Print the new matrix. 6. Stop
示例
在这里,我们看到了删除操作的实际实现:
#include <stdio.h>
// Function to delete a row from the matrix
void deleteRow(int mat[3][3], int rowIndex, int rows, int cols) {
// Create a new matrix with one less row
int newMat[2][3];
// Copy the elements except the row to be deleted
int newRow = 0;
// Loop through the rows of original matrix
for (int i = 0; i < rows; i++) {
// Skip the row to be deleted
if (i != rowIndex) {
// Loop through the cols of original matrix
for (int j = 0; j < cols; j++) {
// Copy the element from mat to newMat
newMat[newRow][j] = mat[i][j];
}
newRow++; // Increment the row index
}
}
// Print the new matrix
printf("The new Matrix after deleting a row: \n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", newMat[i][j]);
}
printf("\n");
}
}
int main() {
// The original matrix
int matrix[3][3] = {{19, 14, 21}, {53, 63, 73}, {22, 91, 81}};
// Delete the first row
deleteRow(matrix, 0, 3, 3);
return 0;
}
#include <iostream>
using namespace std;
// Function to delete a row from the matrix
void deleteRow(int mat[3][3], int rowIndex, int rows, int cols) {
// Create a new matrix with one less row
int newMat[2][3];
// Copy the elements except the row to be deleted
int newRow = 0;
// Loop through the rows of original matrix
for (int i = 0; i < rows; i++) {
// Skip the row to be deleted
if (i != rowIndex) {
// Loop through the cols of original matrix
for (int j = 0; j < cols; j++) {
// Copy the element from mat to newMat
newMat[newRow][j] = mat[i][j];
}
newRow++; // Increment the row index
}
}
// Print the new matrix
cout << "The new Matrix after deleting a row: \n";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < cols; j++) {
cout << newMat[i][j] << ' ';
}
cout << '\n';
}
}
int main() {
// The original matrix
int matrix[3][3] = {{19, 14, 21}, {53, 63, 73}, {22, 91, 81}};
// Delete the first row
deleteRow(matrix, 0, 3, 3);
return 0;
}
import java.util.Arrays;
public class Main {
// method to delete row
public static int[][] deleteRow(int[][] mat, int rowIndex) {
// get the number of rows and columns
int rows = mat.length;
int cols = mat[0].length;
// create a new matrix with one less row
int[][] newMat = new int[rows - 1][cols];
// copy the elements except the row to be deleted
int newRow = 0;
// loop through the rows of original matrix
for (int i = 0; i < rows; i++) {
// skip the row to be deleted
if (i != rowIndex) {
// loop through the cols of original matrix
for (int j = 0; j < cols; j++) {
// copy the element from mat to newMat
newMat[newRow][j] = mat[i][j];
}
newRow++; // increment the row index
}
}
// return the new matrix
return newMat;
}
public static void main(String[] args) {
// the original matrix
int[][] matrix = {{19, 14, 21}, {53, 63, 73}, {22, 91, 81}};
// delete the first row
int[][] newmatrix = deleteRow(matrix, 0);
// Printing the new matrix
System.out.println("The new Matrix after deleting a row: ");
for (int i = 0; i < newmatrix.length; i++) {
for (int j = 0; j < newmatrix[0].length; j++) {
System.out.print(newmatrix[i][j] + " ");
}
System.out.println();
}
}
}
# The original matrix
matrix = [[19, 14, 21], [53, 63, 73], [22, 91, 81]]
# Delete the first row
newmatrix = matrix[1:]
# Printing the new matrix
print("The new Matrix after deleting a row: ")
for row in newmatrix:
print(' '.join(map(str, row)))
输出
The new Matrix after deleting a row: 53 63 73 22 91 81