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,9 +6,9 @@
namespace hello_algo.chapter_graph;
/* Undirected graph class based on adjacency list */
/* Undirected graph class implemented with an adjacency list */
public class GraphAdjList {
// 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
public Dictionary<Vertex, List<Vertex>> adjList;
/* Constructor */
@@ -27,7 +27,7 @@ public class GraphAdjList {
return adjList.Count;
}
/* Add edge */
/* Add an edge */
public void AddEdge(Vertex vet1, Vertex vet2) {
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
throw new InvalidOperationException();
@@ -36,7 +36,7 @@ public class GraphAdjList {
adjList[vet2].Add(vet1);
}
/* Remove edge */
/* Remove an edge */
public void RemoveEdge(Vertex vet1, Vertex vet2) {
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
throw new InvalidOperationException();
@@ -45,27 +45,27 @@ public class GraphAdjList {
adjList[vet2].Remove(vet1);
}
/* Add vertex */
/* Add a vertex */
public void AddVertex(Vertex vet) {
if (adjList.ContainsKey(vet))
return;
// Add a new linked list in the adjacency list
// Add a new list to the adjacency list
adjList.Add(vet, []);
}
/* Remove vertex */
/* Remove a vertex */
public void RemoveVertex(Vertex vet) {
if (!adjList.ContainsKey(vet))
throw new InvalidOperationException();
// Remove the linked list corresponding to vertex vet in the adjacency list
// Remove the list corresponding to vertex vet from the adjacency list
adjList.Remove(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
foreach (List<Vertex> list in adjList.Values) {
list.Remove(vet);
}
}
/* Print adjacency list */
/* Print the adjacency list */
public void Print() {
Console.WriteLine("Adjacency list =");
foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {
@@ -80,7 +80,7 @@ public class GraphAdjList {
public class graph_adjacency_list {
[Test]
public void Test() {
/* Add edge */
/* Initialize the undirected graph */
Vertex[] v = Vertex.ValsToVets([1, 3, 2, 5, 4]);
Vertex[][] edges =
[
@@ -92,31 +92,31 @@ public class graph_adjacency_list {
[v[3], v[4]]
];
GraphAdjList graph = new(edges);
Console.WriteLine("\nAfter initialization, graph is");
Console.WriteLine("\nGraph after initialization:");
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]);
Console.WriteLine("\nAfter adding edge 1-2, graph is");
Console.WriteLine("\nGraph after adding edge 1-2:");
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]);
Console.WriteLine("\nAfter removing edge 1-3, graph is");
Console.WriteLine("\nGraph after removing edge 1-3:");
graph.Print();
/* Add vertex */
/* Add a vertex */
Vertex v5 = new(6);
graph.AddVertex(v5);
Console.WriteLine("\nAfter adding vertex 6, graph is");
Console.WriteLine("\nGraph after adding vertex 6:");
graph.Print();
/* Remove vertex */
/* Remove a vertex */
// Vertex 3 is v[1]
graph.RemoveVertex(v[1]);
Console.WriteLine("\nAfter removing vertex 3, graph is");
Console.WriteLine("\nGraph after removing vertex 3:");
graph.Print();
}
}

View File

@@ -6,21 +6,21 @@
namespace hello_algo.chapter_graph;
/* Undirected graph class based on adjacency matrix */
/* Undirected graph class implemented with an adjacency matrix */
class GraphAdjMat {
List<int> vertices; // Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
List<List<int>> adjMat; // Adjacency matrix, where the row and column indices correspond to the "vertex index"
List<int> vertices; // Vertex list: elements are vertex values, and indices are vertex indices
List<List<int>> adjMat; // Adjacency matrix: row and column indices correspond to vertex indices
/* Constructor */
public GraphAdjMat(int[] vertices, int[][] edges) {
this.vertices = [];
this.adjMat = [];
// Add vertex
// Add a vertex
foreach (int val in 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
foreach (int[] e in edges) {
AddEdge(e[0], e[1]);
}
@@ -31,10 +31,10 @@ class GraphAdjMat {
return vertices.Count;
}
/* Add vertex */
/* Add a vertex */
public 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.Add(val);
// Add a row to the adjacency matrix
List<int> newRow = new(n);
@@ -48,42 +48,42 @@ class GraphAdjMat {
}
}
/* Remove vertex */
/* Remove a vertex */
public void RemoveVertex(int index) {
if (index >= Size())
throw new IndexOutOfRangeException();
// Remove the vertex at index from the vertex list
// Remove the vertex at index `index` from the vertex list
vertices.RemoveAt(index);
// Remove the row at index from the adjacency matrix
// Remove row `index` from the adjacency matrix
adjMat.RemoveAt(index);
// Remove the column at index from the adjacency matrix
// Remove column `index` from the adjacency matrix
foreach (List<int> row in adjMat) {
row.RemoveAt(index);
}
}
/* Add edge */
// Parameters i, j correspond to the vertices element indices
/* Add an edge */
// Parameters i and j are element indices in vertices
public 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 new IndexOutOfRangeException();
// 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
public 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 new IndexOutOfRangeException();
adjMat[i][j] = 0;
adjMat[j][i] = 0;
}
/* Print adjacency matrix */
/* Print the adjacency matrix */
public void Print() {
Console.Write("Vertex list = ");
PrintUtil.PrintList(vertices);
@@ -95,8 +95,8 @@ class GraphAdjMat {
public class graph_adjacency_matrix {
[Test]
public void Test() {
/* 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
int[] vertices = [1, 3, 2, 5, 4];
int[][] edges =
[
@@ -108,30 +108,30 @@ public class graph_adjacency_matrix {
[3, 4]
];
GraphAdjMat graph = new(vertices, edges);
Console.WriteLine("\nAfter initialization, graph is");
Console.WriteLine("\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.WriteLine("\nAfter adding edge 1-2, graph is");
Console.WriteLine("\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.WriteLine("\nAfter removing edge 1-3, graph is");
Console.WriteLine("\nGraph after removing edge 1-3:");
graph.Print();
/* Add vertex */
/* Add a vertex */
graph.AddVertex(6);
Console.WriteLine("\nAfter adding vertex 6, graph is");
Console.WriteLine("\nGraph after adding vertex 6:");
graph.Print();
/* Remove vertex */
/* Remove a vertex */
// Vertex 3 has index 1
graph.RemoveVertex(1);
Console.WriteLine("\nAfter removing vertex 3, graph is");
Console.WriteLine("\nGraph after removing vertex 3:");
graph.Print();
}
}

View File

@@ -8,35 +8,35 @@ namespace hello_algo.chapter_graph;
public class graph_bfs {
/* 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
List<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {
// Vertex traversal sequence
List<Vertex> res = [];
// Hash set for recording vertices that have been visited
// Hash set recording visited vertices
HashSet<Vertex> visited = [startVet];
// Queue used to implement BFS
// Use a queue to implement BFS
Queue<Vertex> que = new();
que.Enqueue(startVet);
// Starting from vertex vet, loop until all vertices are visited
// Starting from startVet, loop until all reachable vertices have been visited
while (que.Count > 0) {
Vertex vet = que.Dequeue(); // Dequeue the front vertex
res.Add(vet); // Record visited vertex
res.Add(vet); // Record the visited vertex
foreach (Vertex adjVet in graph.adjList[vet]) {
if (visited.Contains(adjVet)) {
continue; // Skip vertices that have been visited
continue; // Skip vertices that have already been visited
}
que.Enqueue(adjVet); // Only enqueue unvisited vertices
que.Enqueue(adjVet); // Enqueue only unvisited vertices
visited.Add(adjVet); // Mark this vertex as visited
}
}
// Return vertex traversal sequence
// Return the vertex traversal sequence
return res;
}
[Test]
public void Test() {
/* Add edge */
/* Initialize the undirected graph */
Vertex[] v = Vertex.ValsToVets([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
Vertex[][] edges =
[
@@ -47,12 +47,12 @@ public class graph_bfs {
];
GraphAdjList graph = new(edges);
Console.WriteLine("\nAfter initialization, graph is");
Console.WriteLine("\nGraph after initialization:");
graph.Print();
/* Breadth-first traversal */
List<Vertex> res = GraphBFS(graph, v[0]);
Console.WriteLine("\nBreadth-first traversal (BFS) vertex sequence is");
Console.WriteLine("\nVertex sequence from breadth-first traversal (BFS):");
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
}
}

View File

@@ -9,24 +9,24 @@ namespace hello_algo.chapter_graph;
public class graph_dfs {
/* Depth-first traversal helper function */
void DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {
res.Add(vet); // Record visited vertex
res.Add(vet); // Record the visited vertex
visited.Add(vet); // Mark this vertex as visited
// Traverse all adjacent vertices of this vertex
// Traverse all neighbors of this vertex
foreach (Vertex adjVet in graph.adjList[vet]) {
if (visited.Contains(adjVet)) {
continue; // Skip vertices that have been visited
continue; // Skip vertices that have already been visited
}
// Recursively visit adjacent vertices
// 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
List<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {
// Vertex traversal sequence
List<Vertex> res = [];
// Hash set for recording vertices that have been visited
// Hash set recording visited vertices
HashSet<Vertex> visited = [];
DFS(graph, visited, res, startVet);
return res;
@@ -34,7 +34,7 @@ public class graph_dfs {
[Test]
public void Test() {
/* Add edge */
/* Initialize the undirected graph */
Vertex[] v = Vertex.ValsToVets([0, 1, 2, 3, 4, 5, 6]);
Vertex[][] edges =
[
@@ -43,12 +43,12 @@ public class graph_dfs {
];
GraphAdjList graph = new(edges);
Console.WriteLine("\nAfter initialization, graph is");
Console.WriteLine("\nGraph after initialization:");
graph.Print();
/* Depth-first traversal */
List<Vertex> res = GraphDFS(graph, v[0]);
Console.WriteLine("\nDepth-first traversal (DFS) vertex sequence is");
Console.WriteLine("\nVertex sequence from depth-first traversal (DFS):");
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
}
}