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,121 @@
/**
* File: graph_adjacency_list.swift
* Created Time: 2023-02-01
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* */
public class GraphAdjList {
// key value
public private(set) var adjList: [Vertex: [Vertex]]
/* */
public init(edges: [[Vertex]]) {
adjList = [:]
//
for edge in edges {
addVertex(vet: edge[0])
addVertex(vet: edge[1])
addEdge(vet1: edge[0], vet2: edge[1])
}
}
/* */
public func size() -> Int {
adjList.count
}
/* */
public func addEdge(vet1: Vertex, vet2: Vertex) {
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
fatalError("引数エラー")
}
// vet1 - vet2
adjList[vet1]?.append(vet2)
adjList[vet2]?.append(vet1)
}
/* */
public func removeEdge(vet1: Vertex, vet2: Vertex) {
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
fatalError("引数エラー")
}
// vet1 - vet2
adjList[vet1]?.removeAll { $0 == vet2 }
adjList[vet2]?.removeAll { $0 == vet1 }
}
/* */
public func addVertex(vet: Vertex) {
if adjList[vet] != nil {
return
}
//
adjList[vet] = []
}
/* */
public func removeVertex(vet: Vertex) {
if adjList[vet] == nil {
fatalError("引数エラー")
}
// vet
adjList.removeValue(forKey: vet)
// vet
for key in adjList.keys {
adjList[key]?.removeAll { $0 == vet }
}
}
/* */
public func print() {
Swift.print("隣接リスト =")
for (vertex, list) in adjList {
let list = list.map { $0.val }
Swift.print("\(vertex.val): \(list),")
}
}
}
#if !TARGET
@main
enum GraphAdjacencyList {
/* Driver Code */
static func main() {
/* */
let v = Vertex.valsToVets(vals: [1, 3, 2, 5, 4])
let 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]]]
let graph = GraphAdjList(edges: edges)
print("\n初期化後のグラフ")
graph.print()
/* */
// 1, 2 v[0], v[2]
graph.addEdge(vet1: v[0], vet2: v[2])
print("\n辺 1-2 を追加後のグラフ")
graph.print()
/* */
// 1, 3 v[0], v[1]
graph.removeEdge(vet1: v[0], vet2: v[1])
print("\n辺 1-3 を削除後のグラフ")
graph.print()
/* */
let v5 = Vertex(val: 6)
graph.addVertex(vet: v5)
print("\n頂点 6 を追加後のグラフ")
graph.print()
/* */
// 3 v[1]
graph.removeVertex(vet: v[1])
print("\n頂点 3 を削除後のグラフ")
graph.print()
}
}
#endif
@@ -0,0 +1,121 @@
/**
* File: graph_adjacency_list.swift
* Created Time: 2023-02-01
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* */
public class GraphAdjList {
// key value
public private(set) var adjList: [Vertex: [Vertex]]
/* */
public init(edges: [[Vertex]]) {
adjList = [:]
//
for edge in edges {
addVertex(vet: edge[0])
addVertex(vet: edge[1])
addEdge(vet1: edge[0], vet2: edge[1])
}
}
/* */
public func size() -> Int {
adjList.count
}
/* */
public func addEdge(vet1: Vertex, vet2: Vertex) {
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
fatalError("引数エラー")
}
// vet1 - vet2
adjList[vet1]?.append(vet2)
adjList[vet2]?.append(vet1)
}
/* */
public func removeEdge(vet1: Vertex, vet2: Vertex) {
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
fatalError("引数エラー")
}
// vet1 - vet2
adjList[vet1]?.removeAll { $0 == vet2 }
adjList[vet2]?.removeAll { $0 == vet1 }
}
/* */
public func addVertex(vet: Vertex) {
if adjList[vet] != nil {
return
}
//
adjList[vet] = []
}
/* */
public func removeVertex(vet: Vertex) {
if adjList[vet] == nil {
fatalError("引数エラー")
}
// vet
adjList.removeValue(forKey: vet)
// vet
for key in adjList.keys {
adjList[key]?.removeAll { $0 == vet }
}
}
/* */
public func print() {
Swift.print("隣接リスト =")
for (vertex, list) in adjList {
let list = list.map { $0.val }
Swift.print("\(vertex.val): \(list),")
}
}
}
#if !TARGET
@main
enum GraphAdjacencyList {
/* Driver Code */
static func main() {
/* */
let v = Vertex.valsToVets(vals: [1, 3, 2, 5, 4])
let 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]]]
let graph = GraphAdjList(edges: edges)
print("\n初期化後のグラフ")
graph.print()
/* */
// 1, 2 v[0], v[2]
graph.addEdge(vet1: v[0], vet2: v[2])
print("\n辺 1-2 を追加後のグラフ")
graph.print()
/* */
// 1, 3 v[0], v[1]
graph.removeEdge(vet1: v[0], vet2: v[1])
print("\n辺 1-3 を削除後のグラフ")
graph.print()
/* */
let v5 = Vertex(val: 6)
graph.addVertex(vet: v5)
print("\n頂点 6 を追加後のグラフ")
graph.print()
/* */
// 3 v[1]
graph.removeVertex(vet: v[1])
print("\n頂点 3 を削除後のグラフ")
graph.print()
}
}
#endif
@@ -0,0 +1,130 @@
/**
* File: graph_adjacency_matrix.swift
* Created Time: 2023-02-01
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* */
class GraphAdjMat {
private var vertices: [Int] //
private var adjMat: [[Int]] //
/* */
init(vertices: [Int], edges: [[Int]]) {
self.vertices = []
adjMat = []
//
for val in vertices {
addVertex(val: val)
}
//
// edges vertices
for e in edges {
addEdge(i: e[0], j: e[1])
}
}
/* */
func size() -> Int {
vertices.count
}
/* */
func addVertex(val: Int) {
let n = size()
//
vertices.append(val)
// 1
let newRow = Array(repeating: 0, count: n)
adjMat.append(newRow)
// 1
for i in adjMat.indices {
adjMat[i].append(0)
}
}
/* */
func removeVertex(index: Int) {
if index >= size() {
fatalError("範囲外")
}
// index
vertices.remove(at: index)
// index
adjMat.remove(at: index)
// index
for i in adjMat.indices {
adjMat[i].remove(at: index)
}
}
/* */
// i, j vertices
func addEdge(i: Int, j: Int) {
//
if i < 0 || j < 0 || i >= size() || j >= size() || i == j {
fatalError("範囲外")
}
// (i, j) == (j, i)
adjMat[i][j] = 1
adjMat[j][i] = 1
}
/* */
// i, j vertices
func removeEdge(i: Int, j: Int) {
//
if i < 0 || j < 0 || i >= size() || j >= size() || i == j {
fatalError("範囲外")
}
adjMat[i][j] = 0
adjMat[j][i] = 0
}
/* */
func print() {
Swift.print("頂点リスト = ", terminator: "")
Swift.print(vertices)
Swift.print("隣接行列 =")
PrintUtil.printMatrix(matrix: adjMat)
}
}
@main
enum GraphAdjacencyMatrix {
/* Driver Code */
static func main() {
/* */
// edges vertices
let vertices = [1, 3, 2, 5, 4]
let edges = [[0, 1], [1, 2], [2, 3], [0, 3], [2, 4], [3, 4]]
let graph = GraphAdjMat(vertices: vertices, edges: edges)
print("\n初期化後のグラフ")
graph.print()
/* */
// 1, 2 0, 2
graph.addEdge(i: 0, j: 2)
print("\n辺 1-2 を追加後のグラフ")
graph.print()
/* */
// 1, 3 0, 1
graph.removeEdge(i: 0, j: 1)
print("\n辺 1-3 を削除後のグラフ")
graph.print()
/* */
graph.addVertex(val: 6)
print("\n頂点 6 を追加後のグラフ")
graph.print()
/* */
// 3 1
graph.removeVertex(index: 1)
print("\n頂点 3 を削除後のグラフ")
graph.print()
}
}
@@ -0,0 +1,56 @@
/**
* File: graph_bfs.swift
* Created Time: 2023-02-21
* Author: nuomi1 (nuomi1@qq.com)
*/
import graph_adjacency_list_target
import utils
/* */
//
func graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {
//
var res: [Vertex] = []
//
var visited: Set<Vertex> = [startVet]
// BFS
var que: [Vertex] = [startVet]
// vet
while !que.isEmpty {
let vet = que.removeFirst() //
res.append(vet) //
//
for adjVet in graph.adjList[vet] ?? [] {
if visited.contains(adjVet) {
continue //
}
que.append(adjVet) //
visited.insert(adjVet) //
}
}
//
return res
}
@main
enum GraphBFS {
/* Driver Code */
static func main() {
/* */
let v = Vertex.valsToVets(vals: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
let 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]],
]
let graph = GraphAdjList(edges: edges)
print("\n初期化後のグラフ")
graph.print()
/* */
let res = graphBFS(graph: graph, startVet: v[0])
print("\n幅優先探索(BFS)の頂点列")
print(Vertex.vetsToVals(vets: res))
}
}
@@ -0,0 +1,54 @@
/**
* File: graph_dfs.swift
* Created Time: 2023-02-21
* Author: nuomi1 (nuomi1@qq.com)
*/
import graph_adjacency_list_target
import utils
/* */
func dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {
res.append(vet) //
visited.insert(vet) //
//
for adjVet in graph.adjList[vet] ?? [] {
if visited.contains(adjVet) {
continue //
}
//
dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)
}
}
/* */
//
func graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {
//
var res: [Vertex] = []
//
var visited: Set<Vertex> = []
dfs(graph: graph, visited: &visited, res: &res, vet: startVet)
return res
}
@main
enum GraphDFS {
/* Driver Code */
static func main() {
/* */
let v = Vertex.valsToVets(vals: [0, 1, 2, 3, 4, 5, 6])
let 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]],
]
let graph = GraphAdjList(edges: edges)
print("\n初期化後のグラフ")
graph.print()
/* */
let res = graphDFS(graph: graph, startVet: v[0])
print("\n深さ優先探索(DFS)の頂点列")
print(Vertex.vetsToVals(vets: res))
}
}