mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-12 08:48:50 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -6,9 +6,9 @@
|
||||
|
||||
const { Vertex } = require('../modules/Vertex');
|
||||
|
||||
/* Undirected graph class based on adjacency list */
|
||||
/* Undirected graph class implemented with an adjacency list */
|
||||
class GraphAdjList {
|
||||
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
|
||||
// Adjacency list: key is a vertex, value is the list of all its neighbors
|
||||
adjList;
|
||||
|
||||
/* Constructor */
|
||||
@@ -27,21 +27,21 @@ class GraphAdjList {
|
||||
return this.adjList.size;
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
/* Add an edge */
|
||||
addEdge(vet1, vet2) {
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
!this.adjList.has(vet2) ||
|
||||
vet1 === vet2
|
||||
) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
throw new Error('Invalid argument');
|
||||
}
|
||||
// Add edge vet1 - vet2
|
||||
this.adjList.get(vet1).push(vet2);
|
||||
this.adjList.get(vet2).push(vet1);
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
/* Remove an edge */
|
||||
removeEdge(vet1, vet2) {
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
@@ -49,28 +49,28 @@ class GraphAdjList {
|
||||
vet1 === vet2 ||
|
||||
this.adjList.get(vet1).indexOf(vet2) === -1
|
||||
) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
throw new Error('Invalid argument');
|
||||
}
|
||||
// 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 */
|
||||
/* Add a vertex */
|
||||
addVertex(vet) {
|
||||
if (this.adjList.has(vet)) return;
|
||||
// Add a new linked list in the adjacency list
|
||||
// Add a new list to the adjacency list
|
||||
this.adjList.set(vet, []);
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
/* Remove a vertex */
|
||||
removeVertex(vet) {
|
||||
if (!this.adjList.has(vet)) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
throw new Error('Invalid argument');
|
||||
}
|
||||
// Remove the linked list corresponding to vertex vet in the adjacency list
|
||||
// Remove the list corresponding to vertex vet from the adjacency list
|
||||
this.adjList.delete(vet);
|
||||
// Traverse the linked lists of other vertices and remove all edges containing vet
|
||||
// Traverse the lists of the other vertices and remove all edges incident to vet
|
||||
for (const set of this.adjList.values()) {
|
||||
const index = set.indexOf(vet);
|
||||
if (index > -1) {
|
||||
@@ -79,7 +79,7 @@ class GraphAdjList {
|
||||
}
|
||||
}
|
||||
|
||||
/* Print adjacency list */
|
||||
/* Print the adjacency list */
|
||||
print() {
|
||||
console.log('Adjacency list =');
|
||||
for (const [key, value] of this.adjList) {
|
||||
@@ -93,8 +93,8 @@ class GraphAdjList {
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
/* Driver Code */
|
||||
/* Add edge */
|
||||
/* Driver code */
|
||||
/* Initialize the undirected graph */
|
||||
const v0 = new Vertex(1),
|
||||
v1 = new Vertex(3),
|
||||
v2 = new Vertex(2),
|
||||
@@ -109,31 +109,31 @@ if (require.main === module) {
|
||||
[v3, v4],
|
||||
];
|
||||
const graph = new GraphAdjList(edges);
|
||||
console.log('\nAfter initialization, graph is');
|
||||
console.log('\nGraph after initialization:');
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Vertices 1, 2 are v0, v2
|
||||
/* Add an edge */
|
||||
// Vertices 1 and 2 are v0 and v2
|
||||
graph.addEdge(v0, v2);
|
||||
console.log('\nAfter adding edge 1-2, graph is');
|
||||
console.log('\nGraph after adding edge 1-2:');
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertices 1, 3 are v0, v1
|
||||
/* Remove an edge */
|
||||
// Vertices 1 and 3 are v0 and v1
|
||||
graph.removeEdge(v0, v1);
|
||||
console.log('\nAfter removing edge 1-3, graph is');
|
||||
console.log('\nGraph after removing edge 1-3:');
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
/* Add a vertex */
|
||||
const v5 = new Vertex(6);
|
||||
graph.addVertex(v5);
|
||||
console.log('\nAfter adding vertex 6, graph is');
|
||||
console.log('\nGraph after adding vertex 6:');
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
/* Remove a vertex */
|
||||
// Vertex 3 is v1
|
||||
graph.removeVertex(v1);
|
||||
console.log('\nAfter removing vertex 3, graph is');
|
||||
console.log('\nGraph after removing vertex 3:');
|
||||
graph.print();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,21 +4,21 @@
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* Undirected graph class based on adjacency matrix */
|
||||
/* Undirected graph class implemented with an 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"
|
||||
vertices; // Vertex list: elements are vertex values, and indices are vertex indices
|
||||
adjMat; // Adjacency matrix: row and column indices correspond to vertex indices
|
||||
|
||||
/* Constructor */
|
||||
constructor(vertices, edges) {
|
||||
this.vertices = [];
|
||||
this.adjMat = [];
|
||||
// Add vertex
|
||||
// Add a 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
|
||||
// Add an edge
|
||||
// Note that the entries in edges are vertex indices, corresponding to element indices in vertices
|
||||
for (const e of edges) {
|
||||
this.addEdge(e[0], e[1]);
|
||||
}
|
||||
@@ -29,10 +29,10 @@ class GraphAdjMat {
|
||||
return this.vertices.length;
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
/* Add a vertex */
|
||||
addVertex(val) {
|
||||
const n = this.size();
|
||||
// Add the value of the new vertex to the vertex list
|
||||
// Append the new vertex value to the vertex list
|
||||
this.vertices.push(val);
|
||||
// Add a row to the adjacency matrix
|
||||
const newRow = [];
|
||||
@@ -46,55 +46,55 @@ class GraphAdjMat {
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
/* Remove a vertex */
|
||||
removeVertex(index) {
|
||||
if (index >= this.size()) {
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
throw new RangeError('Index out of bounds');
|
||||
}
|
||||
// Remove the vertex at index from the vertex list
|
||||
// Remove the vertex at index `index` from the vertex list
|
||||
this.vertices.splice(index, 1);
|
||||
|
||||
// Remove the row at index from the adjacency matrix
|
||||
// Remove row `index` from the adjacency matrix
|
||||
this.adjMat.splice(index, 1);
|
||||
// Remove the column at index from the adjacency matrix
|
||||
// Remove column `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
|
||||
/* Add an edge */
|
||||
// Parameters i and j are element indices in vertices
|
||||
addEdge(i, j) {
|
||||
// Handle index out of bounds and equality
|
||||
// Handle out-of-bounds or equal indices
|
||||
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
throw new RangeError('Index out of bounds');
|
||||
}
|
||||
// In undirected graph, adjacency matrix is symmetric about main diagonal, i.e., satisfies (i, j) === (j, i)
|
||||
// In an undirected graph, the adjacency matrix is symmetric about its main diagonal: (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
|
||||
/* Remove an edge */
|
||||
// Parameters i and j are element indices in vertices
|
||||
removeEdge(i, j) {
|
||||
// Handle index out of bounds and equality
|
||||
// Handle out-of-bounds or equal indices
|
||||
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
throw new RangeError('Index out of bounds');
|
||||
}
|
||||
this.adjMat[i][j] = 0;
|
||||
this.adjMat[j][i] = 0;
|
||||
}
|
||||
|
||||
/* Print adjacency matrix */
|
||||
/* Print the 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
|
||||
/* Driver code */
|
||||
/* Initialize the undirected graph */
|
||||
// Note that the entries in edges are vertex indices, corresponding to element indices in vertices
|
||||
const vertices = [1, 3, 2, 5, 4];
|
||||
const edges = [
|
||||
[0, 1],
|
||||
@@ -105,28 +105,28 @@ const edges = [
|
||||
[3, 4],
|
||||
];
|
||||
const graph = new GraphAdjMat(vertices, edges);
|
||||
console.log('\nAfter initialization, graph is');
|
||||
console.log('\nGraph after initialization:');
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Add vertex
|
||||
/* Add an edge */
|
||||
// Vertices 1 and 2 have indices 0 and 2, respectively
|
||||
graph.addEdge(0, 2);
|
||||
console.log('\nAfter adding edge 1-2, graph is');
|
||||
console.log('\nGraph after adding edge 1-2:');
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertices 1, 3 have indices 0, 1 respectively
|
||||
/* Remove an edge */
|
||||
// Vertices 1 and 3 have indices 0 and 1, respectively
|
||||
graph.removeEdge(0, 1);
|
||||
console.log('\nAfter removing edge 1-3, graph is');
|
||||
console.log('\nGraph after removing edge 1-3:');
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
/* Add a vertex */
|
||||
graph.addVertex(6);
|
||||
console.log('\nAfter adding vertex 6, graph is');
|
||||
console.log('\nGraph after adding vertex 6:');
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
/* Remove a vertex */
|
||||
// Vertex 3 has index 1
|
||||
graph.removeVertex(1);
|
||||
console.log('\nAfter removing vertex 3, graph is');
|
||||
console.log('\nGraph after removing vertex 3:');
|
||||
graph.print();
|
||||
|
||||
@@ -8,34 +8,34 @@ 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
|
||||
// Represent the graph with an adjacency list to access all neighbors of a given vertex
|
||||
function graphBFS(graph, startVet) {
|
||||
// Vertex traversal sequence
|
||||
const res = [];
|
||||
// Hash set for recording vertices that have been visited
|
||||
// Hash set recording visited vertices
|
||||
const visited = new Set();
|
||||
visited.add(startVet);
|
||||
// Queue used to implement BFS
|
||||
// Use a queue to implement BFS
|
||||
const que = [startVet];
|
||||
// Starting from vertex vet, loop until all vertices are visited
|
||||
// Starting from startVet, loop until all reachable vertices have been 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
|
||||
res.push(vet); // Record the visited vertex
|
||||
// Traverse all neighbors of this vertex
|
||||
for (const adjVet of graph.adjList.get(vet) ?? []) {
|
||||
if (visited.has(adjVet)) {
|
||||
continue; // Skip vertices that have been visited
|
||||
continue; // Skip vertices that have already been visited
|
||||
}
|
||||
que.push(adjVet); // Only enqueue unvisited vertices
|
||||
que.push(adjVet); // Enqueue only unvisited vertices
|
||||
visited.add(adjVet); // Mark this vertex as visited
|
||||
}
|
||||
}
|
||||
// Return vertex traversal sequence
|
||||
// Return the vertex traversal sequence
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Add edge */
|
||||
/* Driver code */
|
||||
/* Initialize the undirected graph */
|
||||
const v = Vertex.valsToVets([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
const edges = [
|
||||
[v[0], v[1]],
|
||||
@@ -52,10 +52,10 @@ const edges = [
|
||||
[v[7], v[8]],
|
||||
];
|
||||
const graph = new GraphAdjList(edges);
|
||||
console.log('\nAfter initialization, graph is');
|
||||
console.log('\nGraph after initialization:');
|
||||
graph.print();
|
||||
|
||||
/* Breadth-first traversal */
|
||||
const res = graphBFS(graph, v[0]);
|
||||
console.log('\nBreadth-first traversal (BFS) vertex sequence is');
|
||||
console.log('\nVertex sequence from breadth-first traversal (BFS):');
|
||||
console.log(Vertex.vetsToVals(res));
|
||||
|
||||
@@ -8,33 +8,33 @@ 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
|
||||
// Represent the graph with an adjacency list to access all neighbors of a given vertex
|
||||
function dfs(graph, visited, res, vet) {
|
||||
res.push(vet); // Record visited vertex
|
||||
res.push(vet); // Record the visited vertex
|
||||
visited.add(vet); // Mark this vertex as visited
|
||||
// Traverse all adjacent vertices of this vertex
|
||||
// Traverse all neighbors of this vertex
|
||||
for (const adjVet of graph.adjList.get(vet)) {
|
||||
if (visited.has(adjVet)) {
|
||||
continue; // Skip vertices that have been visited
|
||||
continue; // Skip vertices that have already been visited
|
||||
}
|
||||
// Recursively visit adjacent vertices
|
||||
// Recursively visit the neighboring vertex
|
||||
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
|
||||
// Represent the graph with an adjacency list to access all neighbors of a given vertex
|
||||
function graphDFS(graph, startVet) {
|
||||
// Vertex traversal sequence
|
||||
const res = [];
|
||||
// Hash set for recording vertices that have been visited
|
||||
// Hash set recording visited vertices
|
||||
const visited = new Set();
|
||||
dfs(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Add edge */
|
||||
/* Driver code */
|
||||
/* Initialize the undirected graph */
|
||||
const v = Vertex.valsToVets([0, 1, 2, 3, 4, 5, 6]);
|
||||
const edges = [
|
||||
[v[0], v[1]],
|
||||
@@ -45,10 +45,10 @@ const edges = [
|
||||
[v[5], v[6]],
|
||||
];
|
||||
const graph = new GraphAdjList(edges);
|
||||
console.log('\nAfter initialization, graph is');
|
||||
console.log('\nGraph after initialization:');
|
||||
graph.print();
|
||||
|
||||
/* Depth-first traversal */
|
||||
const res = graphDFS(graph, v[0]);
|
||||
console.log('\nDepth-first traversal (DFS) vertex sequence is');
|
||||
console.log('\nVertex sequence from depth-first traversal (DFS):');
|
||||
console.log(Vertex.vetsToVals(res));
|
||||
|
||||
Reference in New Issue
Block a user