mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +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,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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user