mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-09 05:41:47 +08:00
Add the initial EN translation for C++ code (#1346)
This commit is contained in:
5
en/codes/cpp/chapter_graph/CMakeLists.txt
Normal file
5
en/codes/cpp/chapter_graph/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
add_executable(graph_bfs graph_bfs.cpp)
|
||||
add_executable(graph_dfs graph_dfs.cpp)
|
||||
# add_executable(graph_adjacency_list graph_adjacency_list.cpp)
|
||||
add_executable(graph_adjacency_list_test graph_adjacency_list_test.cpp)
|
||||
add_executable(graph_adjacency_matrix graph_adjacency_matrix.cpp)
|
||||
90
en/codes/cpp/chapter_graph/graph_adjacency_list.cpp
Normal file
90
en/codes/cpp/chapter_graph/graph_adjacency_list.cpp
Normal 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"
|
||||
|
||||
/* Undirected graph class based on adjacency list */
|
||||
class GraphAdjList {
|
||||
public:
|
||||
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
|
||||
unordered_map<Vertex *, vector<Vertex *>> adjList;
|
||||
|
||||
/* Remove a specified node from vector */
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Constructor */
|
||||
GraphAdjList(const vector<vector<Vertex *>> &edges) {
|
||||
// Add all vertices and edges
|
||||
for (const vector<Vertex *> &edge : edges) {
|
||||
addVertex(edge[0]);
|
||||
addVertex(edge[1]);
|
||||
addEdge(edge[0], edge[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
int size() {
|
||||
return adjList.size();
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
void addEdge(Vertex *vet1, Vertex *vet2) {
|
||||
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
|
||||
throw invalid_argument("Vertex does not exist");
|
||||
// Add edge vet1 - vet2
|
||||
adjList[vet1].push_back(vet2);
|
||||
adjList[vet2].push_back(vet1);
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
void removeEdge(Vertex *vet1, Vertex *vet2) {
|
||||
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
|
||||
throw invalid_argument("Vertex does not exist");
|
||||
// Remove edge vet1 - vet2
|
||||
remove(adjList[vet1], vet2);
|
||||
remove(adjList[vet2], vet1);
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
void addVertex(Vertex *vet) {
|
||||
if (adjList.count(vet))
|
||||
return;
|
||||
// Add a new linked list to the adjacency list
|
||||
adjList[vet] = vector<Vertex *>();
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
void removeVertex(Vertex *vet) {
|
||||
if (!adjList.count(vet))
|
||||
throw invalid_argument("Vertex does not exist");
|
||||
// Remove the vertex vet's corresponding linked list from the adjacency list
|
||||
adjList.erase(vet);
|
||||
// Traverse other vertices' linked lists, removing all edges containing vet
|
||||
for (auto &adj : adjList) {
|
||||
remove(adj.second, vet);
|
||||
}
|
||||
}
|
||||
|
||||
/* Print the adjacency list */
|
||||
void print() {
|
||||
cout << "Adjacency list =" << endl;
|
||||
for (auto &adj : adjList) {
|
||||
const auto &key = adj.first;
|
||||
const auto &vec = adj.second;
|
||||
cout << key->val << ": ";
|
||||
printVector(vetsToVals(vec));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// See test case in graph_adjacency_list_test.cpp
|
||||
49
en/codes/cpp/chapter_graph/graph_adjacency_list_test.cpp
Normal file
49
en/codes/cpp/chapter_graph/graph_adjacency_list_test.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* File: graph_adjacency_list_test.cpp
|
||||
* Created Time: 2023-02-09
|
||||
* Author: what-is-me (whatisme@outlook.jp), krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "./graph_adjacency_list.cpp"
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize undirected graph */
|
||||
vector<Vertex *> v = valsToVets(vector<int>{1, 3, 2, 5, 4});
|
||||
vector<vector<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(edges);
|
||||
cout << "\nAfter initialization, the graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Vertices 1, 2 i.e., v[0], v[2]
|
||||
graph.addEdge(v[0], v[2]);
|
||||
cout << "\nAfter adding edge 1-2, the graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertices 1, 3 i.e., v[0], v[1]
|
||||
graph.removeEdge(v[0], v[1]);
|
||||
cout << "\nAfter removing edge 1-3, the graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
Vertex *v5 = new Vertex(6);
|
||||
graph.addVertex(v5);
|
||||
cout << "\nAfter adding vertex 6, the graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 i.e., v[1]
|
||||
graph.removeVertex(v[1]);
|
||||
cout << "\nAfter removing vertex 3, the graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
// Free memory
|
||||
for (Vertex *vet : v) {
|
||||
delete vet;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
127
en/codes/cpp/chapter_graph/graph_adjacency_matrix.cpp
Normal file
127
en/codes/cpp/chapter_graph/graph_adjacency_matrix.cpp
Normal 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"
|
||||
|
||||
/* Undirected graph class based on adjacency matrix */
|
||||
class GraphAdjMat {
|
||||
vector<int> vertices; // Vertex list, elements represent "vertex value", index represents "vertex index"
|
||||
vector<vector<int>> adjMat; // Adjacency matrix, row and column indices correspond to "vertex index"
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {
|
||||
// Add vertex
|
||||
for (int val : vertices) {
|
||||
addVertex(val);
|
||||
}
|
||||
// Add edge
|
||||
// Edges elements represent vertex indices
|
||||
for (const vector<int> &edge : edges) {
|
||||
addEdge(edge[0], edge[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
int size() const {
|
||||
return vertices.size();
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
void addVertex(int val) {
|
||||
int n = size();
|
||||
// Add new vertex value to the vertex list
|
||||
vertices.push_back(val);
|
||||
// Add a row to the adjacency matrix
|
||||
adjMat.emplace_back(vector<int>(n, 0));
|
||||
// Add a column to the adjacency matrix
|
||||
for (vector<int> &row : adjMat) {
|
||||
row.push_back(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
void removeVertex(int index) {
|
||||
if (index >= size()) {
|
||||
throw out_of_range("Vertex does not exist");
|
||||
}
|
||||
// Remove vertex at `index` from the vertex list
|
||||
vertices.erase(vertices.begin() + index);
|
||||
// Remove the row at `index` from the adjacency matrix
|
||||
adjMat.erase(adjMat.begin() + index);
|
||||
// Remove the column at `index` from the adjacency matrix
|
||||
for (vector<int> &row : adjMat) {
|
||||
row.erase(row.begin() + index);
|
||||
}
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
// Parameters i, j correspond to vertices element indices
|
||||
void addEdge(int i, int j) {
|
||||
// Handle index out of bounds and equality
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {
|
||||
throw out_of_range("Vertex does not exist");
|
||||
}
|
||||
// In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., satisfies (i, j) == (j, i)
|
||||
adjMat[i][j] = 1;
|
||||
adjMat[j][i] = 1;
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
// Parameters i, j correspond to vertices element indices
|
||||
void removeEdge(int i, int j) {
|
||||
// Handle index out of bounds and equality
|
||||
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;
|
||||
}
|
||||
|
||||
/* Print adjacency matrix */
|
||||
void print() {
|
||||
cout << "Vertex list = ";
|
||||
printVector(vertices);
|
||||
cout << "Adjacency matrix =" << endl;
|
||||
printVectorMatrix(adjMat);
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize undirected graph */
|
||||
// Edges elements represent vertex indices
|
||||
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 << "\nAfter initialization, the graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Indices of vertices 1, 2 are 0, 2 respectively
|
||||
graph.addEdge(0, 2);
|
||||
cout << "\nAfter adding edge 1-2, the graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Indices of vertices 1, 3 are 0, 1 respectively
|
||||
graph.removeEdge(0, 1);
|
||||
cout << "\nAfter removing edge 1-3, the graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
graph.addVertex(6);
|
||||
cout << "\nAfter adding vertex 6, the graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
// Index of vertex 3 is 1
|
||||
graph.removeVertex(1);
|
||||
cout << "\nAfter removing vertex 3, the graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
return 0;
|
||||
}
|
||||
59
en/codes/cpp/chapter_graph/graph_bfs.cpp
Normal file
59
en/codes/cpp/chapter_graph/graph_bfs.cpp
Normal 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"
|
||||
|
||||
/* Breadth-first traversal */
|
||||
// Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
|
||||
vector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {
|
||||
// Vertex traversal sequence
|
||||
vector<Vertex *> res;
|
||||
// Hash set, used to record visited vertices
|
||||
unordered_set<Vertex *> visited = {startVet};
|
||||
// Queue used to implement BFS
|
||||
queue<Vertex *> que;
|
||||
que.push(startVet);
|
||||
// Starting from vertex vet, loop until all vertices are visited
|
||||
while (!que.empty()) {
|
||||
Vertex *vet = que.front();
|
||||
que.pop(); // Dequeue the vertex at the head of the queue
|
||||
res.push_back(vet); // Record visited vertex
|
||||
// Traverse all adjacent vertices of that vertex
|
||||
for (auto adjVet : graph.adjList[vet]) {
|
||||
if (visited.count(adjVet))
|
||||
continue; // Skip already visited vertices
|
||||
que.push(adjVet); // Only enqueue unvisited vertices
|
||||
visited.emplace(adjVet); // Mark the vertex as visited
|
||||
}
|
||||
}
|
||||
// Return the vertex traversal sequence
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize undirected graph */
|
||||
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 << "\nAfter initialization, the graph is\n";
|
||||
graph.print();
|
||||
|
||||
/* Breadth-first traversal */
|
||||
vector<Vertex *> res = graphBFS(graph, v[0]);
|
||||
cout << "\nBreadth-first traversal (BFS) vertex sequence is" << endl;
|
||||
printVector(vetsToVals(res));
|
||||
|
||||
// Free memory
|
||||
for (Vertex *vet : v) {
|
||||
delete vet;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
55
en/codes/cpp/chapter_graph/graph_dfs.cpp
Normal file
55
en/codes/cpp/chapter_graph/graph_dfs.cpp
Normal 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"
|
||||
|
||||
/* Depth-first traversal helper function */
|
||||
void dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {
|
||||
res.push_back(vet); // Record visited vertex
|
||||
visited.emplace(vet); // Mark the vertex as visited
|
||||
// Traverse all adjacent vertices of that vertex
|
||||
for (Vertex *adjVet : graph.adjList[vet]) {
|
||||
if (visited.count(adjVet))
|
||||
continue; // Skip already visited vertices
|
||||
// Recursively visit adjacent vertices
|
||||
dfs(graph, visited, res, adjVet);
|
||||
}
|
||||
}
|
||||
|
||||
/* Depth-first traversal */
|
||||
// Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
|
||||
vector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {
|
||||
// Vertex traversal sequence
|
||||
vector<Vertex *> res;
|
||||
// Hash set, used to record visited vertices
|
||||
unordered_set<Vertex *> visited;
|
||||
dfs(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize undirected graph */
|
||||
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 << "\nAfter initialization, the graph is" << endl;
|
||||
graph.print();
|
||||
|
||||
/* Depth-first traversal */
|
||||
vector<Vertex *> res = graphDFS(graph, v[0]);
|
||||
cout << "\nDepth-first traversal (DFS) vertex sequence is" << endl;
|
||||
printVector(vetsToVals(res));
|
||||
|
||||
// Free memory
|
||||
for (Vertex *vet : v) {
|
||||
delete vet;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user