docs: add Japanese translate documents (#1812)

* docs: add Japanese documents (`ja/docs`)

* docs: add Japanese documents (`ja/codes`)

* docs: add Japanese documents

* Remove pythontutor blocks in ja/

* Add an empty at the end of each markdown file.

* Add the missing figures (use the English version temporarily).

* Add index.md for Japanese version.

* Add index.html for Japanese version.

* Add missing index.assets

* Fix backtracking_algorithm.md for Japanese version.

* Add avatar_eltociear.jpg. Fix image links on the Japanese landing page.

* Add the Japanese banner.

---------

Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
Ikko Eltociear Ashimine
2025-10-17 06:04:43 +09:00
committed by GitHub
parent 2487a27036
commit 954c45864b
886 changed files with 33569 additions and 0 deletions

View File

@ -0,0 +1,90 @@
/**
* File: graph_adjacency_list.cpp
* Created Time: 2023-02-09
* Author: what-is-me (whatisme@outlook.jp), krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* 隣接リストに基づく無向グラフクラス */
class GraphAdjList {
public:
// 隣接リスト、キー:頂点、値:その頂点のすべての隣接頂点
unordered_map<Vertex *, vector<Vertex *>> adjList;
/* ベクターから指定されたノードを削除 */
void remove(vector<Vertex *> &vec, Vertex *vet) {
for (int i = 0; i < vec.size(); i++) {
if (vec[i] == vet) {
vec.erase(vec.begin() + i);
break;
}
}
}
/* コンストラクタ */
GraphAdjList(const vector<vector<Vertex *>> &edges) {
// すべての頂点と辺を追加
for (const vector<Vertex *> &edge : edges) {
addVertex(edge[0]);
addVertex(edge[1]);
addEdge(edge[0], edge[1]);
}
}
/* 頂点数を取得 */
int size() {
return adjList.size();
}
/* 辺を追加 */
void addEdge(Vertex *vet1, Vertex *vet2) {
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
throw invalid_argument("Vertex does not exist");
// 辺 vet1 - vet2 を追加
adjList[vet1].push_back(vet2);
adjList[vet2].push_back(vet1);
}
/* 辺を削除 */
void removeEdge(Vertex *vet1, Vertex *vet2) {
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
throw invalid_argument("Vertex does not exist");
// 辺 vet1 - vet2 を削除
remove(adjList[vet1], vet2);
remove(adjList[vet2], vet1);
}
/* 頂点を追加 */
void addVertex(Vertex *vet) {
if (adjList.count(vet))
return;
// 隣接リストに新しい連結リストを追加
adjList[vet] = vector<Vertex *>();
}
/* 頂点を削除 */
void removeVertex(Vertex *vet) {
if (!adjList.count(vet))
throw invalid_argument("Vertex does not exist");
// 隣接リストから頂点vetに対応する連結リストを削除
adjList.erase(vet);
// 他の頂点の連結リストを走査し、vetを含むすべての辺を削除
for (auto &adj : adjList) {
remove(adj.second, vet);
}
}
/* 隣接リストを印刷 */
void print() {
cout << "隣接リスト =" << endl;
for (auto &adj : adjList) {
const auto &key = adj.first;
const auto &vec = adj.second;
cout << key->val << ": ";
printVector(vetsToVals(vec));
}
}
};
// テストケースはgraph_adjacency_list_test.cppを参照

View File

@ -0,0 +1,127 @@
/**
* File: graph_adjacency_matrix.cpp
* Created Time: 2023-02-09
* Author: what-is-me (whatisme@outlook.jp)
*/
#include "../utils/common.hpp"
/* 隣接行列に基づく無向グラフクラス */
class GraphAdjMat {
vector<int> vertices; // 頂点リスト、要素は「頂点値」を表し、インデックスは「頂点インデックス」を表す
vector<vector<int>> adjMat; // 隣接行列、行と列のインデックスは「頂点インデックス」に対応
public:
/* コンストラクタ */
GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {
// 頂点を追加
for (int val : vertices) {
addVertex(val);
}
// 辺を追加
// 辺の要素は頂点インデックスを表す
for (const vector<int> &edge : edges) {
addEdge(edge[0], edge[1]);
}
}
/* 頂点数を取得 */
int size() const {
return vertices.size();
}
/* 頂点を追加 */
void addVertex(int val) {
int n = size();
// 頂点リストに新しい頂点値を追加
vertices.push_back(val);
// 隣接行列に行を追加
adjMat.emplace_back(vector<int>(n, 0));
// 隣接行列に列を追加
for (vector<int> &row : adjMat) {
row.push_back(0);
}
}
/* 頂点を削除 */
void removeVertex(int index) {
if (index >= size()) {
throw out_of_range("Vertex does not exist");
}
// 頂点リストから`index`の頂点を削除
vertices.erase(vertices.begin() + index);
// 隣接行列から`index`の行を削除
adjMat.erase(adjMat.begin() + index);
// 隣接行列から`index`の列を削除
for (vector<int> &row : adjMat) {
row.erase(row.begin() + index);
}
}
/* 辺を追加 */
// パラメータi、jは頂点要素のインデックスに対応
void addEdge(int i, int j) {
// インデックス範囲外と等価性を処理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {
throw out_of_range("Vertex does not exist");
}
// 無向グラフでは、隣接行列は主対角線について対称、即ち(i, j) == (j, i)を満たす
adjMat[i][j] = 1;
adjMat[j][i] = 1;
}
/* 辺を削除 */
// パラメータi、jは頂点要素のインデックスに対応
void removeEdge(int i, int j) {
// インデックス範囲外と等価性を処理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {
throw out_of_range("Vertex does not exist");
}
adjMat[i][j] = 0;
adjMat[j][i] = 0;
}
/* 隣接行列を印刷 */
void print() {
cout << "頂点リスト = ";
printVector(vertices);
cout << "隣接行列 =" << endl;
printVectorMatrix(adjMat);
}
};
/* ドライバーコード */
int main() {
/* 無向グラフを初期化 */
// 辺の要素は頂点インデックスを表す
vector<int> vertices = {1, 3, 2, 5, 4};
vector<vector<int>> edges = {{0, 1}, {0, 3}, {1, 2}, {2, 3}, {2, 4}, {3, 4}};
GraphAdjMat graph(vertices, edges);
cout << "\n初期化後、グラフは" << endl;
graph.print();
/* 辺を追加 */
// 頂点1、2のインデックスはそれぞれ0、2
graph.addEdge(0, 2);
cout << "\n辺 1-2 を追加後、グラフは" << endl;
graph.print();
/* 辺を削除 */
// 頂点1、3のインデックスはそれぞれ0、1
graph.removeEdge(0, 1);
cout << "\n辺 1-3 を削除後、グラフは" << endl;
graph.print();
/* 頂点を追加 */
graph.addVertex(6);
cout << "\n頂点 6 を追加後、グラフは" << endl;
graph.print();
/* 頂点を削除 */
// 頂点3のインデックスは1
graph.removeVertex(1);
cout << "\n頂点 3 を削除後、グラフは" << endl;
graph.print();
return 0;
}

View File

@ -0,0 +1,59 @@
/**
* File: graph_bfs.cpp
* Created Time: 2023-03-02
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
#include "./graph_adjacency_list.cpp"
/* 幅優先走査 */
// 隣接リストを使用してグラフを表現し、指定された頂点のすべての隣接頂点を取得
vector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {
// 頂点走査順序
vector<Vertex *> res;
// ハッシュセット、訪問済み頂点を記録するために使用
unordered_set<Vertex *> visited = {startVet};
// BFSを実装するために使用されるキュー
queue<Vertex *> que;
que.push(startVet);
// 頂点vetから開始し、すべての頂点が訪問されるまでループ
while (!que.empty()) {
Vertex *vet = que.front();
que.pop(); // キューの先頭の頂点をデキュー
res.push_back(vet); // 訪問済み頂点を記録
// その頂点のすべての隣接頂点を走査
for (auto adjVet : graph.adjList[vet]) {
if (visited.count(adjVet))
continue; // すでに訪問済みの頂点をスキップ
que.push(adjVet); // 未訪問の頂点のみをエンキュー
visited.emplace(adjVet); // 頂点を訪問済みとしてマーク
}
}
// 頂点走査順序を返す
return res;
}
/* ドライバーコード */
int main() {
/* 無向グラフを初期化 */
vector<Vertex *> v = valsToVets({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
vector<vector<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(edges);
cout << "\n初期化後、グラフは\n";
graph.print();
/* 幅優先走査 */
vector<Vertex *> res = graphBFS(graph, v[0]);
cout << "\n幅優先走査BFSの頂点順序は" << endl;
printVector(vetsToVals(res));
// メモリを解放
for (Vertex *vet : v) {
delete vet;
}
return 0;
}

View File

@ -0,0 +1,55 @@
/**
* File: graph_dfs.cpp
* Created Time: 2023-03-02
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
#include "./graph_adjacency_list.cpp"
/* 深さ優先走査ヘルパー関数 */
void dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {
res.push_back(vet); // 訪問済み頂点を記録
visited.emplace(vet); // 頂点を訪問済みとしてマーク
// その頂点のすべての隣接頂点を走査
for (Vertex *adjVet : graph.adjList[vet]) {
if (visited.count(adjVet))
continue; // すでに訪問済みの頂点をスキップ
// 隣接頂点を再帰的に訪問
dfs(graph, visited, res, adjVet);
}
}
/* 深さ優先走査 */
// 隣接リストを使用してグラフを表現し、指定された頂点のすべての隣接頂点を取得
vector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {
// 頂点走査順序
vector<Vertex *> res;
// ハッシュセット、訪問済み頂点を記録するために使用
unordered_set<Vertex *> visited;
dfs(graph, visited, res, startVet);
return res;
}
/* ドライバーコード */
int main() {
/* 無向グラフを初期化 */
vector<Vertex *> v = valsToVets(vector<int>{0, 1, 2, 3, 4, 5, 6});
vector<vector<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(edges);
cout << "\n初期化後、グラフは" << endl;
graph.print();
/* 深さ優先走査 */
vector<Vertex *> res = graphDFS(graph, v[0]);
cout << "\n深さ優先走査DFSの頂点順序は" << endl;
printVector(vetsToVals(res));
// メモリを解放
for (Vertex *vet : v) {
delete vet;
}
return 0;
}