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,100 @@
// File: graph_adjacency_list.go
// Created Time: 2023-01-31
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
"fmt"
"strconv"
"strings"
. "github.com/krahets/hello-algo/pkg"
)
/* 隣接リストに基づく無向グラフクラス */
type graphAdjList struct {
// 隣接リスト。key は頂点、value はその頂点に隣接する全頂点
adjList map[Vertex][]Vertex
}
/* コンストラクタ */
func newGraphAdjList(edges [][]Vertex) *graphAdjList {
g := &graphAdjList{
adjList: make(map[Vertex][]Vertex),
}
// すべての頂点と辺を追加
for _, edge := range edges {
g.addVertex(edge[0])
g.addVertex(edge[1])
g.addEdge(edge[0], edge[1])
}
return g
}
/* 頂点数を取得 */
func (g *graphAdjList) size() int {
return len(g.adjList)
}
/* 辺を追加 */
func (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {
_, ok1 := g.adjList[vet1]
_, ok2 := g.adjList[vet2]
if !ok1 || !ok2 || vet1 == vet2 {
panic("error")
}
// 辺 `vet1 - vet2` を追加し、無名 `struct{}` を追加する
g.adjList[vet1] = append(g.adjList[vet1], vet2)
g.adjList[vet2] = append(g.adjList[vet2], vet1)
}
/* 辺を削除 */
func (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {
_, ok1 := g.adjList[vet1]
_, ok2 := g.adjList[vet2]
if !ok1 || !ok2 || vet1 == vet2 {
panic("error")
}
// 辺 vet1 - vet2 を削除
g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)
g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)
}
/* 頂点を追加 */
func (g *graphAdjList) addVertex(vet Vertex) {
_, ok := g.adjList[vet]
if ok {
return
}
// 隣接リストに新しいリストを追加
g.adjList[vet] = make([]Vertex, 0)
}
/* 頂点を削除 */
func (g *graphAdjList) removeVertex(vet Vertex) {
_, ok := g.adjList[vet]
if !ok {
panic("error")
}
// 隣接リストから頂点 vet に対応するリストを削除
delete(g.adjList, vet)
// 他の頂点のリストを走査し、vet を含むすべての辺を削除
for v, list := range g.adjList {
g.adjList[v] = DeleteSliceElms(list, vet)
}
}
/* 隣接リストを出力 */
func (g *graphAdjList) print() {
var builder strings.Builder
fmt.Printf("隣接リスト = \n")
for k, v := range g.adjList {
builder.WriteString("\t\t" + strconv.Itoa(k.Val) + ": ")
for _, vet := range v {
builder.WriteString(strconv.Itoa(vet.Val) + " ")
}
fmt.Println(builder.String())
builder.Reset()
}
}
@@ -0,0 +1,45 @@
// File: graph_adjacency_list_test.go
// Created Time: 2023-01-31
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestGraphAdjList(t *testing.T) {
/* 無向グラフを初期化 */
v := ValsToVets([]int{1, 3, 2, 5, 4})
edges := [][]Vertex{{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]}, {v[2], v[3]}, {v[2], v[4]}, {v[3], v[4]}}
graph := newGraphAdjList(edges)
fmt.Println("初期化後、グラフは:")
graph.print()
/* 辺を追加 */
// 頂点 1, 2 は v[0], v[2]
graph.addEdge(v[0], v[2])
fmt.Println("\n辺 1-2 を追加後、グラフは")
graph.print()
/* 辺を削除 */
// 頂点 1, 3 は v[0], v[1]
graph.removeEdge(v[0], v[1])
fmt.Println("\n辺 1-3 を削除後、グラフは")
graph.print()
/* 頂点を追加 */
v5 := NewVertex(6)
graph.addVertex(v5)
fmt.Println("\n頂点 6 を追加後、グラフは")
graph.print()
/* 頂点を削除 */
// 頂点 3 は v[1]
graph.removeVertex(v[1])
fmt.Println("\n頂点 3 を削除後、グラフは")
graph.print()
}
@@ -0,0 +1,102 @@
// File: graph_adjacency_matrix.go
// Created Time: 2023-01-31
// Author: Reanon (793584285@qq.com)
package chapter_graph
import "fmt"
/* 隣接行列に基づく無向グラフクラス */
type graphAdjMat struct {
// 頂点リスト。要素は「頂点値」、インデックスは「頂点インデックス」を表す
vertices []int
// 隣接行列。行・列のインデックスは「頂点インデックス」に対応
adjMat [][]int
}
/* コンストラクタ */
func newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {
// 頂点を追加
n := len(vertices)
adjMat := make([][]int, n)
for i := range adjMat {
adjMat[i] = make([]int, n)
}
// グラフを初期化する
g := &graphAdjMat{
vertices: vertices,
adjMat: adjMat,
}
// 辺を追加
// 注意:edges の各要素は頂点インデックスを表し、vertices の要素インデックスに対応する
for i := range edges {
g.addEdge(edges[i][0], edges[i][1])
}
return g
}
/* 頂点数を取得 */
func (g *graphAdjMat) size() int {
return len(g.vertices)
}
/* 頂点を追加 */
func (g *graphAdjMat) addVertex(val int) {
n := g.size()
// 頂点リストに新しい頂点の値を追加
g.vertices = append(g.vertices, val)
// 隣接行列に 1 行追加
newRow := make([]int, n)
g.adjMat = append(g.adjMat, newRow)
// 隣接行列に 1 列追加
for i := range g.adjMat {
g.adjMat[i] = append(g.adjMat[i], 0)
}
}
/* 頂点を削除 */
func (g *graphAdjMat) removeVertex(index int) {
if index >= g.size() {
return
}
// 頂点リストから index の頂点を削除する
g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)
// 隣接行列で index 行を削除する
g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)
// 隣接行列で index 列を削除する
for i := range g.adjMat {
g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)
}
}
/* 辺を追加 */
// 引数 i, j は vertices の要素インデックスに対応する
func (g *graphAdjMat) addEdge(i, j int) {
// インデックスの範囲外と等値の処理
if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {
fmt.Errorf("%s", "Index Out Of Bounds Exception")
}
// 無向グラフでは、隣接行列は主対角線に関して対称、すなわち (i, j) == (j, i) を満たす
g.adjMat[i][j] = 1
g.adjMat[j][i] = 1
}
/* 辺を削除 */
// 引数 i, j は vertices の要素インデックスに対応する
func (g *graphAdjMat) removeEdge(i, j int) {
// インデックスの範囲外と等値の処理
if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {
fmt.Errorf("%s", "Index Out Of Bounds Exception")
}
g.adjMat[i][j] = 0
g.adjMat[j][i] = 0
}
/* 隣接行列を出力 */
func (g *graphAdjMat) print() {
fmt.Printf("\t頂点リスト = %v\n", g.vertices)
fmt.Printf("\t隣接行列 = \n")
for i := range g.adjMat {
fmt.Printf("\t\t\t%v\n", g.adjMat[i])
}
}
@@ -0,0 +1,43 @@
// File: graph_adjacency_matrix_test.go
// Created Time: 2023-01-31
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
"fmt"
"testing"
)
func TestGraphAdjMat(t *testing.T) {
/* 無向グラフを初期化 */
// edges の要素は頂点インデックス、すなわち vertices の要素インデックスに対応する点に注意
vertices := []int{1, 3, 2, 5, 4}
edges := [][]int{{0, 1}, {1, 2}, {2, 3}, {0, 3}, {2, 4}, {3, 4}}
graph := newGraphAdjMat(vertices, edges)
fmt.Println("初期化後、グラフは:")
graph.print()
/* 辺を追加 */
// 頂点 1, 2 のインデックスはそれぞれ 0, 2
graph.addEdge(0, 2)
fmt.Println("辺 1-2 を追加後、グラフは")
graph.print()
/* 辺を削除 */
// 頂点 1, 3 のインデックスはそれぞれ 0, 1
graph.removeEdge(0, 1)
fmt.Println("辺 1-3 を削除後、グラフは")
graph.print()
/* 頂点を追加 */
graph.addVertex(6)
fmt.Println("頂点 6 を追加後、グラフは")
graph.print()
/* 頂点を削除 */
// 頂点 3 のインデックスは 1
graph.removeVertex(1)
fmt.Println("頂点 3 を削除後、グラフは")
graph.print()
}
+41
View File
@@ -0,0 +1,41 @@
// File: graph_bfs.go
// Created Time: 2023-02-18
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
. "github.com/krahets/hello-algo/pkg"
)
/* 幅優先探索 */
// グラフを隣接リストで表し、指定した頂点の隣接頂点をすべて取得できるようにする
func graphBFS(g *graphAdjList, startVet Vertex) []Vertex {
// 頂点の走査順序
res := make([]Vertex, 0)
// 訪問済み頂点を記録するためのハッシュ集合
visited := make(map[Vertex]struct{})
visited[startVet] = struct{}{}
// キューは BFS の実装に用い、スライスでキューをシミュレートする
queue := make([]Vertex, 0)
queue = append(queue, startVet)
// 頂点 vet を起点に、すべての頂点を訪問し終えるまで繰り返す
for len(queue) > 0 {
// 先頭の頂点をデキュー
vet := queue[0]
queue = queue[1:]
// 訪問した頂点を記録
res = append(res, vet)
// この頂点のすべての隣接頂点を走査
for _, adjVet := range g.adjList[vet] {
_, isExist := visited[adjVet]
// 未訪問の頂点のみをキューに追加
if !isExist {
queue = append(queue, adjVet)
visited[adjVet] = struct{}{}
}
}
}
// 頂点の走査順を返す
return res
}
@@ -0,0 +1,29 @@
// File: graph_bfs_test.go
// Created Time: 2023-02-18
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestGraphBFS(t *testing.T) {
/* 無向グラフを初期化 */
vets := ValsToVets([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
edges := [][]Vertex{
{vets[0], vets[1]}, {vets[0], vets[3]}, {vets[1], vets[2]}, {vets[1], vets[4]},
{vets[2], vets[5]}, {vets[3], vets[4]}, {vets[3], vets[6]}, {vets[4], vets[5]},
{vets[4], vets[7]}, {vets[5], vets[8]}, {vets[6], vets[7]}, {vets[7], vets[8]}}
graph := newGraphAdjList(edges)
fmt.Println("初期化後、グラフは:")
graph.print()
/* 幅優先探索 */
res := graphBFS(graph, vets[0])
fmt.Println("幅優先探索(BFS)の頂点列:")
PrintSlice(VetsToVals(res))
}
+36
View File
@@ -0,0 +1,36 @@
// File: graph_dfs.go
// Created Time: 2023-02-18
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
. "github.com/krahets/hello-algo/pkg"
)
/* 深さ優先走査の補助関数 */
func dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {
// append 操作は新しい参照を返すため、元の参照を新しい slice の参照で再代入する必要がある
*res = append(*res, vet)
visited[vet] = struct{}{}
// この頂点のすべての隣接頂点を走査
for _, adjVet := range g.adjList[vet] {
_, isExist := visited[adjVet]
// 隣接頂点を再帰的に訪問
if !isExist {
dfs(g, visited, res, adjVet)
}
}
}
/* 深さ優先探索 */
// グラフを隣接リストで表し、指定した頂点の隣接頂点をすべて取得できるようにする
func graphDFS(g *graphAdjList, startVet Vertex) []Vertex {
// 頂点の走査順序
res := make([]Vertex, 0)
// 訪問済み頂点を記録するためのハッシュ集合
visited := make(map[Vertex]struct{})
dfs(g, visited, &res, startVet)
// 頂点の走査順を返す
return res
}
@@ -0,0 +1,28 @@
// File: graph_dfs_test.go
// Created Time: 2023-02-18
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestGraphDFS(t *testing.T) {
/* 無向グラフを初期化 */
vets := ValsToVets([]int{0, 1, 2, 3, 4, 5, 6})
edges := [][]Vertex{
{vets[0], vets[1]}, {vets[0], vets[3]}, {vets[1], vets[2]},
{vets[2], vets[5]}, {vets[4], vets[5]}, {vets[5], vets[6]}}
graph := newGraphAdjList(edges)
fmt.Println("初期化後、グラフは:")
graph.print()
/* 深さ優先探索 */
res := graphDFS(graph, vets[0])
fmt.Println("深さ優先探索(DFS)の頂点列:")
PrintSlice(VetsToVals(res))
}