Add single vertex contains method and replace vertices field with adjMatrix' keys
This commit is contained in:
@@ -27,8 +27,8 @@ private:
|
||||
template<typename T>
|
||||
std::map<T, bool> BreadthFirstSearch<T>::initExplore() {
|
||||
std::map<T, bool> graphExplore;
|
||||
for (const auto& v : G.vertices) {
|
||||
graphExplore[v] = false;
|
||||
for (const auto& v : G.adjMatrix) {
|
||||
graphExplore[v.first] = false;
|
||||
}
|
||||
return graphExplore;
|
||||
}
|
||||
|
||||
@@ -49,8 +49,8 @@ void DecrementalSCC<T>::findSCC() {
|
||||
for (auto& C : SCCs) {
|
||||
const auto& w = C.representative();
|
||||
|
||||
for (const auto& v : C.vertices)
|
||||
A[v] = w;
|
||||
for (const auto& v : C.adjMatrix)
|
||||
A[v.first] = w;
|
||||
|
||||
outTree[w] =
|
||||
BreadthFirstTree<T>(BreadthFirstSearch<T>(C).execute(w));
|
||||
|
||||
@@ -66,9 +66,9 @@ void Tarjan<T>::strongConnect(const T& v) {
|
||||
|
||||
template<typename T>
|
||||
std::vector<SCC<T>> Tarjan<T>::execute() {
|
||||
for (const auto& v : G.vertices) {
|
||||
if (p[v].index == -1) {
|
||||
strongConnect(v);
|
||||
for (auto& v : G.adjMatrix) {
|
||||
if (p[v.first].index == -1) {
|
||||
strongConnect(v.first);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,8 +22,6 @@ public:
|
||||
template<typename T>
|
||||
Digraph<T>::Digraph(std::map<T, std::set<T>> digraph) {
|
||||
Graph<T>::adjMatrix = digraph;
|
||||
auto kv = std::views::keys(Graph<T>::adjMatrix);
|
||||
Graph<T>::vertices = std::set<T>{ kv.begin(), kv.end() };
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
||||
@@ -18,6 +18,9 @@ public:
|
||||
// Return true if e(u,v) exists
|
||||
virtual bool contains(const T& u, const T& v);
|
||||
|
||||
// Return true if vertex u exists
|
||||
virtual bool contains(const T& u);
|
||||
|
||||
// Remove edge e(u,v)
|
||||
virtual void remove(const T& u, const T& v);
|
||||
|
||||
@@ -31,20 +34,16 @@ public:
|
||||
virtual void output();
|
||||
|
||||
// Adjacency matrix representation
|
||||
std::set<T> vertices;
|
||||
std::map<T, std::set<T>> adjMatrix;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
Graph<T>::~Graph() {
|
||||
vertices.clear();
|
||||
adjMatrix.clear();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void Graph<T>::insert(const T& u, const T& v) {
|
||||
Graph<T>::vertices.insert(u);
|
||||
Graph<T>::vertices.insert(v);
|
||||
Graph<T>::adjMatrix[u].insert(v);
|
||||
}
|
||||
|
||||
@@ -53,6 +52,11 @@ bool Graph<T>::contains(const T& u, const T& v) {
|
||||
return adjMatrix[u].contains(v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Graph<T>::contains(const T& u) {
|
||||
return adjMatrix.contains(u);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Graph<T>::remove(const T& u, const T& v) {
|
||||
adjMatrix[u].erase(v);
|
||||
@@ -60,23 +64,23 @@ void Graph<T>::remove(const T& u, const T& v) {
|
||||
|
||||
template<typename T>
|
||||
std::uint16_t Graph<T>::V() {
|
||||
return static_cast<std::uint16_t>(vertices.size());
|
||||
return static_cast<std::uint16_t>(adjMatrix.size());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::uint16_t Graph<T>::E() {
|
||||
std::uint16_t edges = 0;
|
||||
for (const auto& v : vertices) {
|
||||
edges += static_cast<std::uint16_t>(adjMatrix[v].size());
|
||||
for (const auto& v : adjMatrix) {
|
||||
edges += static_cast<std::uint16_t>(v.second.size());
|
||||
}
|
||||
return static_cast<std::uint16_t>(edges);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Graph<T>::output() {
|
||||
for (const auto& v : vertices) {
|
||||
for (const auto& u : adjMatrix[v]) {
|
||||
std::cout << v << "->" << u << '|';
|
||||
for (const auto& v : adjMatrix) {
|
||||
for (const auto& u : v.second) {
|
||||
std::cout << v.first << "->" << u << '|';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user