mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-27 03:50:19 +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:
122
en/codes/csharp/chapter_graph/graph_adjacency_list.cs
Normal file
122
en/codes/csharp/chapter_graph/graph_adjacency_list.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* File: graph_adjacency_list.cs
|
||||
* Created Time: 2023-02-06
|
||||
* Author: zjkung1123 (zjkung1123@gmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_graph;
|
||||
|
||||
/* Undirected graph class based on adjacency list */
|
||||
public class GraphAdjList {
|
||||
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
|
||||
public Dictionary<Vertex, List<Vertex>> adjList;
|
||||
|
||||
/* Constructor */
|
||||
public GraphAdjList(Vertex[][] edges) {
|
||||
adjList = [];
|
||||
// Add all vertices and edges
|
||||
foreach (Vertex[] edge in edges) {
|
||||
AddVertex(edge[0]);
|
||||
AddVertex(edge[1]);
|
||||
AddEdge(edge[0], edge[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
int Size() {
|
||||
return adjList.Count;
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
public void AddEdge(Vertex vet1, Vertex vet2) {
|
||||
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
|
||||
throw new InvalidOperationException();
|
||||
// Add edge vet1 - vet2
|
||||
adjList[vet1].Add(vet2);
|
||||
adjList[vet2].Add(vet1);
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
public void RemoveEdge(Vertex vet1, Vertex vet2) {
|
||||
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
|
||||
throw new InvalidOperationException();
|
||||
// Remove edge vet1 - vet2
|
||||
adjList[vet1].Remove(vet2);
|
||||
adjList[vet2].Remove(vet1);
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
public void AddVertex(Vertex vet) {
|
||||
if (adjList.ContainsKey(vet))
|
||||
return;
|
||||
// Add a new linked list in the adjacency list
|
||||
adjList.Add(vet, []);
|
||||
}
|
||||
|
||||
/* Remove 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
|
||||
adjList.Remove(vet);
|
||||
// Traverse the linked lists of other vertices and remove all edges containing vet
|
||||
foreach (List<Vertex> list in adjList.Values) {
|
||||
list.Remove(vet);
|
||||
}
|
||||
}
|
||||
|
||||
/* Print adjacency list */
|
||||
public void Print() {
|
||||
Console.WriteLine("Adjacency list =");
|
||||
foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {
|
||||
List<int> tmp = [];
|
||||
foreach (Vertex vertex in pair.Value)
|
||||
tmp.Add(vertex.val);
|
||||
Console.WriteLine(pair.Key.val + ": [" + string.Join(", ", tmp) + "],");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class graph_adjacency_list {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Add edge */
|
||||
Vertex[] v = Vertex.ValsToVets([1, 3, 2, 5, 4]);
|
||||
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 = new(edges);
|
||||
Console.WriteLine("\nAfter initialization, graph is");
|
||||
graph.Print();
|
||||
|
||||
/* Add edge */
|
||||
// Vertices 1, 3 are v[0], v[1]
|
||||
graph.AddEdge(v[0], v[2]);
|
||||
Console.WriteLine("\nAfter adding edge 1-2, graph is");
|
||||
graph.Print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertex 3 is v[1]
|
||||
graph.RemoveEdge(v[0], v[1]);
|
||||
Console.WriteLine("\nAfter removing edge 1-3, graph is");
|
||||
graph.Print();
|
||||
|
||||
/* Add vertex */
|
||||
Vertex v5 = new(6);
|
||||
graph.AddVertex(v5);
|
||||
Console.WriteLine("\nAfter adding vertex 6, graph is");
|
||||
graph.Print();
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 is v[1]
|
||||
graph.RemoveVertex(v[1]);
|
||||
Console.WriteLine("\nAfter removing vertex 3, graph is");
|
||||
graph.Print();
|
||||
}
|
||||
}
|
||||
137
en/codes/csharp/chapter_graph/graph_adjacency_matrix.cs
Normal file
137
en/codes/csharp/chapter_graph/graph_adjacency_matrix.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
/**
|
||||
* File: graph_adjacency_matrix.cs
|
||||
* Created Time: 2023-02-06
|
||||
* Author: zjkung1123 (zjkung1123@gmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_graph;
|
||||
|
||||
/* Undirected graph class based on 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"
|
||||
|
||||
/* Constructor */
|
||||
public GraphAdjMat(int[] vertices, int[][] edges) {
|
||||
this.vertices = [];
|
||||
this.adjMat = [];
|
||||
// Add 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
|
||||
foreach (int[] e in edges) {
|
||||
AddEdge(e[0], e[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
int Size() {
|
||||
return vertices.Count;
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
public void AddVertex(int val) {
|
||||
int n = Size();
|
||||
// Add the value of the new vertex to the vertex list
|
||||
vertices.Add(val);
|
||||
// Add a row to the adjacency matrix
|
||||
List<int> newRow = new(n);
|
||||
for (int j = 0; j < n; j++) {
|
||||
newRow.Add(0);
|
||||
}
|
||||
adjMat.Add(newRow);
|
||||
// Add a column to the adjacency matrix
|
||||
foreach (List<int> row in adjMat) {
|
||||
row.Add(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
public void RemoveVertex(int index) {
|
||||
if (index >= Size())
|
||||
throw new IndexOutOfRangeException();
|
||||
// Remove the vertex at index from the vertex list
|
||||
vertices.RemoveAt(index);
|
||||
// Remove the row at index from the adjacency matrix
|
||||
adjMat.RemoveAt(index);
|
||||
// Remove the column at 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
|
||||
public 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 new IndexOutOfRangeException();
|
||||
// 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 the vertices element indices
|
||||
public void RemoveEdge(int i, int j) {
|
||||
// Handle index out of bounds and equality
|
||||
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 */
|
||||
public void Print() {
|
||||
Console.Write("Vertex list = ");
|
||||
PrintUtil.PrintList(vertices);
|
||||
Console.WriteLine("Adjacency matrix =");
|
||||
PrintUtil.PrintMatrix(adjMat);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
int[] vertices = [1, 3, 2, 5, 4];
|
||||
int[][] edges =
|
||||
[
|
||||
[0, 1],
|
||||
[0, 3],
|
||||
[1, 2],
|
||||
[2, 3],
|
||||
[2, 4],
|
||||
[3, 4]
|
||||
];
|
||||
GraphAdjMat graph = new(vertices, edges);
|
||||
Console.WriteLine("\nAfter initialization, graph is");
|
||||
graph.Print();
|
||||
|
||||
/* Add edge */
|
||||
// Add vertex
|
||||
graph.AddEdge(0, 2);
|
||||
Console.WriteLine("\nAfter adding edge 1-2, graph is");
|
||||
graph.Print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertices 1, 3 have indices 0, 1 respectively
|
||||
graph.RemoveEdge(0, 1);
|
||||
Console.WriteLine("\nAfter removing edge 1-3, graph is");
|
||||
graph.Print();
|
||||
|
||||
/* Add vertex */
|
||||
graph.AddVertex(6);
|
||||
Console.WriteLine("\nAfter adding vertex 6, graph is");
|
||||
graph.Print();
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 has index 1
|
||||
graph.RemoveVertex(1);
|
||||
Console.WriteLine("\nAfter removing vertex 3, graph is");
|
||||
graph.Print();
|
||||
}
|
||||
}
|
||||
58
en/codes/csharp/chapter_graph/graph_bfs.cs
Normal file
58
en/codes/csharp/chapter_graph/graph_bfs.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* File: graph_bfs.cs
|
||||
* Created Time: 2023-03-08
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
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
|
||||
List<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {
|
||||
// Vertex traversal sequence
|
||||
List<Vertex> res = [];
|
||||
// Hash set for recording vertices that have been visited
|
||||
HashSet<Vertex> visited = [startVet];
|
||||
// Queue used to implement BFS
|
||||
Queue<Vertex> que = new();
|
||||
que.Enqueue(startVet);
|
||||
// Starting from vertex vet, loop until all vertices are visited
|
||||
while (que.Count > 0) {
|
||||
Vertex vet = que.Dequeue(); // Dequeue the front vertex
|
||||
res.Add(vet); // Record visited vertex
|
||||
foreach (Vertex adjVet in graph.adjList[vet]) {
|
||||
if (visited.Contains(adjVet)) {
|
||||
continue; // Skip vertices that have been visited
|
||||
}
|
||||
que.Enqueue(adjVet); // Only enqueue unvisited vertices
|
||||
visited.Add(adjVet); // Mark this vertex as visited
|
||||
}
|
||||
}
|
||||
|
||||
// Return vertex traversal sequence
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Add edge */
|
||||
Vertex[] v = Vertex.ValsToVets([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
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 = new(edges);
|
||||
Console.WriteLine("\nAfter initialization, graph is");
|
||||
graph.Print();
|
||||
|
||||
/* Breadth-first traversal */
|
||||
List<Vertex> res = GraphBFS(graph, v[0]);
|
||||
Console.WriteLine("\nBreadth-first traversal (BFS) vertex sequence is");
|
||||
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
|
||||
}
|
||||
}
|
||||
54
en/codes/csharp/chapter_graph/graph_dfs.cs
Normal file
54
en/codes/csharp/chapter_graph/graph_dfs.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* File: graph_dfs.cs
|
||||
* Created Time: 2023-03-08
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
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
|
||||
visited.Add(vet); // Mark this vertex as visited
|
||||
// Traverse all adjacent vertices of this vertex
|
||||
foreach (Vertex adjVet in graph.adjList[vet]) {
|
||||
if (visited.Contains(adjVet)) {
|
||||
continue; // Skip vertices that have been visited
|
||||
}
|
||||
// Recursively visit adjacent vertices
|
||||
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
|
||||
List<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {
|
||||
// Vertex traversal sequence
|
||||
List<Vertex> res = [];
|
||||
// Hash set for recording vertices that have been visited
|
||||
HashSet<Vertex> visited = [];
|
||||
DFS(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Add edge */
|
||||
Vertex[] v = Vertex.ValsToVets([0, 1, 2, 3, 4, 5, 6]);
|
||||
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 = new(edges);
|
||||
Console.WriteLine("\nAfter initialization, graph is");
|
||||
graph.Print();
|
||||
|
||||
/* Depth-first traversal */
|
||||
List<Vertex> res = GraphDFS(graph, v[0]);
|
||||
Console.WriteLine("\nDepth-first traversal (DFS) vertex sequence is");
|
||||
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user