mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-21 07:47:14 +00: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:
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* File: graph_adjacency_list.dart
|
||||
* Created Time: 2023-05-15
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
import '../utils/vertex.dart';
|
||||
|
||||
/* Undirected graph class based on adjacency list */
|
||||
class GraphAdjList {
|
||||
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
|
||||
Map<Vertex, List<Vertex>> adjList = {};
|
||||
|
||||
/* Constructor */
|
||||
GraphAdjList(List<List<Vertex>> edges) {
|
||||
for (List<Vertex> edge in edges) {
|
||||
addVertex(edge[0]);
|
||||
addVertex(edge[1]);
|
||||
addEdge(edge[0], edge[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
int size() {
|
||||
return adjList.length;
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
void addEdge(Vertex vet1, Vertex vet2) {
|
||||
if (!adjList.containsKey(vet1) ||
|
||||
!adjList.containsKey(vet2) ||
|
||||
vet1 == vet2) {
|
||||
throw ArgumentError;
|
||||
}
|
||||
// Add edge vet1 - vet2
|
||||
adjList[vet1]!.add(vet2);
|
||||
adjList[vet2]!.add(vet1);
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
void removeEdge(Vertex vet1, Vertex vet2) {
|
||||
if (!adjList.containsKey(vet1) ||
|
||||
!adjList.containsKey(vet2) ||
|
||||
vet1 == vet2) {
|
||||
throw ArgumentError;
|
||||
}
|
||||
// Remove edge vet1 - vet2
|
||||
adjList[vet1]!.remove(vet2);
|
||||
adjList[vet2]!.remove(vet1);
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
void addVertex(Vertex vet) {
|
||||
if (adjList.containsKey(vet)) return;
|
||||
// Add a new linked list in the adjacency list
|
||||
adjList[vet] = [];
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
void removeVertex(Vertex vet) {
|
||||
if (!adjList.containsKey(vet)) {
|
||||
throw ArgumentError;
|
||||
}
|
||||
// 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
|
||||
adjList.forEach((key, value) {
|
||||
value.remove(vet);
|
||||
});
|
||||
}
|
||||
|
||||
/* Print adjacency list */
|
||||
void printAdjList() {
|
||||
print("Adjacency list =");
|
||||
adjList.forEach((key, value) {
|
||||
List<int> tmp = [];
|
||||
for (Vertex vertex in value) {
|
||||
tmp.add(vertex.val);
|
||||
}
|
||||
print("${key.val}: $tmp,");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
/* Add edge */
|
||||
List<Vertex> v = Vertex.valsToVets([1, 3, 2, 5, 4]);
|
||||
List<List<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 = GraphAdjList(edges);
|
||||
print("\nAfter initialization, graph is");
|
||||
graph.printAdjList();
|
||||
|
||||
/* Add edge */
|
||||
// Vertices 1, 3 are v[0], v[1]
|
||||
graph.addEdge(v[0], v[2]);
|
||||
print("\nAfter adding edge 1-2, graph is");
|
||||
graph.printAdjList();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertex 3 is v[1]
|
||||
graph.removeEdge(v[0], v[1]);
|
||||
print("\nAfter removing edge 1-3, graph is");
|
||||
graph.printAdjList();
|
||||
|
||||
/* Add vertex */
|
||||
Vertex v5 = Vertex(6);
|
||||
graph.addVertex(v5);
|
||||
print("\nAfter adding vertex 6, graph is");
|
||||
graph.printAdjList();
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 is v[1]
|
||||
graph.removeVertex(v[1]);
|
||||
print("\nAfter removing vertex 3, graph is");
|
||||
graph.printAdjList();
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* File: graph_adjacency_matrix.dart
|
||||
* Created Time: 2023-05-15
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
import '../utils/print_util.dart';
|
||||
|
||||
/* Undirected graph class based on adjacency matrix */
|
||||
class GraphAdjMat {
|
||||
List<int> vertices = []; // Vertex elements, elements represent "vertex values", indices represent "vertex indices"
|
||||
List<List<int>> adjMat = []; // Adjacency matrix, where the row and column indices correspond to the "vertex index"
|
||||
|
||||
/* Constructor */
|
||||
GraphAdjMat(List<int> vertices, List<List<int>> edges) {
|
||||
this.vertices = [];
|
||||
this.adjMat = [];
|
||||
// Add vertex
|
||||
for (int val in vertices) {
|
||||
addVertex(val);
|
||||
}
|
||||
// Add edge
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
for (List<int> e in edges) {
|
||||
addEdge(e[0], e[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
int size() {
|
||||
return vertices.length;
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
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 = List.filled(n, 0, growable: true);
|
||||
adjMat.add(newRow);
|
||||
// Add a column to the adjacency matrix
|
||||
for (List<int> row in adjMat) {
|
||||
row.add(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
void removeVertex(int index) {
|
||||
if (index >= size()) {
|
||||
throw IndexError;
|
||||
}
|
||||
// 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
|
||||
for (List<int> row in adjMat) {
|
||||
row.removeAt(index);
|
||||
}
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
// 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 IndexError;
|
||||
}
|
||||
// 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
|
||||
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 IndexError;
|
||||
}
|
||||
adjMat[i][j] = 0;
|
||||
adjMat[j][i] = 0;
|
||||
}
|
||||
|
||||
/* Print adjacency matrix */
|
||||
void printAdjMat() {
|
||||
print("Vertex list = $vertices");
|
||||
print("Adjacency matrix = ");
|
||||
printMatrix(adjMat);
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
/* Add edge */
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
List<int> vertices = [1, 3, 2, 5, 4];
|
||||
List<List<int>> edges = [
|
||||
[0, 1],
|
||||
[0, 3],
|
||||
[1, 2],
|
||||
[2, 3],
|
||||
[2, 4],
|
||||
[3, 4],
|
||||
];
|
||||
GraphAdjMat graph = GraphAdjMat(vertices, edges);
|
||||
print("\nAfter initialization, graph is");
|
||||
graph.printAdjMat();
|
||||
|
||||
/* Add edge */
|
||||
// Add vertex
|
||||
graph.addEdge(0, 2);
|
||||
print("\nAfter adding edge 1-2, graph is");
|
||||
graph.printAdjMat();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertices 1, 3 have indices 0, 1 respectively
|
||||
graph.removeEdge(0, 1);
|
||||
print("\nAfter removing edge 1-3, graph is");
|
||||
graph.printAdjMat();
|
||||
|
||||
/* Add vertex */
|
||||
graph.addVertex(6);
|
||||
print("\nAfter adding vertex 6, graph is");
|
||||
graph.printAdjMat();
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 has index 1
|
||||
graph.removeVertex(1);
|
||||
print("\nAfter removing vertex 3, graph is");
|
||||
graph.printAdjMat();
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* File: graph_bfs.dart
|
||||
* Created Time: 2023-05-15
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
import 'dart:collection';
|
||||
|
||||
import '../utils/vertex.dart';
|
||||
import 'graph_adjacency_list.dart';
|
||||
|
||||
/* Breadth-first traversal */
|
||||
List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
|
||||
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
// Vertex traversal sequence
|
||||
List<Vertex> res = [];
|
||||
// Hash set for recording vertices that have been visited
|
||||
Set<Vertex> visited = {};
|
||||
visited.add(startVet);
|
||||
// Queue used to implement BFS
|
||||
Queue<Vertex> que = Queue();
|
||||
que.add(startVet);
|
||||
// Starting from vertex vet, loop until all vertices are visited
|
||||
while (que.isNotEmpty) {
|
||||
Vertex vet = que.removeFirst(); // Dequeue the front vertex
|
||||
res.add(vet); // Record visited vertex
|
||||
// Traverse all adjacent vertices of this vertex
|
||||
for (Vertex adjVet in graph.adjList[vet]!) {
|
||||
if (visited.contains(adjVet)) {
|
||||
continue; // Skip vertices that have been visited
|
||||
}
|
||||
que.add(adjVet); // Only enqueue unvisited vertices
|
||||
visited.add(adjVet); // Mark this vertex as visited
|
||||
}
|
||||
}
|
||||
// Return vertex traversal sequence
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Dirver Code */
|
||||
void main() {
|
||||
/* Add edge */
|
||||
List<Vertex> v = Vertex.valsToVets([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
List<List<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 = GraphAdjList(edges);
|
||||
print("\nAfter initialization, graph is");
|
||||
graph.printAdjList();
|
||||
|
||||
/* Breadth-first traversal */
|
||||
List<Vertex> res = graphBFS(graph, v[0]);
|
||||
print("\nBreadth-first traversal (BFS) vertex sequence is");
|
||||
print(Vertex.vetsToVals(res));
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* File: graph_dfs.dart
|
||||
* Created Time: 2023-05-15
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
import '../utils/vertex.dart';
|
||||
import 'graph_adjacency_list.dart';
|
||||
|
||||
/* Depth-first traversal helper function */
|
||||
void dfs(
|
||||
GraphAdjList graph,
|
||||
Set<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
|
||||
for (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 */
|
||||
List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
|
||||
// Vertex traversal sequence
|
||||
List<Vertex> res = [];
|
||||
// Hash set for recording vertices that have been visited
|
||||
Set<Vertex> visited = {};
|
||||
dfs(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
/* Add edge */
|
||||
List<Vertex> v = Vertex.valsToVets([0, 1, 2, 3, 4, 5, 6]);
|
||||
List<List<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 = GraphAdjList(edges);
|
||||
print("\nAfter initialization, graph is");
|
||||
graph.printAdjList();
|
||||
|
||||
/* Depth-first traversal */
|
||||
List<Vertex> res = graphDFS(graph, v[0]);
|
||||
print("\nDepth-first traversal (DFS) vertex sequence is");
|
||||
print(Vertex.vetsToVals(res));
|
||||
}
|
||||
Reference in New Issue
Block a user