mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-12 00:39:48 +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:
@@ -9,9 +9,9 @@ package chapter_graph;
|
||||
import java.util.*;
|
||||
import utils.*;
|
||||
|
||||
/* 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
|
||||
Map<Vertex, List<Vertex>> adjList;
|
||||
|
||||
/* Constructor */
|
||||
@@ -30,7 +30,7 @@ class GraphAdjList {
|
||||
return adjList.size();
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
/* Add an edge */
|
||||
public void addEdge(Vertex vet1, Vertex vet2) {
|
||||
if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
|
||||
throw new IllegalArgumentException();
|
||||
@@ -39,7 +39,7 @@ class GraphAdjList {
|
||||
adjList.get(vet2).add(vet1);
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
/* Remove an edge */
|
||||
public void removeEdge(Vertex vet1, Vertex vet2) {
|
||||
if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
|
||||
throw new IllegalArgumentException();
|
||||
@@ -48,27 +48,27 @@ class GraphAdjList {
|
||||
adjList.get(vet2).remove(vet1);
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
/* Add a vertex */
|
||||
public void addVertex(Vertex vet) {
|
||||
if (adjList.containsKey(vet))
|
||||
return;
|
||||
// Add a new linked list in the adjacency list
|
||||
// Add a new list to the adjacency list
|
||||
adjList.put(vet, new ArrayList<>());
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
/* Remove a vertex */
|
||||
public void removeVertex(Vertex vet) {
|
||||
if (!adjList.containsKey(vet))
|
||||
throw new IllegalArgumentException();
|
||||
// Remove the linked list corresponding to vertex vet in the adjacency list
|
||||
// Remove the list corresponding to vertex vet from the adjacency list
|
||||
adjList.remove(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 (List<Vertex> list : adjList.values()) {
|
||||
list.remove(vet);
|
||||
}
|
||||
}
|
||||
|
||||
/* Print adjacency list */
|
||||
/* Print the adjacency list */
|
||||
public void print() {
|
||||
System.out.println("Adjacency list =");
|
||||
for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {
|
||||
@@ -82,36 +82,36 @@ class GraphAdjList {
|
||||
|
||||
public class graph_adjacency_list {
|
||||
public static void main(String[] args) {
|
||||
/* Add edge */
|
||||
/* Initialize the undirected graph */
|
||||
Vertex[] v = Vertex.valsToVets(new int[] { 1, 3, 2, 5, 4 });
|
||||
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 = new GraphAdjList(edges);
|
||||
System.out.println("\nAfter initialization, graph is");
|
||||
System.out.println("\nGraph after initialization:");
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Vertices 1, 3 are v[0], v[1]
|
||||
/* Add an edge */
|
||||
// Vertices 1 and 2 are v[0] and v[2]
|
||||
graph.addEdge(v[0], v[2]);
|
||||
System.out.println("\nAfter adding edge 1-2, graph is");
|
||||
System.out.println("\nGraph after adding edge 1-2:");
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertex 3 is v[1]
|
||||
/* Remove an edge */
|
||||
// Vertices 1 and 3 are v[0] and v[1]
|
||||
graph.removeEdge(v[0], v[1]);
|
||||
System.out.println("\nAfter removing edge 1-3, graph is");
|
||||
System.out.println("\nGraph after removing edge 1-3:");
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
/* Add a vertex */
|
||||
Vertex v5 = new Vertex(6);
|
||||
graph.addVertex(v5);
|
||||
System.out.println("\nAfter adding vertex 6, graph is");
|
||||
System.out.println("\nGraph after adding vertex 6:");
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
/* Remove a vertex */
|
||||
// Vertex 3 is v[1]
|
||||
graph.removeVertex(v[1]);
|
||||
System.out.println("\nAfter removing vertex 3, graph is");
|
||||
System.out.println("\nGraph after removing vertex 3:");
|
||||
graph.print();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,21 +9,21 @@ package chapter_graph;
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
/* Undirected graph class based on adjacency matrix */
|
||||
/* Undirected graph class implemented with an adjacency matrix */
|
||||
class GraphAdjMat {
|
||||
List<Integer> vertices; // Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
|
||||
List<List<Integer>> adjMat; // Adjacency matrix, where the row and column indices correspond to the "vertex index"
|
||||
List<Integer> vertices; // Vertex list: elements are vertex values, and indices are vertex indices
|
||||
List<List<Integer>> adjMat; // Adjacency matrix: row and column indices correspond to vertex indices
|
||||
|
||||
/* Constructor */
|
||||
public GraphAdjMat(int[] vertices, int[][] edges) {
|
||||
this.vertices = new ArrayList<>();
|
||||
this.adjMat = new ArrayList<>();
|
||||
// Add vertex
|
||||
// Add a vertex
|
||||
for (int val : vertices) {
|
||||
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 (int[] e : edges) {
|
||||
addEdge(e[0], e[1]);
|
||||
}
|
||||
@@ -34,10 +34,10 @@ class GraphAdjMat {
|
||||
return vertices.size();
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
/* Add a vertex */
|
||||
public void addVertex(int val) {
|
||||
int n = size();
|
||||
// Add the value of the new vertex to the vertex list
|
||||
// Append the new vertex value to the vertex list
|
||||
vertices.add(val);
|
||||
// Add a row to the adjacency matrix
|
||||
List<Integer> newRow = new ArrayList<>(n);
|
||||
@@ -51,42 +51,42 @@ class GraphAdjMat {
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
/* Remove a vertex */
|
||||
public void removeVertex(int index) {
|
||||
if (index >= size())
|
||||
throw new IndexOutOfBoundsException();
|
||||
// Remove the vertex at index from the vertex list
|
||||
// Remove the vertex at index `index` from the vertex list
|
||||
vertices.remove(index);
|
||||
// Remove the row at index from the adjacency matrix
|
||||
// Remove row `index` from the adjacency matrix
|
||||
adjMat.remove(index);
|
||||
// Remove the column at index from the adjacency matrix
|
||||
// Remove column `index` from the adjacency matrix
|
||||
for (List<Integer> row : adjMat) {
|
||||
row.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
/* Add an edge */
|
||||
// Parameters i and j are element indices in vertices
|
||||
public void addEdge(int i, int j) {
|
||||
// Handle index out of bounds and equality
|
||||
// Handle out-of-bounds or equal indices
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
|
||||
throw new IndexOutOfBoundsException();
|
||||
// In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)
|
||||
// For an undirected graph, the adjacency matrix is symmetric about its main diagonal: entries (i, j) and (j, i) are equal
|
||||
adjMat.get(i).set(j, 1);
|
||||
adjMat.get(j).set(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
|
||||
public void removeEdge(int i, int j) {
|
||||
// Handle index out of bounds and equality
|
||||
// Handle out-of-bounds or equal indices
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
|
||||
throw new IndexOutOfBoundsException();
|
||||
adjMat.get(i).set(j, 0);
|
||||
adjMat.get(j).set(i, 0);
|
||||
}
|
||||
|
||||
/* Print adjacency matrix */
|
||||
/* Print the adjacency matrix */
|
||||
public void print() {
|
||||
System.out.print("Vertex list = ");
|
||||
System.out.println(vertices);
|
||||
@@ -97,35 +97,35 @@ class GraphAdjMat {
|
||||
|
||||
public class graph_adjacency_matrix {
|
||||
public static void main(String[] args) {
|
||||
/* Add edge */
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
/* Initialize the undirected graph */
|
||||
// Note that the entries in edges are vertex indices, corresponding to element indices in vertices
|
||||
int[] vertices = { 1, 3, 2, 5, 4 };
|
||||
int[][] edges = { { 0, 1 }, { 0, 3 }, { 1, 2 }, { 2, 3 }, { 2, 4 }, { 3, 4 } };
|
||||
GraphAdjMat graph = new GraphAdjMat(vertices, edges);
|
||||
System.out.println("\nAfter initialization, graph is");
|
||||
System.out.println("\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);
|
||||
System.out.println("\nAfter adding edge 1-2, graph is");
|
||||
System.out.println("\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);
|
||||
System.out.println("\nAfter removing edge 1-3, graph is");
|
||||
System.out.println("\nGraph after removing edge 1-3:");
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
/* Add a vertex */
|
||||
graph.addVertex(6);
|
||||
System.out.println("\nAfter adding vertex 6, graph is");
|
||||
System.out.println("\nGraph after adding vertex 6:");
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
/* Remove a vertex */
|
||||
// Vertex 3 has index 1
|
||||
graph.removeVertex(1);
|
||||
System.out.println("\nAfter removing vertex 3, graph is");
|
||||
System.out.println("\nGraph after removing vertex 3:");
|
||||
graph.print();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,45 +11,45 @@ import utils.*;
|
||||
|
||||
public class graph_bfs {
|
||||
/* 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
|
||||
static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
|
||||
// Vertex traversal sequence
|
||||
List<Vertex> res = new ArrayList<>();
|
||||
// Hash set for recording vertices that have been visited
|
||||
// Hash set recording visited vertices
|
||||
Set<Vertex> visited = new HashSet<>();
|
||||
visited.add(startVet);
|
||||
// Queue used to implement BFS
|
||||
// Use a queue to implement BFS
|
||||
Queue<Vertex> que = new LinkedList<>();
|
||||
que.offer(startVet);
|
||||
// Starting from vertex vet, loop until all vertices are visited
|
||||
// Starting from startVet, loop until all reachable vertices have been visited
|
||||
while (!que.isEmpty()) {
|
||||
Vertex vet = que.poll(); // Dequeue the front vertex
|
||||
res.add(vet); // Record visited vertex
|
||||
// Traverse all adjacent vertices of this vertex
|
||||
res.add(vet); // Record the visited vertex
|
||||
// Traverse all neighbors of this vertex
|
||||
for (Vertex adjVet : graph.adjList.get(vet)) {
|
||||
if (visited.contains(adjVet))
|
||||
continue; // Skip vertices that have been visited
|
||||
que.offer(adjVet); // Only enqueue unvisited vertices
|
||||
continue; // Skip vertices that have already been visited
|
||||
que.offer(adjVet); // Enqueue only unvisited vertices
|
||||
visited.add(adjVet); // Mark this vertex as visited
|
||||
}
|
||||
}
|
||||
// Return vertex traversal sequence
|
||||
// Return the vertex traversal sequence
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Add edge */
|
||||
/* Initialize the undirected graph */
|
||||
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
||||
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 = new GraphAdjList(edges);
|
||||
System.out.println("\nAfter initialization, graph is");
|
||||
System.out.println("\nGraph after initialization:");
|
||||
graph.print();
|
||||
|
||||
/* Breadth-first traversal */
|
||||
List<Vertex> res = graphBFS(graph, v[0]);
|
||||
System.out.println("\nBreadth-first traversal (BFS) vertex sequence is");
|
||||
System.out.println("\nVertex sequence from breadth-first traversal (BFS):");
|
||||
System.out.println(Vertex.vetsToVals(res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,40 +12,40 @@ import utils.*;
|
||||
public class graph_dfs {
|
||||
/* Depth-first traversal helper function */
|
||||
static void dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {
|
||||
res.add(vet); // Record visited vertex
|
||||
res.add(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 (Vertex adjVet : graph.adjList.get(vet)) {
|
||||
if (visited.contains(adjVet))
|
||||
continue; // Skip vertices that have been visited
|
||||
// Recursively visit adjacent vertices
|
||||
continue; // Skip vertices that have already been visited
|
||||
// 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
|
||||
static List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
|
||||
// Vertex traversal sequence
|
||||
List<Vertex> res = new ArrayList<>();
|
||||
// Hash set for recording vertices that have been visited
|
||||
// Hash set recording visited vertices
|
||||
Set<Vertex> visited = new HashSet<>();
|
||||
dfs(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Add edge */
|
||||
/* Initialize the undirected graph */
|
||||
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6 });
|
||||
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 = new GraphAdjList(edges);
|
||||
System.out.println("\nAfter initialization, graph is");
|
||||
System.out.println("\nGraph after initialization:");
|
||||
graph.print();
|
||||
|
||||
/* Depth-first traversal */
|
||||
List<Vertex> res = graphDFS(graph, v[0]);
|
||||
System.out.println("\nDepth-first traversal (DFS) vertex sequence is");
|
||||
System.out.println("\nVertex sequence from depth-first traversal (DFS):");
|
||||
System.out.println(Vertex.vetsToVals(res));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user