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

@@ -4,21 +4,21 @@
* Author: Zhuo Qinyue (1403450829@qq.com)
*/
/* Undirected graph class based on adjacency matrix */
/* Undirected graph class implemented with an adjacency matrix */
class GraphAdjMat {
vertices; // Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
adjMat; // Adjacency matrix, where the row and column indices correspond to the "vertex index"
vertices; // Vertex list: elements are vertex values, and indices are vertex indices
adjMat; // Adjacency matrix: row and column indices correspond to vertex indices
/* Constructor */
constructor(vertices, edges) {
this.vertices = [];
this.adjMat = [];
// Add vertex
// Add a vertex
for (const val of vertices) {
this.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 e of edges) {
this.addEdge(e[0], e[1]);
}
@@ -29,10 +29,10 @@ class GraphAdjMat {
return this.vertices.length;
}
/* Add vertex */
/* Add a vertex */
addVertex(val) {
const n = this.size();
// Add the value of the new vertex to the vertex list
// Append the new vertex value to the vertex list
this.vertices.push(val);
// Add a row to the adjacency matrix
const newRow = [];
@@ -46,55 +46,55 @@ class GraphAdjMat {
}
}
/* Remove vertex */
/* Remove a vertex */
removeVertex(index) {
if (index >= this.size()) {
throw new RangeError('Index Out Of Bounds Exception');
throw new RangeError('Index out of bounds');
}
// Remove the vertex at index from the vertex list
// Remove the vertex at index `index` from the vertex list
this.vertices.splice(index, 1);
// Remove the row at index from the adjacency matrix
// Remove row `index` from the adjacency matrix
this.adjMat.splice(index, 1);
// Remove the column at index from the adjacency matrix
// Remove column `index` from the adjacency matrix
for (const row of this.adjMat) {
row.splice(index, 1);
}
}
/* Add edge */
// Parameters i, j correspond to the vertices element indices
/* Add an edge */
// Parameters i and j are element indices in vertices
addEdge(i, j) {
// Handle index out of bounds and equality
// Handle out-of-bounds or equal indices
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
throw new RangeError('Index Out Of Bounds Exception');
throw new RangeError('Index out of bounds');
}
// In undirected graph, adjacency matrix is symmetric about main diagonal, i.e., satisfies (i, j) === (j, i)
// In an undirected graph, the adjacency matrix is symmetric about its main diagonal: (i, j) === (j, i)
this.adjMat[i][j] = 1;
this.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
removeEdge(i, j) {
// Handle index out of bounds and equality
// Handle out-of-bounds or equal indices
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
throw new RangeError('Index Out Of Bounds Exception');
throw new RangeError('Index out of bounds');
}
this.adjMat[i][j] = 0;
this.adjMat[j][i] = 0;
}
/* Print adjacency matrix */
/* Print the adjacency matrix */
print() {
console.log('Vertex list = ', this.vertices);
console.log('Adjacency matrix =', this.adjMat);
}
}
/* Driver Code */
/* Add edge */
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
/* Driver code */
/* Initialize the undirected graph */
// Note that the entries in edges are vertex indices, corresponding to element indices in vertices
const vertices = [1, 3, 2, 5, 4];
const edges = [
[0, 1],
@@ -105,28 +105,28 @@ const edges = [
[3, 4],
];
const graph = new GraphAdjMat(vertices, edges);
console.log('\nAfter initialization, graph is');
console.log('\nGraph after initialization:');
graph.print();
/* Add edge */
// Add vertex
/* Add an edge */
// Vertices 1 and 2 have indices 0 and 2, respectively
graph.addEdge(0, 2);
console.log('\nAfter adding edge 1-2, graph is');
console.log('\nGraph after adding edge 1-2:');
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);
console.log('\nAfter removing edge 1-3, graph is');
console.log('\nGraph after removing edge 1-3:');
graph.print();
/* Add vertex */
/* Add a vertex */
graph.addVertex(6);
console.log('\nAfter adding vertex 6, graph is');
console.log('\nGraph after adding vertex 6:');
graph.print();
/* Remove vertex */
/* Remove a vertex */
// Vertex 3 has index 1
graph.removeVertex(1);
console.log('\nAfter removing vertex 3, graph is');
console.log('\nGraph after removing vertex 3:');
graph.print();