cpp-algo-cases/chapter_graph/graph_adjacency_matrix.cpp

128 lines
3.3 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: graph_adjacency_matrix.cpp
* Created Time: 2023-02-09
* Author: what-is-me (whatisme@outlook.jp)
*/
#include "../utils/common.hpp"
/* <20><><EFBFBD><EFBFBD><EFBFBD>ڽӾ<DABD><D3BE><EFBFBD>ʵ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC> */
class GraphAdjMat {
vector<int> vertices; // <20><><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD>Ԫ<EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
vector<vector<int>> adjMat; // <20>ڽӾ<DABD><D3BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public:
/* <20><><EFBFBD><EFBFBD><ECB7BD> */
GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {
// <20><><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>
for (int val : vertices) {
addVertex(val);
}
// <20><><EFBFBD>ӱ<EFBFBD>
// <20><>ע<EFBFBD>⣬edges Ԫ<>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ vertices Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (const vector<int> &edge : edges) {
addEdge(edge[0], edge[1]);
}
}
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int size() const {
return vertices.size();
}
/* <20><><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD> */
void addVertex(int val) {
int n = size();
// <20>򶥵<EFBFBD><F2B6A5B5>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><C2B6><EFBFBD><EFBFBD><EFBFBD>ֵ
vertices.push_back(val);
// <20><><EFBFBD>ڽӾ<DABD><D3BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
adjMat.emplace_back(vector<int>(n, 0));
// <20><><EFBFBD>ڽӾ<DABD><D3BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
for (vector<int> &row : adjMat) {
row.push_back(0);
}
}
/* ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void removeVertex(int index) {
if (index >= size()) {
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
// <20>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD> index <20>Ķ<EFBFBD><C4B6><EFBFBD>
vertices.erase(vertices.begin() + index);
// <20><><EFBFBD>ڽӾ<DABD><D3BE><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> index <20><><EFBFBD><EFBFBD>
adjMat.erase(adjMat.begin() + index);
// <20><><EFBFBD>ڽӾ<DABD><D3BE><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> index <20><><EFBFBD><EFBFBD>
for (vector<int> &row : adjMat) {
row.erase(row.begin() + index);
}
}
/* <20><><EFBFBD>ӱ<EFBFBD> */
// <20><><EFBFBD><EFBFBD> i, j <20><>Ӧ vertices Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void addEdge(int i, int j) {
// <20><><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD>У<EFBFBD><D0A3>ڽӾ<DABD><D3BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>߶Գƣ<D4B3><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (i, j) == (j, i)
adjMat[i][j] = 1;
adjMat[j][i] = 1;
}
/* ɾ<><C9BE><EFBFBD><EFBFBD> */
// <20><><EFBFBD><EFBFBD> i, j <20><>Ӧ vertices Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void removeEdge(int i, int j) {
// <20><><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
adjMat[i][j] = 0;
adjMat[j][i] = 0;
}
/* <20><>ӡ<EFBFBD>ڽӾ<DABD><D3BE><EFBFBD> */
void print() {
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD> = ";
printVector(vertices);
cout << "<EFBFBD>ڽӾ<EFBFBD><EFBFBD><EFBFBD> =" << endl;
printVectorMatrix(adjMat);
}
};
/* Driver Code */
int main() {
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ */
// <20><>ע<EFBFBD>⣬edges Ԫ<>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ vertices Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼΪ" << endl;
graph.print();
/* <20><><EFBFBD>ӱ<EFBFBD> */
// <20><><EFBFBD><EFBFBD> 1, 2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ϊ 0, 2
graph.addEdge(0, 2);
cout << "\n<EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD> 1-2 <20><><EFBFBD><EFBFBD>ͼΪ" << endl;
graph.print();
/* ɾ<><C9BE><EFBFBD><EFBFBD> */
// <20><><EFBFBD><EFBFBD> 1, 3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ϊ 0, 1
graph.removeEdge(0, 1);
cout << "\nɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1-3 <20><><EFBFBD><EFBFBD>ͼΪ" << endl;
graph.print();
/* <20><><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD> */
graph.addVertex(6);
cout << "\n<EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD> 6 <20><><EFBFBD><EFBFBD>ͼΪ" << endl;
graph.print();
/* ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
// <20><><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 1
graph.removeVertex(1);
cout << "\nɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD>ͼΪ" << endl;
graph.print();
return 0;
}