91 lines
2.4 KiB
C++
91 lines
2.4 KiB
C++
|
/**
|
|||
|
* 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"
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ڽӱ<DABD>ʵ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC> */
|
|||
|
class GraphAdjList {
|
|||
|
public:
|
|||
|
// <20>ڽӱ<DABD><D3B1><EFBFBD>key<65><79><EFBFBD><EFBFBD><EFBFBD>㣬value<75><65><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽӶ<DABD><D3B6><EFBFBD>
|
|||
|
unordered_map<Vertex *, vector<Vertex *>> adjList;
|
|||
|
|
|||
|
/* <20><> vector <20><>ɾ<EFBFBD><C9BE>ָ<EFBFBD><D6B8><EFBFBD>ڵ<EFBFBD> */
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>췽<EFBFBD><ECB7BD> */
|
|||
|
GraphAdjList(const vector<vector<Vertex *>> &edges) {
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ͱ<EFBFBD>
|
|||
|
for (const vector<Vertex *> &edge : edges) {
|
|||
|
addVertex(edge[0]);
|
|||
|
addVertex(edge[1]);
|
|||
|
addEdge(edge[0], edge[1]);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
int size() {
|
|||
|
return adjList.size();
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ӱ<EFBFBD> */
|
|||
|
void addEdge(Vertex *vet1, Vertex *vet2) {
|
|||
|
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
|
|||
|
throw invalid_argument("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD>");
|
|||
|
// <20><><EFBFBD>ӱ<EFBFBD> vet1 - vet2
|
|||
|
adjList[vet1].push_back(vet2);
|
|||
|
adjList[vet2].push_back(vet1);
|
|||
|
}
|
|||
|
|
|||
|
/* ɾ<><C9BE><EFBFBD><EFBFBD> */
|
|||
|
void removeEdge(Vertex *vet1, Vertex *vet2) {
|
|||
|
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
|
|||
|
throw invalid_argument("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD>");
|
|||
|
// ɾ<><C9BE><EFBFBD><EFBFBD> vet1 - vet2
|
|||
|
remove(adjList[vet1], vet2);
|
|||
|
remove(adjList[vet2], vet1);
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD> */
|
|||
|
void addVertex(Vertex *vet) {
|
|||
|
if (adjList.count(vet))
|
|||
|
return;
|
|||
|
// <20><><EFBFBD>ڽӱ<DABD><D3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
adjList[vet] = vector<Vertex *>();
|
|||
|
}
|
|||
|
|
|||
|
/* ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
void removeVertex(Vertex *vet) {
|
|||
|
if (!adjList.count(vet))
|
|||
|
throw invalid_argument("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD>");
|
|||
|
// <20><><EFBFBD>ڽӱ<DABD><D3B1><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> vet <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
adjList.erase(vet);
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD>а<EFBFBD><D0B0><EFBFBD> vet <20>ı<EFBFBD>
|
|||
|
for (auto &adj : adjList) {
|
|||
|
remove(adj.second, vet);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ӡ<EFBFBD>ڽӱ<DABD> */
|
|||
|
void print() {
|
|||
|
cout << "<EFBFBD>ڽӱ<EFBFBD> =" << endl;
|
|||
|
for (auto &adj : adjList) {
|
|||
|
const auto &key = adj.first;
|
|||
|
const auto &vec = adj.second;
|
|||
|
cout << key->val << ": ";
|
|||
|
printVector(vetsToVals(vec));
|
|||
|
}
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> graph_adjacency_list_test.cpp
|