mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 10:53:35 +08:00
* 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
133 lines
3.8 KiB
JavaScript
133 lines
3.8 KiB
JavaScript
/**
|
|
* 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();
|