mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-14 12:40:58 +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,135 @@
|
||||
/*
|
||||
* File: graph_adjacency_list.rs
|
||||
* Created Time: 2023-07-12
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
pub use hello_algo_rust::include::{vals_to_vets, vets_to_vals, Vertex};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
/* Undirected graph type based on adjacency list */
|
||||
pub struct GraphAdjList {
|
||||
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
|
||||
pub adj_list: HashMap<Vertex, Vec<Vertex>>, // maybe HashSet<Vertex> for value part is better?
|
||||
}
|
||||
|
||||
impl GraphAdjList {
|
||||
/* Constructor */
|
||||
pub fn new(edges: Vec<[Vertex; 2]>) -> Self {
|
||||
let mut graph = GraphAdjList {
|
||||
adj_list: HashMap::new(),
|
||||
};
|
||||
// Add all vertices and edges
|
||||
for edge in edges {
|
||||
graph.add_vertex(edge[0]);
|
||||
graph.add_vertex(edge[1]);
|
||||
graph.add_edge(edge[0], edge[1]);
|
||||
}
|
||||
|
||||
graph
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
#[allow(unused)]
|
||||
pub fn size(&self) -> usize {
|
||||
self.adj_list.len()
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
pub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {
|
||||
if vet1 == vet2 {
|
||||
panic!("value error");
|
||||
}
|
||||
// Add edge vet1 - vet2
|
||||
self.adj_list.entry(vet1).or_default().push(vet2);
|
||||
self.adj_list.entry(vet2).or_default().push(vet1);
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
#[allow(unused)]
|
||||
pub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {
|
||||
if vet1 == vet2 {
|
||||
panic!("value error");
|
||||
}
|
||||
// Remove edge vet1 - vet2
|
||||
self.adj_list
|
||||
.entry(vet1)
|
||||
.and_modify(|v| v.retain(|&e| e != vet2));
|
||||
self.adj_list
|
||||
.entry(vet2)
|
||||
.and_modify(|v| v.retain(|&e| e != vet1));
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
pub fn add_vertex(&mut self, vet: Vertex) {
|
||||
if self.adj_list.contains_key(&vet) {
|
||||
return;
|
||||
}
|
||||
// Add a new linked list in the adjacency list
|
||||
self.adj_list.insert(vet, vec![]);
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
#[allow(unused)]
|
||||
pub fn remove_vertex(&mut self, vet: Vertex) {
|
||||
// Remove the linked list corresponding to vertex vet in the adjacency list
|
||||
self.adj_list.remove(&vet);
|
||||
// Traverse the linked lists of other vertices and remove all edges containing vet
|
||||
for list in self.adj_list.values_mut() {
|
||||
list.retain(|&v| v != vet);
|
||||
}
|
||||
}
|
||||
|
||||
/* Print adjacency list */
|
||||
pub fn print(&self) {
|
||||
println!("Adjacency list =");
|
||||
for (vertex, list) in &self.adj_list {
|
||||
let list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();
|
||||
println!("{}: {:?},", vertex.val, list);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
#[allow(unused)]
|
||||
fn main() {
|
||||
/* Add edge */
|
||||
let v = vals_to_vets(vec![1, 3, 2, 5, 4]);
|
||||
let edges = vec![
|
||||
[v[0], v[1]],
|
||||
[v[0], v[3]],
|
||||
[v[1], v[2]],
|
||||
[v[2], v[3]],
|
||||
[v[2], v[4]],
|
||||
[v[3], v[4]],
|
||||
];
|
||||
|
||||
let mut graph = GraphAdjList::new(edges);
|
||||
println!("\nAfter initialization, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Vertices 1, 3 are v[0], v[1]
|
||||
graph.add_edge(v[0], v[2]);
|
||||
println!("\nAfter adding edge 1-2, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertex 3 is v[1]
|
||||
graph.remove_edge(v[0], v[1]);
|
||||
println!("\nAfter removing edge 1-3, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
let v5 = Vertex { val: 6 };
|
||||
graph.add_vertex(v5);
|
||||
println!("\nAfter adding vertex 6, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 is v[1]
|
||||
graph.remove_vertex(v[1]);
|
||||
println!("\nAfter removing vertex 3, graph is");
|
||||
graph.print();
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* File: graph_adjacency_matrix.rs
|
||||
* Created Time: 2023-07-12
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
/* Undirected graph type based on adjacency matrix */
|
||||
pub struct GraphAdjMat {
|
||||
// Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
|
||||
pub vertices: Vec<i32>,
|
||||
// Adjacency matrix, where the row and column indices correspond to the "vertex index"
|
||||
pub adj_mat: Vec<Vec<i32>>,
|
||||
}
|
||||
|
||||
impl GraphAdjMat {
|
||||
/* Constructor */
|
||||
pub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {
|
||||
let mut graph = GraphAdjMat {
|
||||
vertices: vec![],
|
||||
adj_mat: vec![],
|
||||
};
|
||||
// Add vertex
|
||||
for val in vertices {
|
||||
graph.add_vertex(val);
|
||||
}
|
||||
// Add edge
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
for edge in edges {
|
||||
graph.add_edge(edge[0], edge[1])
|
||||
}
|
||||
|
||||
graph
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
pub fn size(&self) -> usize {
|
||||
self.vertices.len()
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
pub fn add_vertex(&mut self, val: i32) {
|
||||
let n = self.size();
|
||||
// Add the value of the new vertex to the vertex list
|
||||
self.vertices.push(val);
|
||||
// Add a row to the adjacency matrix
|
||||
self.adj_mat.push(vec![0; n]);
|
||||
// Add a column to the adjacency matrix
|
||||
for row in self.adj_mat.iter_mut() {
|
||||
row.push(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
pub fn remove_vertex(&mut self, index: usize) {
|
||||
if index >= self.size() {
|
||||
panic!("index error")
|
||||
}
|
||||
// Remove the vertex at index from the vertex list
|
||||
self.vertices.remove(index);
|
||||
// Remove the row at index from the adjacency matrix
|
||||
self.adj_mat.remove(index);
|
||||
// Remove the column at index from the adjacency matrix
|
||||
for row in self.adj_mat.iter_mut() {
|
||||
row.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
pub fn add_edge(&mut self, i: usize, j: usize) {
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
// Handle index out of bounds and equality
|
||||
if i >= self.size() || j >= self.size() || i == j {
|
||||
panic!("index error")
|
||||
}
|
||||
// In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)
|
||||
self.adj_mat[i][j] = 1;
|
||||
self.adj_mat[j][i] = 1;
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
pub fn remove_edge(&mut self, i: usize, j: usize) {
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
// Handle index out of bounds and equality
|
||||
if i >= self.size() || j >= self.size() || i == j {
|
||||
panic!("index error")
|
||||
}
|
||||
self.adj_mat[i][j] = 0;
|
||||
self.adj_mat[j][i] = 0;
|
||||
}
|
||||
|
||||
/* Print adjacency matrix */
|
||||
pub fn print(&self) {
|
||||
println!("Vertex list = {:?}", self.vertices);
|
||||
println!("Adjacency matrix =");
|
||||
println!("[");
|
||||
for row in &self.adj_mat {
|
||||
println!(" {:?},", row);
|
||||
}
|
||||
println!("]")
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* Add edge */
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
let vertices = vec![1, 3, 2, 5, 4];
|
||||
let edges = vec![[0, 1], [0, 3], [1, 2], [2, 3], [2, 4], [3, 4]];
|
||||
let mut graph = GraphAdjMat::new(vertices, edges);
|
||||
println!("\nAfter initialization, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Add vertex
|
||||
graph.add_edge(0, 2);
|
||||
println!("\nAfter adding edge 1-2, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertices 1, 3 have indices 0, 1 respectively
|
||||
graph.remove_edge(0, 1);
|
||||
println!("\nAfter removing edge 1-3, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
graph.add_vertex(6);
|
||||
println!("\nAfter adding vertex 6, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 has index 1
|
||||
graph.remove_vertex(1);
|
||||
println!("\nAfter removing vertex 3, graph is");
|
||||
graph.print();
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* File: graph_bfs.rs
|
||||
* Created Time: 2023-07-12
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
mod graph_adjacency_list;
|
||||
|
||||
use graph_adjacency_list::GraphAdjList;
|
||||
use graph_adjacency_list::{vals_to_vets, vets_to_vals, Vertex};
|
||||
use std::collections::{HashSet, VecDeque};
|
||||
|
||||
/* Breadth-first traversal */
|
||||
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
fn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {
|
||||
// Vertex traversal sequence
|
||||
let mut res = vec![];
|
||||
// Hash set for recording vertices that have been visited
|
||||
let mut visited = HashSet::new();
|
||||
visited.insert(start_vet);
|
||||
// Queue used to implement BFS
|
||||
let mut que = VecDeque::new();
|
||||
que.push_back(start_vet);
|
||||
// Starting from vertex vet, loop until all vertices are visited
|
||||
while let Some(vet) = que.pop_front() {
|
||||
res.push(vet); // Record visited vertex
|
||||
|
||||
// Traverse all adjacent vertices of this vertex
|
||||
if let Some(adj_vets) = graph.adj_list.get(&vet) {
|
||||
for &adj_vet in adj_vets {
|
||||
if visited.contains(&adj_vet) {
|
||||
continue; // Skip vertices that have been visited
|
||||
}
|
||||
que.push_back(adj_vet); // Only enqueue unvisited vertices
|
||||
visited.insert(adj_vet); // Mark this vertex as visited
|
||||
}
|
||||
}
|
||||
}
|
||||
// Return vertex traversal sequence
|
||||
res
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* Add edge */
|
||||
let v = vals_to_vets(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
let edges = vec![
|
||||
[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]],
|
||||
];
|
||||
let graph = GraphAdjList::new(edges);
|
||||
println!("\nAfter initialization, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Breadth-first traversal */
|
||||
let res = graph_bfs(graph, v[0]);
|
||||
println!("\nBreadth-first traversal (BFS) vertex sequence is");
|
||||
println!("{:?}", vets_to_vals(res));
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* File: graph_dfs.rs
|
||||
* Created Time: 2023-07-12
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
mod graph_adjacency_list;
|
||||
|
||||
use graph_adjacency_list::GraphAdjList;
|
||||
use graph_adjacency_list::{vals_to_vets, vets_to_vals, Vertex};
|
||||
use std::collections::HashSet;
|
||||
|
||||
/* Depth-first traversal helper function */
|
||||
fn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {
|
||||
res.push(vet); // Record visited vertex
|
||||
visited.insert(vet); // Mark this vertex as visited
|
||||
// Traverse all adjacent vertices of this vertex
|
||||
if let Some(adj_vets) = graph.adj_list.get(&vet) {
|
||||
for &adj_vet in adj_vets {
|
||||
if visited.contains(&adj_vet) {
|
||||
continue; // Skip vertices that have been visited
|
||||
}
|
||||
// Recursively visit adjacent vertices
|
||||
dfs(graph, visited, res, adj_vet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Depth-first traversal */
|
||||
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
fn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {
|
||||
// Vertex traversal sequence
|
||||
let mut res = vec![];
|
||||
// Hash set for recording vertices that have been visited
|
||||
let mut visited = HashSet::new();
|
||||
dfs(&graph, &mut visited, &mut res, start_vet);
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* Add edge */
|
||||
let v = vals_to_vets(vec![0, 1, 2, 3, 4, 5, 6]);
|
||||
let edges = vec![
|
||||
[v[0], v[1]],
|
||||
[v[0], v[3]],
|
||||
[v[1], v[2]],
|
||||
[v[2], v[5]],
|
||||
[v[4], v[5]],
|
||||
[v[5], v[6]],
|
||||
];
|
||||
let graph = GraphAdjList::new(edges);
|
||||
println!("\nAfter initialization, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Depth-first traversal */
|
||||
let res = graph_dfs(graph, v[0]);
|
||||
println!("\nDepth-first traversal (DFS) vertex sequence is");
|
||||
println!("{:?}", vets_to_vals(res));
|
||||
}
|
||||
Reference in New Issue
Block a user