mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-24 17:17: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,116 @@
|
||||
=begin
|
||||
File: graph_adjacency_list.rb
|
||||
Created Time: 2024-04-25
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
require_relative '../utils/vertex'
|
||||
|
||||
### Undirected graph class based on adjacency list ###
|
||||
class GraphAdjList
|
||||
attr_reader :adj_list
|
||||
|
||||
### Constructor ###
|
||||
def initialize(edges)
|
||||
# Adjacency list, key: vertex, value: all adjacent vertices of that vertex
|
||||
@adj_list = {}
|
||||
# Add all vertices and edges
|
||||
for edge in edges
|
||||
add_vertex(edge[0])
|
||||
add_vertex(edge[1])
|
||||
add_edge(edge[0], edge[1])
|
||||
end
|
||||
end
|
||||
|
||||
### Get number of vertices ###
|
||||
def size
|
||||
@adj_list.length
|
||||
end
|
||||
|
||||
### Add edge ###
|
||||
def add_edge(vet1, vet2)
|
||||
raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)
|
||||
|
||||
@adj_list[vet1] << vet2
|
||||
@adj_list[vet2] << vet1
|
||||
end
|
||||
|
||||
### Delete edge ###
|
||||
def remove_edge(vet1, vet2)
|
||||
raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)
|
||||
|
||||
# Remove edge vet1 - vet2
|
||||
@adj_list[vet1].delete(vet2)
|
||||
@adj_list[vet2].delete(vet1)
|
||||
end
|
||||
|
||||
### Add vertex ###
|
||||
def add_vertex(vet)
|
||||
return if @adj_list.include?(vet)
|
||||
|
||||
# Add a new linked list in the adjacency list
|
||||
@adj_list[vet] = []
|
||||
end
|
||||
|
||||
### Delete vertex ###
|
||||
def remove_vertex(vet)
|
||||
raise ArgumentError unless @adj_list.include?(vet)
|
||||
|
||||
# Remove the linked list corresponding to vertex vet in the adjacency list
|
||||
@adj_list.delete(vet)
|
||||
# Traverse the linked lists of other vertices and remove all edges containing vet
|
||||
for vertex in @adj_list
|
||||
@adj_list[vertex.first].delete(vet) if @adj_list[vertex.first].include?(vet)
|
||||
end
|
||||
end
|
||||
|
||||
### Print adjacency list ###
|
||||
def __print__
|
||||
puts 'Adjacency list ='
|
||||
for vertex in @adj_list
|
||||
tmp = @adj_list[vertex.first].map { |v| v.val }
|
||||
puts "#{vertex.first.val}: #{tmp},"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Add edge
|
||||
v = vals_to_vets([1, 3, 2, 5, 4])
|
||||
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]],
|
||||
]
|
||||
graph = GraphAdjList.new(edges)
|
||||
puts "\nAfter initialization, graph is"
|
||||
graph.__print__
|
||||
|
||||
# Add edge
|
||||
# Vertices 1, 2 are v[0], v[2]
|
||||
graph.add_edge(v[0], v[2])
|
||||
puts "\nAfter adding edge 1-2, graph is"
|
||||
graph.__print__
|
||||
|
||||
# Remove edge
|
||||
# Vertices 1, 3 are v[0], v[1]
|
||||
graph.remove_edge(v[0], v[1])
|
||||
puts "\nAfter removing edge 1-3, graph is"
|
||||
graph.__print__
|
||||
|
||||
# Add vertex
|
||||
v5 = Vertex.new(6)
|
||||
graph.add_vertex(v5)
|
||||
puts "\nAfter adding vertex 6, graph is"
|
||||
graph.__print__
|
||||
|
||||
# Remove vertex
|
||||
# Vertex 3 is v[1]
|
||||
graph.remove_vertex(v[1])
|
||||
puts "\nAfter removing vertex 3, graph is"
|
||||
graph.__print__
|
||||
end
|
||||
@@ -0,0 +1,116 @@
|
||||
=begin
|
||||
File: graph_adjacency_matrix.rb
|
||||
Created Time: 2024-04-25
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
require_relative '../utils/print_util'
|
||||
|
||||
### Undirected graph class based on adjacency matrix ###
|
||||
class GraphAdjMat
|
||||
def initialize(vertices, edges)
|
||||
### Constructor ###
|
||||
# Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
|
||||
@vertices = []
|
||||
# Adjacency matrix, where the row and column indices correspond to the "vertex index"
|
||||
@adj_mat = []
|
||||
# Add vertex
|
||||
vertices.each { |val| add_vertex(val) }
|
||||
# Add edge
|
||||
# Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
edges.each { |e| add_edge(e[0], e[1]) }
|
||||
end
|
||||
|
||||
### Get number of vertices ###
|
||||
def size
|
||||
@vertices.length
|
||||
end
|
||||
|
||||
### Add vertex ###
|
||||
def add_vertex(val)
|
||||
n = size
|
||||
# Add the value of the new vertex to the vertex list
|
||||
@vertices << val
|
||||
# Add a row to the adjacency matrix
|
||||
new_row = Array.new(n, 0)
|
||||
@adj_mat << new_row
|
||||
# Add a column to the adjacency matrix
|
||||
@adj_mat.each { |row| row << 0 }
|
||||
end
|
||||
|
||||
### Delete vertex ###
|
||||
def remove_vertex(index)
|
||||
raise IndexError if index >= size
|
||||
|
||||
# Remove the vertex at index from the vertex list
|
||||
@vertices.delete_at(index)
|
||||
# Remove the row at index from the adjacency matrix
|
||||
@adj_mat.delete_at(index)
|
||||
# Remove the column at index from the adjacency matrix
|
||||
@adj_mat.each { |row| row.delete_at(index) }
|
||||
end
|
||||
|
||||
### Add edge ###
|
||||
def add_edge(i, j)
|
||||
# Parameters i, j correspond to the vertices element indices
|
||||
# Handle index out of bounds and equality
|
||||
if i < 0 || j < 0 || i >= size || j >= size || i == j
|
||||
raise IndexError
|
||||
end
|
||||
# In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)
|
||||
@adj_mat[i][j] = 1
|
||||
@adj_mat[j][i] = 1
|
||||
end
|
||||
|
||||
### Delete edge ###
|
||||
def remove_edge(i, j)
|
||||
# Parameters i, j correspond to the vertices element indices
|
||||
# Handle index out of bounds and equality
|
||||
if i < 0 || j < 0 || i >= size || j >= size || i == j
|
||||
raise IndexError
|
||||
end
|
||||
@adj_mat[i][j] = 0
|
||||
@adj_mat[j][i] = 0
|
||||
end
|
||||
|
||||
### Print adjacency matrix ###
|
||||
def __print__
|
||||
puts "Vertex list = #{@vertices}"
|
||||
puts 'Adjacency matrix ='
|
||||
print_matrix(@adj_mat)
|
||||
end
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Add edge
|
||||
# Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
vertices = [1, 3, 2, 5, 4]
|
||||
edges = [[0, 1], [0, 3], [1, 2], [2, 3], [2, 4], [3, 4]]
|
||||
graph = GraphAdjMat.new(vertices, edges)
|
||||
puts "\nAfter initialization, graph is"
|
||||
graph.__print__
|
||||
|
||||
# Add edge
|
||||
# Add vertex
|
||||
graph.add_edge(0, 2)
|
||||
puts "\nAfter adding edge 1-2, graph is"
|
||||
graph.__print__
|
||||
|
||||
# Remove edge
|
||||
# Vertices 1, 3 have indices 0, 1 respectively
|
||||
graph.remove_edge(0, 1)
|
||||
puts "\nAfter removing edge 1-3, graph is"
|
||||
graph.__print__
|
||||
|
||||
# Add vertex
|
||||
graph.add_vertex(6)
|
||||
puts "\nAfter adding vertex 6, graph is"
|
||||
graph.__print__
|
||||
|
||||
# Remove vertex
|
||||
# Vertex 3 has index 1
|
||||
graph.remove_vertex(1)
|
||||
puts "\nAfter removing vertex 3, graph is"
|
||||
graph.__print__
|
||||
end
|
||||
@@ -0,0 +1,61 @@
|
||||
=begin
|
||||
File: graph_bfs.rb
|
||||
Created Time: 2024-04-25
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
require 'set'
|
||||
require_relative './graph_adjacency_list'
|
||||
require_relative '../utils/vertex'
|
||||
|
||||
### Breadth-first traversal ###
|
||||
def graph_bfs(graph, start_vet)
|
||||
# Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
# Vertex traversal sequence
|
||||
res = []
|
||||
# Hash set for recording vertices that have been visited
|
||||
visited = Set.new([start_vet])
|
||||
# Queue used to implement BFS
|
||||
que = [start_vet]
|
||||
# Starting from vertex vet, loop until all vertices are visited
|
||||
while que.length > 0
|
||||
vet = que.shift # Dequeue the front vertex
|
||||
res << vet # Record visited vertex
|
||||
# Traverse all adjacent vertices of this vertex
|
||||
for adj_vet in graph.adj_list[vet]
|
||||
next if visited.include?(adj_vet) # Skip vertices that have been visited
|
||||
que << adj_vet # Only enqueue unvisited vertices
|
||||
visited.add(adj_vet) # Mark this vertex as visited
|
||||
end
|
||||
end
|
||||
# Return vertex traversal sequence
|
||||
res
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Add edge
|
||||
v = vals_to_vets([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
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]],
|
||||
]
|
||||
graph = GraphAdjList.new(edges)
|
||||
puts "\nAfter initialization, graph is"
|
||||
graph.__print__
|
||||
|
||||
# Breadth-first traversal
|
||||
res = graph_bfs(graph, v.first)
|
||||
puts "\nBreadth-first traversal (BFS) vertex sequence is"
|
||||
p vets_to_vals(res)
|
||||
end
|
||||
@@ -0,0 +1,54 @@
|
||||
=begin
|
||||
File: graph_dfs.rb
|
||||
Created Time: 2024-04-25
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
require 'set'
|
||||
require_relative './graph_adjacency_list'
|
||||
require_relative '../utils/vertex'
|
||||
|
||||
### Depth-first traversal helper function ###
|
||||
def dfs(graph, visited, res, vet)
|
||||
res << vet # Record visited vertex
|
||||
visited.add(vet) # Mark this vertex as visited
|
||||
# Traverse all adjacent vertices of this vertex
|
||||
for adj_vet in graph.adj_list[vet]
|
||||
next if visited.include?(adj_vet) # Skip vertices that have been visited
|
||||
# Recursively visit adjacent vertices
|
||||
dfs(graph, visited, res, adj_vet)
|
||||
end
|
||||
end
|
||||
|
||||
### Depth-first traversal ###
|
||||
def graph_dfs(graph, start_vet)
|
||||
# Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
# Vertex traversal sequence
|
||||
res = []
|
||||
# Hash set for recording vertices that have been visited
|
||||
visited = Set.new
|
||||
dfs(graph, visited, res, start_vet)
|
||||
res
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
# Add edge
|
||||
v = vals_to_vets([0, 1, 2, 3, 4, 5, 6])
|
||||
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]],
|
||||
]
|
||||
graph = GraphAdjList.new(edges)
|
||||
puts "\nAfter initialization, graph is"
|
||||
graph.__print__
|
||||
|
||||
# Depth-first traversal
|
||||
res = graph_dfs(graph, v[0])
|
||||
puts "\nDepth-first traversal (DFS) vertex sequence is"
|
||||
p vets_to_vals(res)
|
||||
end
|
||||
Reference in New Issue
Block a user