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:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -6,13 +6,13 @@
#include "../utils/common.hpp"
/* Undirected graph class based on adjacency list */
/* Undirected graph class implemented with an adjacency list */
class GraphAdjList {
public:
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
// Adjacency list: key is a vertex, value is the list of all its neighbors
unordered_map<Vertex *, vector<Vertex *>> adjList;
/* Remove specified node from vector */
/* Remove the specified vertex from the vector */
void remove(vector<Vertex *> &vec, Vertex *vet) {
for (int i = 0; i < vec.size(); i++) {
if (vec[i] == vet) {
@@ -37,45 +37,45 @@ class GraphAdjList {
return adjList.size();
}
/* Add edge */
/* Add an edge */
void addEdge(Vertex *vet1, Vertex *vet2) {
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
throw invalid_argument("Vertex does not exist");
throw invalid_argument("Invalid vertex");
// Add edge vet1 - vet2
adjList[vet1].push_back(vet2);
adjList[vet2].push_back(vet1);
}
/* Remove edge */
/* Remove an edge */
void removeEdge(Vertex *vet1, Vertex *vet2) {
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
throw invalid_argument("Vertex does not exist");
throw invalid_argument("Invalid vertex");
// Remove edge vet1 - vet2
remove(adjList[vet1], vet2);
remove(adjList[vet2], vet1);
}
/* Add vertex */
/* Add a vertex */
void addVertex(Vertex *vet) {
if (adjList.count(vet))
return;
// Add a new linked list in the adjacency list
// Add a new list to the adjacency list
adjList[vet] = vector<Vertex *>();
}
/* Remove vertex */
/* Remove a vertex */
void removeVertex(Vertex *vet) {
if (!adjList.count(vet))
throw invalid_argument("Vertex does not exist");
// Remove the linked list corresponding to vertex vet in the adjacency list
throw invalid_argument("Invalid vertex");
// Remove the list corresponding to vertex vet from the adjacency list
adjList.erase(vet);
// Traverse the linked lists of other vertices and remove all edges containing vet
// Traverse the lists of the other vertices and remove all edges incident to vet
for (auto &adj : adjList) {
remove(adj.second, vet);
}
}
/* Print adjacency list */
/* Print the adjacency list */
void print() {
cout << "Adjacency list =" << endl;
for (auto &adj : adjList) {
@@ -87,4 +87,4 @@ class GraphAdjList {
}
};
// See graph_adjacency_list_test.cpp for test cases
// See graph_adjacency_list_test.cpp for the test example

View File

@@ -6,38 +6,38 @@
#include "./graph_adjacency_list.cpp"
/* Driver Code */
/* Driver code */
int main() {
/* Add edge */
/* Initialize the undirected graph */
vector<Vertex *> v = valsToVets(vector<int>{1, 3, 2, 5, 4});
vector<vector<Vertex *>> edges = {{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]},
{v[2], v[3]}, {v[2], v[4]}, {v[3], v[4]}};
GraphAdjList graph(edges);
cout << "\nAfter initialization, graph is" << endl;
cout << "\nGraph after initialization:" << endl;
graph.print();
/* Add edge */
// Vertices 1, 3 are v[0], v[1]
/* Add an edge */
// Vertices 1 and 2 are v[0] and v[2]
graph.addEdge(v[0], v[2]);
cout << "\nAfter adding edge 1-2, graph is" << endl;
cout << "\nGraph after adding edge 1-2:" << endl;
graph.print();
/* Remove edge */
// Vertex 3 is v[1]
/* Remove an edge */
// Vertices 1 and 3 are v[0] and v[1]
graph.removeEdge(v[0], v[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 */
Vertex *v5 = new Vertex(6);
graph.addVertex(v5);
cout << "\nAfter adding vertex 6, graph is" << endl;
cout << "\nGraph after adding vertex 6:" << endl;
graph.print();
/* Remove vertex */
/* Remove a vertex */
// Vertex 3 is v[1]
graph.removeVertex(v[1]);
cout << "\nAfter removing vertex 3, graph is" << endl;
cout << "\nGraph after removing vertex 3:" << endl;
graph.print();
// Free memory

View File

@@ -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;

View File

@@ -8,46 +8,46 @@
#include "./graph_adjacency_list.cpp"
/* Breadth-first traversal */
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
// Represent the graph with an adjacency list to access all neighbors of a given vertex
vector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {
// Vertex traversal sequence
vector<Vertex *> res;
// Hash set for recording vertices that have been visited
// Hash set recording visited vertices
unordered_set<Vertex *> visited = {startVet};
// Queue used to implement BFS
// Use a queue to implement BFS
queue<Vertex *> que;
que.push(startVet);
// Starting from vertex vet, loop until all vertices are visited
// Starting from startVet, continue until all reachable vertices have been visited
while (!que.empty()) {
Vertex *vet = que.front();
que.pop(); // Dequeue the front vertex
res.push_back(vet); // Record visited vertex
// Traverse all adjacent vertices of this vertex
res.push_back(vet); // Record the visited vertex
// Traverse all neighbors of this vertex
for (auto adjVet : graph.adjList[vet]) {
if (visited.count(adjVet))
continue; // Skip vertices that have been visited
que.push(adjVet); // Only enqueue unvisited vertices
continue; // Skip vertices that have already been visited
que.push(adjVet); // Enqueue only unvisited vertices
visited.emplace(adjVet); // Mark this vertex as visited
}
}
// Return vertex traversal sequence
// Return the vertex traversal sequence
return res;
}
/* Driver Code */
/* Driver code */
int main() {
/* Add edge */
/* Initialize the undirected graph */
vector<Vertex *> v = valsToVets({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
vector<vector<Vertex *>> edges = {{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]}, {v[1], v[4]},
{v[2], v[5]}, {v[3], v[4]}, {v[3], v[6]}, {v[4], v[5]},
{v[4], v[7]}, {v[5], v[8]}, {v[6], v[7]}, {v[7], v[8]}};
GraphAdjList graph(edges);
cout << "\nAfter initialization, graph is\\n";
cout << "\nGraph after initialization:\\n";
graph.print();
/* Breadth-first traversal */
vector<Vertex *> res = graphBFS(graph, v[0]);
cout << "\nBreadth-first traversal (BFS) vertex sequence is" << endl;
cout << "\nVertex sequence from breadth-first traversal (BFS):" << endl;
printVector(vetsToVals(res));
// Free memory

View File

@@ -9,41 +9,41 @@
/* Depth-first traversal helper function */
void dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {
res.push_back(vet); // Record visited vertex
res.push_back(vet); // Record the visited vertex
visited.emplace(vet); // Mark this vertex as visited
// Traverse all adjacent vertices of this vertex
// Traverse all neighbors of this vertex
for (Vertex *adjVet : graph.adjList[vet]) {
if (visited.count(adjVet))
continue; // Skip vertices that have been visited
// Recursively visit adjacent vertices
continue; // Skip vertices that have already been visited
// Recursively visit the neighboring vertex
dfs(graph, visited, res, adjVet);
}
}
/* Depth-first traversal */
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
// Represent the graph with an adjacency list to access all neighbors of a given vertex
vector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {
// Vertex traversal sequence
vector<Vertex *> res;
// Hash set for recording vertices that have been visited
// Hash set recording visited vertices
unordered_set<Vertex *> visited;
dfs(graph, visited, res, startVet);
return res;
}
/* Driver Code */
/* Driver code */
int main() {
/* Add edge */
/* Initialize the undirected graph */
vector<Vertex *> v = valsToVets(vector<int>{0, 1, 2, 3, 4, 5, 6});
vector<vector<Vertex *>> edges = {{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]},
{v[2], v[5]}, {v[4], v[5]}, {v[5], v[6]}};
GraphAdjList graph(edges);
cout << "\nAfter initialization, graph is" << endl;
cout << "\nGraph after initialization:" << endl;
graph.print();
/* Depth-first traversal */
vector<Vertex *> res = graphDFS(graph, v[0]);
cout << "\nDepth-first traversal (DFS) vertex sequence is" << endl;
cout << "\nVertex sequence from depth-first traversal (DFS):" << endl;
printVector(vetsToVals(res));
// Free memory