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,122 @@
/**
* File: graph_adjacency_list.cs
* Created Time: 2023-02-06
* Author: zjkung1123 (zjkung1123@gmail.com)
*/
namespace hello_algo.chapter_graph;
/* 隣接リストに基づく無向グラフクラス */
public class GraphAdjList {
// 隣接リスト。key は頂点、value はその頂点に隣接する全頂点
public Dictionary<Vertex, List<Vertex>> adjList;
/* コンストラクタ */
public GraphAdjList(Vertex[][] edges) {
adjList = [];
// すべての頂点と辺を追加
foreach (Vertex[] edge in edges) {
AddVertex(edge[0]);
AddVertex(edge[1]);
AddEdge(edge[0], edge[1]);
}
}
/* 頂点数を取得 */
int Size() {
return adjList.Count;
}
/* 辺を追加 */
public void AddEdge(Vertex vet1, Vertex vet2) {
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
throw new InvalidOperationException();
// 辺 vet1 - vet2 を追加
adjList[vet1].Add(vet2);
adjList[vet2].Add(vet1);
}
/* 辺を削除 */
public void RemoveEdge(Vertex vet1, Vertex vet2) {
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
throw new InvalidOperationException();
// 辺 vet1 - vet2 を削除
adjList[vet1].Remove(vet2);
adjList[vet2].Remove(vet1);
}
/* 頂点を追加 */
public void AddVertex(Vertex vet) {
if (adjList.ContainsKey(vet))
return;
// 隣接リストに新しいリストを追加
adjList.Add(vet, []);
}
/* 頂点を削除 */
public void RemoveVertex(Vertex vet) {
if (!adjList.ContainsKey(vet))
throw new InvalidOperationException();
// 隣接リストから頂点 vet に対応するリストを削除
adjList.Remove(vet);
// 他の頂点のリストを走査し、vet を含むすべての辺を削除
foreach (List<Vertex> list in adjList.Values) {
list.Remove(vet);
}
}
/* 隣接リストを出力 */
public void Print() {
Console.WriteLine("隣接リスト =");
foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {
List<int> tmp = [];
foreach (Vertex vertex in pair.Value)
tmp.Add(vertex.val);
Console.WriteLine(pair.Key.val + ": [" + string.Join(", ", tmp) + "],");
}
}
}
public class graph_adjacency_list {
[Test]
public void Test() {
/* 無向グラフを初期化 */
Vertex[] v = Vertex.ValsToVets([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(edges);
Console.WriteLine("\n初期化後、グラフは");
graph.Print();
/* 辺を追加 */
// 頂点 1, 2 は v[0], v[2]
graph.AddEdge(v[0], v[2]);
Console.WriteLine("\n辺 1-2 を追加した後、グラフは");
graph.Print();
/* 辺を削除 */
// 頂点 1, 3 は v[0], v[1]
graph.RemoveEdge(v[0], v[1]);
Console.WriteLine("\n辺 1-3 を削除した後、グラフは");
graph.Print();
/* 頂点を追加 */
Vertex v5 = new(6);
graph.AddVertex(v5);
Console.WriteLine("\n頂点 6 を追加した後、グラフは");
graph.Print();
/* 頂点を削除 */
// 頂点 3 は v[1]
graph.RemoveVertex(v[1]);
Console.WriteLine("\n頂点 3 を削除した後、グラフは");
graph.Print();
}
}
@@ -0,0 +1,137 @@
/**
* File: graph_adjacency_matrix.cs
* Created Time: 2023-02-06
* Author: zjkung1123 (zjkung1123@gmail.com)
*/
namespace hello_algo.chapter_graph;
/* 隣接行列に基づく無向グラフクラス */
class GraphAdjMat {
List<int> vertices; // 頂点リスト。要素は「頂点値」、インデックスは「頂点インデックス」を表す
List<List<int>> adjMat; // 隣接行列。行・列のインデックスは「頂点インデックス」に対応
/* コンストラクタ */
public GraphAdjMat(int[] vertices, int[][] edges) {
this.vertices = [];
this.adjMat = [];
// 頂点を追加
foreach (int val in vertices) {
AddVertex(val);
}
// 辺を追加
// 注意:edges の各要素は頂点インデックスを表し、vertices の要素インデックスに対応する
foreach (int[] e in edges) {
AddEdge(e[0], e[1]);
}
}
/* 頂点数を取得 */
int Size() {
return vertices.Count;
}
/* 頂点を追加 */
public void AddVertex(int val) {
int n = Size();
// 頂点リストに新しい頂点の値を追加
vertices.Add(val);
// 隣接行列に 1 行追加
List<int> newRow = new(n);
for (int j = 0; j < n; j++) {
newRow.Add(0);
}
adjMat.Add(newRow);
// 隣接行列に 1 列追加
foreach (List<int> row in adjMat) {
row.Add(0);
}
}
/* 頂点を削除 */
public void RemoveVertex(int index) {
if (index >= Size())
throw new IndexOutOfRangeException();
// 頂点リストから index の頂点を削除する
vertices.RemoveAt(index);
// 隣接行列で index 行を削除する
adjMat.RemoveAt(index);
// 隣接行列で index 列を削除する
foreach (List<int> row in adjMat) {
row.RemoveAt(index);
}
}
/* 辺を追加 */
// 引数 i, j は vertices の要素インデックスに対応する
public void AddEdge(int i, int j) {
// インデックスの範囲外と等値の処理
if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)
throw new IndexOutOfRangeException();
// 無向グラフでは、隣接行列は主対角線に関して対称、すなわち (i, j) == (j, i) を満たす
adjMat[i][j] = 1;
adjMat[j][i] = 1;
}
/* 辺を削除 */
// 引数 i, j は vertices の要素インデックスに対応する
public void RemoveEdge(int i, int j) {
// インデックスの範囲外と等値の処理
if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)
throw new IndexOutOfRangeException();
adjMat[i][j] = 0;
adjMat[j][i] = 0;
}
/* 隣接行列を出力 */
public void Print() {
Console.Write("頂点リスト = ");
PrintUtil.PrintList(vertices);
Console.WriteLine("隣接行列 =");
PrintUtil.PrintMatrix(adjMat);
}
}
public class graph_adjacency_matrix {
[Test]
public void Test() {
/* 無向グラフを初期化 */
// edges の要素は頂点インデックス、すなわち 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(vertices, edges);
Console.WriteLine("\n初期化後、グラフは");
graph.Print();
/* 辺を追加 */
// 頂点 1, 2 のインデックスはそれぞれ 0, 2
graph.AddEdge(0, 2);
Console.WriteLine("\n辺 1-2 を追加した後、グラフは");
graph.Print();
/* 辺を削除 */
// 頂点 1, 3 のインデックスはそれぞれ 0, 1
graph.RemoveEdge(0, 1);
Console.WriteLine("\n辺 1-3 を削除した後、グラフは");
graph.Print();
/* 頂点を追加 */
graph.AddVertex(6);
Console.WriteLine("\n頂点 6 を追加した後、グラフは");
graph.Print();
/* 頂点を削除 */
// 頂点 3 のインデックスは 1
graph.RemoveVertex(1);
Console.WriteLine("\n頂点 3 を削除した後、グラフは");
graph.Print();
}
}
@@ -0,0 +1,58 @@
/**
* File: graph_bfs.cs
* Created Time: 2023-03-08
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_graph;
public class graph_bfs {
/* 幅優先探索 */
// グラフを隣接リストで表し、指定した頂点の隣接頂点をすべて取得できるようにする
List<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {
// 頂点の走査順序
List<Vertex> res = [];
// 訪問済み頂点を記録するためのハッシュ集合
HashSet<Vertex> visited = [startVet];
// BFS の実装にキューを用いる
Queue<Vertex> que = new();
que.Enqueue(startVet);
// 頂点 vet を起点に、すべての頂点を訪問し終えるまで繰り返す
while (que.Count > 0) {
Vertex vet = que.Dequeue(); // 先頭の頂点をデキュー
res.Add(vet); // 訪問した頂点を記録
foreach (Vertex adjVet in graph.adjList[vet]) {
if (visited.Contains(adjVet)) {
continue; // 訪問済みの頂点をスキップ
}
que.Enqueue(adjVet); // 未訪問の頂点のみをキューに追加
visited.Add(adjVet); // この頂点を訪問済みにする
}
}
// 頂点の走査順を返す
return res;
}
[Test]
public void Test() {
/* 無向グラフを初期化 */
Vertex[] v = Vertex.ValsToVets([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(edges);
Console.WriteLine("\n初期化後、グラフは");
graph.Print();
/* 幅優先探索 */
List<Vertex> res = GraphBFS(graph, v[0]);
Console.WriteLine("\n幅優先探索(BFS)の頂点順序は");
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
}
}
@@ -0,0 +1,54 @@
/**
* File: graph_dfs.cs
* Created Time: 2023-03-08
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_graph;
public class graph_dfs {
/* 深さ優先走査の補助関数 */
void DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {
res.Add(vet); // 訪問した頂点を記録
visited.Add(vet); // この頂点を訪問済みにする
// この頂点のすべての隣接頂点を走査
foreach (Vertex adjVet in graph.adjList[vet]) {
if (visited.Contains(adjVet)) {
continue; // 訪問済みの頂点をスキップ
}
// 隣接頂点を再帰的に訪問
DFS(graph, visited, res, adjVet);
}
}
/* 深さ優先探索 */
// グラフを隣接リストで表し、指定した頂点の隣接頂点をすべて取得できるようにする
List<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {
// 頂点の走査順序
List<Vertex> res = [];
// 訪問済み頂点を記録するためのハッシュ集合
HashSet<Vertex> visited = [];
DFS(graph, visited, res, startVet);
return res;
}
[Test]
public void Test() {
/* 無向グラフを初期化 */
Vertex[] v = Vertex.ValsToVets([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(edges);
Console.WriteLine("\n初期化後、グラフは");
graph.Print();
/* 深さ優先探索 */
List<Vertex> res = GraphDFS(graph, v[0]);
Console.WriteLine("\n深さ優先探索(DFS)の頂点順序は");
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
}
}