mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-09 05:41:47 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -8,8 +8,8 @@
|
||||
|
||||
/* Undirected graph class based on adjacency matrix */
|
||||
class GraphAdjMat {
|
||||
vector<int> vertices; // Vertex list, elements represent "vertex value", index represents "vertex index"
|
||||
vector<vector<int>> adjMat; // Adjacency matrix, row and column indices correspond to "vertex index"
|
||||
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"
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
@@ -19,7 +19,7 @@ class GraphAdjMat {
|
||||
addVertex(val);
|
||||
}
|
||||
// Add edge
|
||||
// Edges elements represent vertex indices
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
for (const vector<int> &edge : edges) {
|
||||
addEdge(edge[0], edge[1]);
|
||||
}
|
||||
@@ -33,7 +33,7 @@ class GraphAdjMat {
|
||||
/* Add vertex */
|
||||
void addVertex(int val) {
|
||||
int n = size();
|
||||
// Add new vertex value to the vertex list
|
||||
// Add the value of the new vertex to the vertex list
|
||||
vertices.push_back(val);
|
||||
// Add a row to the adjacency matrix
|
||||
adjMat.emplace_back(vector<int>(n, 0));
|
||||
@@ -48,30 +48,30 @@ class GraphAdjMat {
|
||||
if (index >= size()) {
|
||||
throw out_of_range("Vertex does not exist");
|
||||
}
|
||||
// Remove vertex at `index` from the vertex list
|
||||
// Remove the vertex at index from the vertex list
|
||||
vertices.erase(vertices.begin() + index);
|
||||
// Remove the row at `index` from the adjacency matrix
|
||||
// Remove the row at index from the adjacency matrix
|
||||
adjMat.erase(adjMat.begin() + index);
|
||||
// Remove the column at `index` from the adjacency matrix
|
||||
// Remove the column at index from the adjacency matrix
|
||||
for (vector<int> &row : adjMat) {
|
||||
row.erase(row.begin() + index);
|
||||
}
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
// Parameters i, j correspond to vertices element indices
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
void addEdge(int i, int j) {
|
||||
// Handle index out of bounds and equality
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {
|
||||
throw out_of_range("Vertex does not exist");
|
||||
}
|
||||
// In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., satisfies (i, j) == (j, i)
|
||||
// In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)
|
||||
adjMat[i][j] = 1;
|
||||
adjMat[j][i] = 1;
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
// Parameters i, j correspond to vertices element indices
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
void removeEdge(int i, int j) {
|
||||
// Handle index out of bounds and equality
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {
|
||||
@@ -92,35 +92,35 @@ class GraphAdjMat {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize undirected graph */
|
||||
// Edges elements represent vertex indices
|
||||
/* Add edge */
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
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, the graph is" << endl;
|
||||
cout << "\nAfter initialization, graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Indices of vertices 1, 2 are 0, 2 respectively
|
||||
// Add vertex
|
||||
graph.addEdge(0, 2);
|
||||
cout << "\nAfter adding edge 1-2, the graph is" << endl;
|
||||
cout << "\nAfter adding edge 1-2, graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Indices of vertices 1, 3 are 0, 1 respectively
|
||||
// Vertices 1, 3 have indices 0, 1 respectively
|
||||
graph.removeEdge(0, 1);
|
||||
cout << "\nAfter removing edge 1-3, the graph is" << endl;
|
||||
cout << "\nAfter removing edge 1-3, graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
graph.addVertex(6);
|
||||
cout << "\nAfter adding vertex 6, the graph is" << endl;
|
||||
cout << "\nAfter adding vertex 6, graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
// Index of vertex 3 is 1
|
||||
// Vertex 3 has index 1
|
||||
graph.removeVertex(1);
|
||||
cout << "\nAfter removing vertex 3, the graph is" << endl;
|
||||
cout << "\nAfter removing vertex 3, graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user