mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +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:
142
en/codes/javascript/chapter_graph/graph_adjacency_list.js
Normal file
142
en/codes/javascript/chapter_graph/graph_adjacency_list.js
Normal file
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* File: graph_adjacency_list.js
|
||||
* Created Time: 2023-02-09
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { Vertex } = require('../modules/Vertex');
|
||||
|
||||
/* Undirected graph class based on adjacency list */
|
||||
class GraphAdjList {
|
||||
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
|
||||
adjList;
|
||||
|
||||
/* Constructor */
|
||||
constructor(edges) {
|
||||
this.adjList = new Map();
|
||||
// Add all vertices and edges
|
||||
for (const edge of edges) {
|
||||
this.addVertex(edge[0]);
|
||||
this.addVertex(edge[1]);
|
||||
this.addEdge(edge[0], edge[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
size() {
|
||||
return this.adjList.size;
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
addEdge(vet1, vet2) {
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
!this.adjList.has(vet2) ||
|
||||
vet1 === vet2
|
||||
) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
}
|
||||
// Add edge vet1 - vet2
|
||||
this.adjList.get(vet1).push(vet2);
|
||||
this.adjList.get(vet2).push(vet1);
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
removeEdge(vet1, vet2) {
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
!this.adjList.has(vet2) ||
|
||||
vet1 === vet2 ||
|
||||
this.adjList.get(vet1).indexOf(vet2) === -1
|
||||
) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
}
|
||||
// Remove edge vet1 - vet2
|
||||
this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);
|
||||
this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
addVertex(vet) {
|
||||
if (this.adjList.has(vet)) return;
|
||||
// Add a new linked list in the adjacency list
|
||||
this.adjList.set(vet, []);
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
removeVertex(vet) {
|
||||
if (!this.adjList.has(vet)) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
}
|
||||
// Remove the linked list corresponding to vertex vet in the adjacency list
|
||||
this.adjList.delete(vet);
|
||||
// Traverse the linked lists of other vertices and remove all edges containing vet
|
||||
for (const set of this.adjList.values()) {
|
||||
const index = set.indexOf(vet);
|
||||
if (index > -1) {
|
||||
set.splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Print adjacency list */
|
||||
print() {
|
||||
console.log('Adjacency list =');
|
||||
for (const [key, value] of this.adjList) {
|
||||
const tmp = [];
|
||||
for (const vertex of value) {
|
||||
tmp.push(vertex.val);
|
||||
}
|
||||
console.log(key.val + ': ' + tmp.join());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
/* Driver Code */
|
||||
/* Add edge */
|
||||
const v0 = new Vertex(1),
|
||||
v1 = new Vertex(3),
|
||||
v2 = new Vertex(2),
|
||||
v3 = new Vertex(5),
|
||||
v4 = new Vertex(4);
|
||||
const edges = [
|
||||
[v0, v1],
|
||||
[v1, v2],
|
||||
[v2, v3],
|
||||
[v0, v3],
|
||||
[v2, v4],
|
||||
[v3, v4],
|
||||
];
|
||||
const graph = new GraphAdjList(edges);
|
||||
console.log('\nAfter initialization, graph is');
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Vertices 1, 2 are v0, v2
|
||||
graph.addEdge(v0, v2);
|
||||
console.log('\nAfter adding edge 1-2, graph is');
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertices 1, 3 are v0, v1
|
||||
graph.removeEdge(v0, v1);
|
||||
console.log('\nAfter removing edge 1-3, graph is');
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
const v5 = new Vertex(6);
|
||||
graph.addVertex(v5);
|
||||
console.log('\nAfter adding vertex 6, graph is');
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 is v1
|
||||
graph.removeVertex(v1);
|
||||
console.log('\nAfter removing vertex 3, graph is');
|
||||
graph.print();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
GraphAdjList,
|
||||
};
|
||||
132
en/codes/javascript/chapter_graph/graph_adjacency_matrix.js
Normal file
132
en/codes/javascript/chapter_graph/graph_adjacency_matrix.js
Normal file
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* File: graph_adjacency_matrix.js
|
||||
* Created Time: 2023-02-09
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* Undirected graph class based on adjacency matrix */
|
||||
class GraphAdjMat {
|
||||
vertices; // Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
|
||||
adjMat; // Adjacency matrix, where the row and column indices correspond to the "vertex index"
|
||||
|
||||
/* Constructor */
|
||||
constructor(vertices, edges) {
|
||||
this.vertices = [];
|
||||
this.adjMat = [];
|
||||
// Add vertex
|
||||
for (const val of vertices) {
|
||||
this.addVertex(val);
|
||||
}
|
||||
// Add edge
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
for (const e of edges) {
|
||||
this.addEdge(e[0], e[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
size() {
|
||||
return this.vertices.length;
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
addVertex(val) {
|
||||
const n = this.size();
|
||||
// Add the value of the new vertex to the vertex list
|
||||
this.vertices.push(val);
|
||||
// Add a row to the adjacency matrix
|
||||
const newRow = [];
|
||||
for (let j = 0; j < n; j++) {
|
||||
newRow.push(0);
|
||||
}
|
||||
this.adjMat.push(newRow);
|
||||
// Add a column to the adjacency matrix
|
||||
for (const row of this.adjMat) {
|
||||
row.push(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
removeVertex(index) {
|
||||
if (index >= this.size()) {
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
}
|
||||
// Remove the vertex at index from the vertex list
|
||||
this.vertices.splice(index, 1);
|
||||
|
||||
// Remove the row at index from the adjacency matrix
|
||||
this.adjMat.splice(index, 1);
|
||||
// Remove the column at index from the adjacency matrix
|
||||
for (const row of this.adjMat) {
|
||||
row.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
addEdge(i, j) {
|
||||
// Handle index out of bounds and equality
|
||||
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
}
|
||||
// In undirected graph, adjacency matrix is symmetric about main diagonal, i.e., satisfies (i, j) === (j, i)
|
||||
this.adjMat[i][j] = 1;
|
||||
this.adjMat[j][i] = 1;
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
removeEdge(i, j) {
|
||||
// Handle index out of bounds and equality
|
||||
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
}
|
||||
this.adjMat[i][j] = 0;
|
||||
this.adjMat[j][i] = 0;
|
||||
}
|
||||
|
||||
/* Print adjacency matrix */
|
||||
print() {
|
||||
console.log('Vertex list = ', this.vertices);
|
||||
console.log('Adjacency matrix =', this.adjMat);
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Add edge */
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
const vertices = [1, 3, 2, 5, 4];
|
||||
const edges = [
|
||||
[0, 1],
|
||||
[1, 2],
|
||||
[2, 3],
|
||||
[0, 3],
|
||||
[2, 4],
|
||||
[3, 4],
|
||||
];
|
||||
const graph = new GraphAdjMat(vertices, edges);
|
||||
console.log('\nAfter initialization, graph is');
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Add vertex
|
||||
graph.addEdge(0, 2);
|
||||
console.log('\nAfter adding edge 1-2, graph is');
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertices 1, 3 have indices 0, 1 respectively
|
||||
graph.removeEdge(0, 1);
|
||||
console.log('\nAfter removing edge 1-3, graph is');
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
graph.addVertex(6);
|
||||
console.log('\nAfter adding vertex 6, graph is');
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 has index 1
|
||||
graph.removeVertex(1);
|
||||
console.log('\nAfter removing vertex 3, graph is');
|
||||
graph.print();
|
||||
61
en/codes/javascript/chapter_graph/graph_bfs.js
Normal file
61
en/codes/javascript/chapter_graph/graph_bfs.js
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* File: graph_bfs.js
|
||||
* Created Time: 2023-02-21
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
const { GraphAdjList } = require('./graph_adjacency_list');
|
||||
const { Vertex } = require('../modules/Vertex');
|
||||
|
||||
/* Breadth-first traversal */
|
||||
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
function graphBFS(graph, startVet) {
|
||||
// Vertex traversal sequence
|
||||
const res = [];
|
||||
// Hash set for recording vertices that have been visited
|
||||
const visited = new Set();
|
||||
visited.add(startVet);
|
||||
// Queue used to implement BFS
|
||||
const que = [startVet];
|
||||
// Starting from vertex vet, loop until all vertices are visited
|
||||
while (que.length) {
|
||||
const vet = que.shift(); // Dequeue the front vertex
|
||||
res.push(vet); // Record visited vertex
|
||||
// Traverse all adjacent vertices of this vertex
|
||||
for (const adjVet of graph.adjList.get(vet) ?? []) {
|
||||
if (visited.has(adjVet)) {
|
||||
continue; // Skip vertices that have been visited
|
||||
}
|
||||
que.push(adjVet); // Only enqueue unvisited vertices
|
||||
visited.add(adjVet); // Mark this vertex as visited
|
||||
}
|
||||
}
|
||||
// Return vertex traversal sequence
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Add edge */
|
||||
const v = Vertex.valsToVets([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
const 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]],
|
||||
];
|
||||
const graph = new GraphAdjList(edges);
|
||||
console.log('\nAfter initialization, graph is');
|
||||
graph.print();
|
||||
|
||||
/* Breadth-first traversal */
|
||||
const res = graphBFS(graph, v[0]);
|
||||
console.log('\nBreadth-first traversal (BFS) vertex sequence is');
|
||||
console.log(Vertex.vetsToVals(res));
|
||||
54
en/codes/javascript/chapter_graph/graph_dfs.js
Normal file
54
en/codes/javascript/chapter_graph/graph_dfs.js
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* File: graph_dfs.js
|
||||
* Created Time: 2023-02-21
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
const { Vertex } = require('../modules/Vertex');
|
||||
const { GraphAdjList } = require('./graph_adjacency_list');
|
||||
|
||||
/* Depth-first traversal */
|
||||
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
function dfs(graph, visited, res, vet) {
|
||||
res.push(vet); // Record visited vertex
|
||||
visited.add(vet); // Mark this vertex as visited
|
||||
// Traverse all adjacent vertices of this vertex
|
||||
for (const adjVet of graph.adjList.get(vet)) {
|
||||
if (visited.has(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
|
||||
function graphDFS(graph, startVet) {
|
||||
// Vertex traversal sequence
|
||||
const res = [];
|
||||
// Hash set for recording vertices that have been visited
|
||||
const visited = new Set();
|
||||
dfs(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Add edge */
|
||||
const v = Vertex.valsToVets([0, 1, 2, 3, 4, 5, 6]);
|
||||
const 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]],
|
||||
];
|
||||
const graph = new GraphAdjList(edges);
|
||||
console.log('\nAfter initialization, graph is');
|
||||
graph.print();
|
||||
|
||||
/* Depth-first traversal */
|
||||
const res = graphDFS(graph, v[0]);
|
||||
console.log('\nDepth-first traversal (DFS) vertex sequence is');
|
||||
console.log(Vertex.vetsToVals(res));
|
||||
Reference in New Issue
Block a user