mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -6,20 +6,20 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Undirected graph class based on adjacency matrix */
|
||||
/* Undirected graph class implemented with an adjacency matrix */
|
||||
class GraphAdjMat {
|
||||
vector<int> vertices; // Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
|
||||
vector<vector<int>> adjMat; // Adjacency matrix, where the row and column indices correspond to the "vertex index"
|
||||
vector<int> vertices; // Vertex list: elements are vertex values, and indices are vertex indices
|
||||
vector<vector<int>> adjMat; // Adjacency matrix: row and column indices correspond to vertex indices
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {
|
||||
// Add vertex
|
||||
// Add a vertex
|
||||
for (int val : vertices) {
|
||||
addVertex(val);
|
||||
}
|
||||
// Add edge
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
// Add an edge
|
||||
// Note that the entries in edges are vertex indices, corresponding to element indices in vertices
|
||||
for (const vector<int> &edge : edges) {
|
||||
addEdge(edge[0], edge[1]);
|
||||
}
|
||||
@@ -30,10 +30,10 @@ class GraphAdjMat {
|
||||
return vertices.size();
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
/* Add a vertex */
|
||||
void addVertex(int val) {
|
||||
int n = size();
|
||||
// Add the value of the new vertex to the vertex list
|
||||
// Append the new vertex value to the vertex list
|
||||
vertices.push_back(val);
|
||||
// Add a row to the adjacency matrix
|
||||
adjMat.emplace_back(vector<int>(n, 0));
|
||||
@@ -43,45 +43,45 @@ class GraphAdjMat {
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
/* Remove a vertex */
|
||||
void removeVertex(int index) {
|
||||
if (index >= size()) {
|
||||
throw out_of_range("Vertex does not exist");
|
||||
throw out_of_range("Invalid vertex");
|
||||
}
|
||||
// Remove the vertex at index from the vertex list
|
||||
// Remove the vertex at index `index` from the vertex list
|
||||
vertices.erase(vertices.begin() + index);
|
||||
// Remove the row at index from the adjacency matrix
|
||||
// Remove row `index` from the adjacency matrix
|
||||
adjMat.erase(adjMat.begin() + index);
|
||||
// Remove the column at index from the adjacency matrix
|
||||
// Remove column `index` from the adjacency matrix
|
||||
for (vector<int> &row : adjMat) {
|
||||
row.erase(row.begin() + index);
|
||||
}
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
/* Add an edge */
|
||||
// Parameters i and j are element indices in vertices
|
||||
void addEdge(int i, int j) {
|
||||
// Handle index out of bounds and equality
|
||||
// Handle out-of-bounds or equal indices
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {
|
||||
throw out_of_range("Vertex does not exist");
|
||||
throw out_of_range("Invalid vertex");
|
||||
}
|
||||
// In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)
|
||||
// For an undirected graph, the adjacency matrix is symmetric about its main diagonal: entries (i, j) and (j, i) are equal
|
||||
adjMat[i][j] = 1;
|
||||
adjMat[j][i] = 1;
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
/* Remove an edge */
|
||||
// Parameters i and j are element indices in vertices
|
||||
void removeEdge(int i, int j) {
|
||||
// Handle index out of bounds and equality
|
||||
// Handle out-of-bounds or equal indices
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {
|
||||
throw out_of_range("Vertex does not exist");
|
||||
throw out_of_range("Invalid vertex");
|
||||
}
|
||||
adjMat[i][j] = 0;
|
||||
adjMat[j][i] = 0;
|
||||
}
|
||||
|
||||
/* Print adjacency matrix */
|
||||
/* Print the adjacency matrix */
|
||||
void print() {
|
||||
cout << "Vertex list = ";
|
||||
printVector(vertices);
|
||||
@@ -90,37 +90,37 @@ class GraphAdjMat {
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Add edge */
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
/* Initialize the undirected graph */
|
||||
// Note that the entries in edges are vertex indices, corresponding to element indices in vertices
|
||||
vector<int> vertices = {1, 3, 2, 5, 4};
|
||||
vector<vector<int>> edges = {{0, 1}, {0, 3}, {1, 2}, {2, 3}, {2, 4}, {3, 4}};
|
||||
GraphAdjMat graph(vertices, edges);
|
||||
cout << "\nAfter initialization, graph is" << endl;
|
||||
cout << "\nGraph after initialization:" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Add vertex
|
||||
/* Add an edge */
|
||||
// Vertices 1 and 2 have indices 0 and 2, respectively
|
||||
graph.addEdge(0, 2);
|
||||
cout << "\nAfter adding edge 1-2, graph is" << endl;
|
||||
cout << "\nGraph after adding edge 1-2:" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertices 1, 3 have indices 0, 1 respectively
|
||||
/* Remove an edge */
|
||||
// Vertices 1 and 3 have indices 0 and 1, respectively
|
||||
graph.removeEdge(0, 1);
|
||||
cout << "\nAfter removing edge 1-3, graph is" << endl;
|
||||
cout << "\nGraph after removing edge 1-3:" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
/* Add a vertex */
|
||||
graph.addVertex(6);
|
||||
cout << "\nAfter adding vertex 6, graph is" << endl;
|
||||
cout << "\nGraph after adding vertex 6:" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
/* Remove a vertex */
|
||||
// Vertex 3 has index 1
|
||||
graph.removeVertex(1);
|
||||
cout << "\nAfter removing vertex 3, graph is" << endl;
|
||||
cout << "\nGraph after removing vertex 3:" << endl;
|
||||
graph.print();
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user