mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Fomrat the JS and TS codes with prettier.
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { Vertex } = require('../modules/Vertex')
|
||||
const { Vertex } = require('../modules/Vertex');
|
||||
|
||||
/* 基于邻接表实现的无向图类 */
|
||||
class GraphAdjList {
|
||||
@@ -29,8 +29,12 @@ class GraphAdjList {
|
||||
|
||||
/* 添加边 */
|
||||
addEdge(vet1, vet2) {
|
||||
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
|
||||
throw new Error("Illegal Argument Exception");
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
!this.adjList.has(vet2) ||
|
||||
vet1 === vet2
|
||||
) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
}
|
||||
// 添加边 vet1 - vet2
|
||||
this.adjList.get(vet1).push(vet2);
|
||||
@@ -39,8 +43,12 @@ class GraphAdjList {
|
||||
|
||||
/* 删除边 */
|
||||
removeEdge(vet1, vet2) {
|
||||
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {
|
||||
throw new Error("Illegal Argument Exception");
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
!this.adjList.has(vet2) ||
|
||||
vet1 === vet2
|
||||
) {
|
||||
throw new Error('Illegal Argument Exception');
|
||||
}
|
||||
// 删除边 vet1 - vet2
|
||||
this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);
|
||||
@@ -57,7 +65,7 @@ class GraphAdjList {
|
||||
/* 删除顶点 */
|
||||
removeVertex(vet) {
|
||||
if (!this.adjList.has(vet)) {
|
||||
throw new Error("Illegal Argument Exception");
|
||||
throw new Error('Illegal Argument Exception');
|
||||
}
|
||||
// 在邻接表中删除顶点 vet 对应的链表
|
||||
this.adjList.delete(vet);
|
||||
@@ -72,13 +80,13 @@ class GraphAdjList {
|
||||
|
||||
/* 打印邻接表 */
|
||||
print() {
|
||||
console.log("邻接表 =");
|
||||
console.log('邻接表 =');
|
||||
for (const [key, value] of this.adjList) {
|
||||
const tmp = [];
|
||||
for (const vertex of value) {
|
||||
tmp.push(vertex.val);
|
||||
}
|
||||
console.log(key.val + ": " + tmp.join());
|
||||
console.log(key.val + ': ' + tmp.join());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,37 +99,43 @@ if (require.main === module) {
|
||||
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 edges = [
|
||||
[v0, v1],
|
||||
[v1, v2],
|
||||
[v2, v3],
|
||||
[v0, v3],
|
||||
[v2, v4],
|
||||
[v3, v4],
|
||||
];
|
||||
const graph = new GraphAdjList(edges);
|
||||
console.log("\n初始化后,图为");
|
||||
console.log('\n初始化后,图为');
|
||||
graph.print();
|
||||
|
||||
/* 添加边 */
|
||||
// 顶点 1, 2 即 v0, v2
|
||||
graph.addEdge(v0, v2);
|
||||
console.log("\n添加边 1-2 后,图为");
|
||||
console.log('\n添加边 1-2 后,图为');
|
||||
graph.print();
|
||||
|
||||
/* 删除边 */
|
||||
// 顶点 1, 3 即 v0, v1
|
||||
graph.removeEdge(v0, v1);
|
||||
console.log("\n删除边 1-3 后,图为");
|
||||
console.log('\n删除边 1-3 后,图为');
|
||||
graph.print();
|
||||
|
||||
/* 添加顶点 */
|
||||
const v5 = new Vertex(6);
|
||||
graph.addVertex(v5);
|
||||
console.log("\n添加顶点 6 后,图为");
|
||||
console.log('\n添加顶点 6 后,图为');
|
||||
graph.print();
|
||||
|
||||
/* 删除顶点 */
|
||||
// 顶点 3 即 v1
|
||||
graph.removeVertex(v1);
|
||||
console.log("\n删除顶点 3 后,图为");
|
||||
console.log('\n删除顶点 3 后,图为');
|
||||
graph.print();
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
GraphAdjList
|
||||
GraphAdjList,
|
||||
};
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
|
||||
/* 基于邻接矩阵实现的无向图类 */
|
||||
class GraphAdjMat {
|
||||
vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
adjMat; // 邻接矩阵,行列索引对应“顶点索引”
|
||||
vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
adjMat; // 邻接矩阵,行列索引对应“顶点索引”
|
||||
|
||||
/* 构造函数 */
|
||||
constructor(vertices, edges) {
|
||||
@@ -49,7 +49,7 @@ class GraphAdjMat {
|
||||
/* 删除顶点 */
|
||||
removeVertex(index) {
|
||||
if (index >= this.size()) {
|
||||
throw new RangeError("Index Out Of Bounds Exception");
|
||||
throw new RangeError('Index Out Of Bounds Exception');
|
||||
}
|
||||
// 在顶点列表中移除索引 index 的顶点
|
||||
this.vertices.splice(index, 1);
|
||||
@@ -67,7 +67,7 @@ class GraphAdjMat {
|
||||
addEdge(i, j) {
|
||||
// 索引越界与相等处理
|
||||
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 Exception');
|
||||
}
|
||||
// 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)
|
||||
this.adjMat[i][j] = 1;
|
||||
@@ -79,7 +79,7 @@ class GraphAdjMat {
|
||||
removeEdge(i, j) {
|
||||
// 索引越界与相等处理
|
||||
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 Exception');
|
||||
}
|
||||
this.adjMat[i][j] = 0;
|
||||
this.adjMat[j][i] = 0;
|
||||
@@ -87,8 +87,8 @@ class GraphAdjMat {
|
||||
|
||||
/* 打印邻接矩阵 */
|
||||
print() {
|
||||
console.log("顶点列表 = ", this.vertices);
|
||||
console.log("邻接矩阵 =", this.adjMat);
|
||||
console.log('顶点列表 = ', this.vertices);
|
||||
console.log('邻接矩阵 =', this.adjMat);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,32 +96,37 @@ class GraphAdjMat {
|
||||
/* 初始化无向图 */
|
||||
// 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引
|
||||
const vertices = [1, 3, 2, 5, 4];
|
||||
const edges = [[0, 1], [1, 2], [2, 3], [0, 3], [2, 4], [3, 4]
|
||||
const edges = [
|
||||
[0, 1],
|
||||
[1, 2],
|
||||
[2, 3],
|
||||
[0, 3],
|
||||
[2, 4],
|
||||
[3, 4],
|
||||
];
|
||||
const graph = new GraphAdjMat(vertices, edges);
|
||||
console.log("\n初始化后,图为");
|
||||
console.log('\n初始化后,图为');
|
||||
graph.print();
|
||||
|
||||
/* 添加边 */
|
||||
// 顶点 1, 2 的索引分别为 0, 2
|
||||
graph.addEdge(0, 2);
|
||||
console.log("\n添加边 1-2 后,图为");
|
||||
console.log('\n添加边 1-2 后,图为');
|
||||
graph.print();
|
||||
|
||||
/* 删除边 */
|
||||
// 顶点 1, 3 的索引分别为 0, 1
|
||||
graph.removeEdge(0, 1);
|
||||
console.log("\n删除边 1-3 后,图为");
|
||||
console.log('\n删除边 1-3 后,图为');
|
||||
graph.print();
|
||||
|
||||
/* 添加顶点 */
|
||||
graph.addVertex(6);
|
||||
console.log("\n添加顶点 6 后,图为");
|
||||
console.log('\n添加顶点 6 后,图为');
|
||||
graph.print();
|
||||
|
||||
/* 删除顶点 */
|
||||
// 顶点 3 的索引为 1
|
||||
graph.removeVertex(1);
|
||||
console.log("\n删除顶点 3 后,图为");
|
||||
console.log('\n删除顶点 3 后,图为');
|
||||
graph.print();
|
||||
|
||||
|
||||
@@ -19,15 +19,15 @@ function graphBFS(graph, startVet) {
|
||||
const que = [startVet];
|
||||
// 以顶点 vet 为起点,循环直至访问完所有顶点
|
||||
while (que.length) {
|
||||
const vet = que.shift(); // 队首顶点出队
|
||||
res.push(vet); // 记录访问顶点
|
||||
const vet = que.shift(); // 队首顶点出队
|
||||
res.push(vet); // 记录访问顶点
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
for (const adjVet of graph.adjList.get(vet) ?? []) {
|
||||
if (visited.has(adjVet)) {
|
||||
continue; // 跳过已被访问过的顶点
|
||||
continue; // 跳过已被访问过的顶点
|
||||
}
|
||||
que.push(adjVet); // 只入队未访问的顶点
|
||||
visited.add(adjVet); // 标记该顶点已被访问
|
||||
que.push(adjVet); // 只入队未访问的顶点
|
||||
visited.add(adjVet); // 标记该顶点已被访问
|
||||
}
|
||||
}
|
||||
// 返回顶点遍历序列
|
||||
@@ -37,14 +37,25 @@ function graphBFS(graph, startVet) {
|
||||
/* Driver Code */
|
||||
/* 初始化无向图 */
|
||||
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 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("\n初始化后,图为");
|
||||
console.log('\n初始化后,图为');
|
||||
graph.print();
|
||||
|
||||
/* 广度优先遍历 BFS */
|
||||
const res = graphBFS(graph, v[0]);
|
||||
console.log("\n广度优先遍历(BFS)顶点序列为");
|
||||
console.log('\n广度优先遍历(BFS)顶点序列为');
|
||||
console.log(Vertex.vetsToVals(res));
|
||||
|
||||
@@ -10,8 +10,8 @@ const { GraphAdjList } = require('./graph_adjacency_list');
|
||||
/* 深度优先遍历 DFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
function dfs(graph, visited, res, vet) {
|
||||
res.push(vet); // 记录访问顶点
|
||||
visited.add(vet); // 标记该顶点已被访问
|
||||
res.push(vet); // 记录访问顶点
|
||||
visited.add(vet); // 标记该顶点已被访问
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
for (const adjVet of graph.adjList.get(vet)) {
|
||||
if (visited.has(adjVet)) {
|
||||
@@ -36,13 +36,19 @@ function graphDFS(graph, startVet) {
|
||||
/* Driver Code */
|
||||
/* 初始化无向图 */
|
||||
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 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("\n初始化后,图为");
|
||||
console.log('\n初始化后,图为');
|
||||
graph.print();
|
||||
|
||||
/* 深度优先遍历 DFS */
|
||||
const res = graphDFS(graph, v[0]);
|
||||
console.log("\n深度优先遍历(DFS)顶点序列为");
|
||||
console.log('\n深度优先遍历(DFS)顶点序列为');
|
||||
console.log(Vertex.vetsToVals(res));
|
||||
|
||||
Reference in New Issue
Block a user