Re-translate the Japanese version (#1871)

* Retranslate Japanese docs with GPT-5.4

* Retranslate Japanese code with GPT-5.4
This commit is contained in:
Yudong Jin
2026-03-30 07:30:15 +08:00
committed by GitHub
parent fe6443235b
commit d7b2277d2b
1444 changed files with 83312 additions and 8363 deletions
@@ -0,0 +1,142 @@
/**
* File: graph_adjacency_list.js
* Created Time: 2023-02-09
* Author: Justin (xiefahit@gmail.com)
*/
const { Vertex } = require('../modules/Vertex');
/* 隣接リストに基づく無向グラフクラス */
class GraphAdjList {
// 隣接リスト。key は頂点、value はその頂点に隣接する全頂点
adjList;
/* コンストラクタ */
constructor(edges) {
this.adjList = new Map();
// すべての頂点と辺を追加
for (const edge of edges) {
this.addVertex(edge[0]);
this.addVertex(edge[1]);
this.addEdge(edge[0], edge[1]);
}
}
/* 頂点数を取得 */
size() {
return this.adjList.size;
}
/* 辺を追加 */
addEdge(vet1, vet2) {
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);
this.adjList.get(vet2).push(vet1);
}
/* 辺を削除 */
removeEdge(vet1, vet2) {
if (
!this.adjList.has(vet1) ||
!this.adjList.has(vet2) ||
vet1 === vet2 ||
this.adjList.get(vet1).indexOf(vet2) === -1
) {
throw new Error('Illegal Argument Exception');
}
// 辺 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);
}
/* 頂点を追加 */
addVertex(vet) {
if (this.adjList.has(vet)) return;
// 隣接リストに新しいリストを追加
this.adjList.set(vet, []);
}
/* 頂点を削除 */
removeVertex(vet) {
if (!this.adjList.has(vet)) {
throw new Error('Illegal Argument Exception');
}
// 隣接リストから頂点 vet に対応するリストを削除
this.adjList.delete(vet);
// 他の頂点のリストを走査し、vet を含むすべての辺を削除
for (const set of this.adjList.values()) {
const index = set.indexOf(vet);
if (index > -1) {
set.splice(index, 1);
}
}
}
/* 隣接リストを出力 */
print() {
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());
}
}
}
if (require.main === module) {
/* Driver Code */
/* 無向グラフを初期化 */
const v0 = new Vertex(1),
v1 = new Vertex(3),
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 graph = new GraphAdjList(edges);
console.log('\n初期化後のグラフは');
graph.print();
/* 辺を追加 */
// 頂点 1, 2 は v0, v2
graph.addEdge(v0, v2);
console.log('\n辺 1-2 を追加した後のグラフは');
graph.print();
/* 辺を削除 */
// 頂点 1, 3 は v0, v1
graph.removeEdge(v0, v1);
console.log('\n辺 1-3 を削除した後のグラフは');
graph.print();
/* 頂点を追加 */
const v5 = new Vertex(6);
graph.addVertex(v5);
console.log('\n頂点 6 を追加した後のグラフは');
graph.print();
/* 頂点を削除 */
// 頂点 3 は v1
graph.removeVertex(v1);
console.log('\n頂点 3 を削除した後のグラフは');
graph.print();
}
module.exports = {
GraphAdjList,
};
@@ -0,0 +1,132 @@
/**
* File: graph_adjacency_matrix.js
* Created Time: 2023-02-09
* Author: Zhuo Qinyue (1403450829@qq.com)
*/
/* 隣接行列に基づく無向グラフクラス */
class GraphAdjMat {
vertices; // 頂点リスト。要素は「頂点値」、インデックスは「頂点インデックス」を表す
adjMat; // 隣接行列。行・列のインデックスは「頂点インデックス」に対応
/* コンストラクタ */
constructor(vertices, edges) {
this.vertices = [];
this.adjMat = [];
// 頂点を追加
for (const val of vertices) {
this.addVertex(val);
}
// 辺を追加
// 注意:edges の各要素は頂点インデックスを表し、vertices の要素インデックスに対応する
for (const e of edges) {
this.addEdge(e[0], e[1]);
}
}
/* 頂点数を取得 */
size() {
return this.vertices.length;
}
/* 頂点を追加 */
addVertex(val) {
const n = this.size();
// 頂点リストに新しい頂点の値を追加
this.vertices.push(val);
// 隣接行列に 1 行追加
const newRow = [];
for (let j = 0; j < n; j++) {
newRow.push(0);
}
this.adjMat.push(newRow);
// 隣接行列に 1 列追加
for (const row of this.adjMat) {
row.push(0);
}
}
/* 頂点を削除 */
removeVertex(index) {
if (index >= this.size()) {
throw new RangeError('Index Out Of Bounds Exception');
}
// 頂点リストから index の頂点を削除する
this.vertices.splice(index, 1);
// 隣接行列で index 行を削除する
this.adjMat.splice(index, 1);
// 隣接行列で index 列を削除する
for (const row of this.adjMat) {
row.splice(index, 1);
}
}
/* 辺を追加 */
// 引数 i, j は vertices の要素インデックスに対応する
addEdge(i, j) {
// インデックスの範囲外と等値の処理
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
throw new RangeError('Index Out Of Bounds Exception');
}
// 無向グラフでは、隣接行列は主対角線に関して対称であり、(i, j) === (j, i) を満たす
this.adjMat[i][j] = 1;
this.adjMat[j][i] = 1;
}
/* 辺を削除 */
// 引数 i, j は vertices の要素インデックスに対応する
removeEdge(i, j) {
// インデックスの範囲外と等値の処理
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() {
console.log('頂点リスト = ', this.vertices);
console.log('隣接行列 =', this.adjMat);
}
}
/* Driver Code */
/* 無向グラフを初期化 */
// edges の要素は頂点インデックス、すなわち vertices の要素インデックスに対応する点に注意
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('\n初期化後のグラフは');
graph.print();
/* 辺を追加 */
// 頂点 1, 2 のインデックスはそれぞれ 0, 2
graph.addEdge(0, 2);
console.log('\n辺 1-2 を追加した後のグラフは');
graph.print();
/* 辺を削除 */
// 頂点 1, 3 のインデックスはそれぞれ 0, 1
graph.removeEdge(0, 1);
console.log('\n辺 1-3 を削除した後のグラフは');
graph.print();
/* 頂点を追加 */
graph.addVertex(6);
console.log('\n頂点 6 を追加した後のグラフは');
graph.print();
/* 頂点を削除 */
// 頂点 3 のインデックスは 1
graph.removeVertex(1);
console.log('\n頂点 3 を削除した後のグラフは');
graph.print();
@@ -0,0 +1,61 @@
/**
* File: graph_bfs.js
* Created Time: 2023-02-21
* Author: Zhuo Qinyue (1403450829@qq.com)
*/
const { GraphAdjList } = require('./graph_adjacency_list');
const { Vertex } = require('../modules/Vertex');
/* 幅優先探索 */
// グラフを隣接リストで表し、指定した頂点の隣接頂点をすべて取得できるようにする
function graphBFS(graph, startVet) {
// 頂点の走査順序
const res = [];
// 訪問済み頂点を記録するためのハッシュ集合
const visited = new Set();
visited.add(startVet);
// BFS の実装にキューを用いる
const que = [startVet];
// 頂点 vet を起点に、すべての頂点を訪問し終えるまで繰り返す
while (que.length) {
const vet = que.shift(); // 先頭の頂点をデキュー
res.push(vet); // 訪問した頂点を記録
// この頂点のすべての隣接頂点を走査
for (const adjVet of graph.adjList.get(vet) ?? []) {
if (visited.has(adjVet)) {
continue; // 訪問済みの頂点をスキップ
}
que.push(adjVet); // 未訪問の頂点のみをキューに追加
visited.add(adjVet); // この頂点を訪問済みにする
}
}
// 頂点の走査順を返す
return res;
}
/* 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 graph = new GraphAdjList(edges);
console.log('\n初期化後のグラフは');
graph.print();
/* 幅優先探索 */
const res = graphBFS(graph, v[0]);
console.log('\n幅優先探索(BFS)の頂点列は');
console.log(Vertex.vetsToVals(res));
@@ -0,0 +1,54 @@
/**
* File: graph_dfs.js
* Created Time: 2023-02-21
* Author: Zhuo Qinyue (1403450829@qq.com)
*/
const { Vertex } = require('../modules/Vertex');
const { GraphAdjList } = require('./graph_adjacency_list');
/* 深さ優先探索 */
// グラフを隣接リストで表し、指定した頂点の隣接頂点をすべて取得できるようにする
function dfs(graph, visited, res, vet) {
res.push(vet); // 訪問した頂点を記録
visited.add(vet); // この頂点を訪問済みにする
// この頂点のすべての隣接頂点を走査
for (const adjVet of graph.adjList.get(vet)) {
if (visited.has(adjVet)) {
continue; // 訪問済みの頂点をスキップ
}
// 隣接頂点を再帰的に訪問
dfs(graph, visited, res, adjVet);
}
}
/* 深さ優先探索 */
// グラフを隣接リストで表し、指定した頂点の隣接頂点をすべて取得できるようにする
function graphDFS(graph, startVet) {
// 頂点の走査順序
const res = [];
// 訪問済み頂点を記録するためのハッシュ集合
const visited = new Set();
dfs(graph, visited, res, startVet);
return res;
}
/* 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 graph = new GraphAdjList(edges);
console.log('\n初期化後のグラフは');
graph.print();
/* 深さ優先探索 */
const res = graphDFS(graph, v[0]);
console.log('\n深さ優先探索(DFS)の頂点列は');
console.log(Vertex.vetsToVals(res));