Move Graph::vertices to Digraph
This commit is contained in:
@@ -4,8 +4,6 @@
|
|||||||
#include "graph.h"
|
#include "graph.h"
|
||||||
#include "algorithm/breadth_first_search.h"
|
#include "algorithm/breadth_first_search.h"
|
||||||
|
|
||||||
#include <ranges>
|
|
||||||
|
|
||||||
namespace graph {
|
namespace graph {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -26,9 +24,6 @@ public:
|
|||||||
|
|
||||||
// Reverse graph directions
|
// Reverse graph directions
|
||||||
auto reverse();
|
auto reverse();
|
||||||
|
|
||||||
// Return vertices
|
|
||||||
auto vertices();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -56,7 +51,7 @@ template<typename T>
|
|||||||
auto Digraph<T>::reverse() {
|
auto Digraph<T>::reverse() {
|
||||||
std::map<T, std::set<T>> revMatrix;
|
std::map<T, std::set<T>> revMatrix;
|
||||||
|
|
||||||
for (const auto& u : vertices()) {
|
for (const auto& u : this->vertices()) {
|
||||||
for (const auto& v : this->adjMatrix[u]) {
|
for (const auto& v : this->adjMatrix[u]) {
|
||||||
revMatrix[v].insert(u);
|
revMatrix[v].insert(u);
|
||||||
}
|
}
|
||||||
@@ -65,11 +60,6 @@ auto Digraph<T>::reverse() {
|
|||||||
return revMatrix;
|
return revMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
auto Digraph<T>::vertices() {
|
|
||||||
return std::views::keys(this->adjMatrix);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace graph
|
} // namespace graph
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
namespace graph {
|
namespace graph {
|
||||||
|
|
||||||
@@ -17,13 +18,16 @@ public:
|
|||||||
~Graph();
|
~Graph();
|
||||||
|
|
||||||
// Return true if there is a path from u to v
|
// Return true if there is a path from u to v
|
||||||
virtual bool contains(const T& u, const T& v) = 0;
|
virtual bool contains(const T& u, const T& v) =0;
|
||||||
|
|
||||||
// Add edge e(u,v)
|
// Add edge e(u,v)
|
||||||
virtual void insert(const T& u, const T& v) = 0;
|
virtual void insert(const T& u, const T& v) =0;
|
||||||
|
|
||||||
// Remove edge e(u,v)
|
// Remove edge e(u,v)
|
||||||
virtual void remove(const T& u, const T& v) = 0;
|
virtual void remove(const T& u, const T& v) =0;
|
||||||
|
|
||||||
|
// Return graph vertices
|
||||||
|
auto vertices();
|
||||||
|
|
||||||
// Return num. of vertices
|
// Return num. of vertices
|
||||||
std::uint16_t V();
|
std::uint16_t V();
|
||||||
@@ -42,6 +46,11 @@ Graph<T>::~Graph() {
|
|||||||
adjMatrix.clear();
|
adjMatrix.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
auto Graph<T>::vertices() {
|
||||||
|
return std::views::keys(adjMatrix);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::uint16_t Graph<T>::V() {
|
std::uint16_t Graph<T>::V() {
|
||||||
return static_cast<std::uint16_t>(adjMatrix.size());
|
return static_cast<std::uint16_t>(adjMatrix.size());
|
||||||
@@ -50,8 +59,8 @@ std::uint16_t Graph<T>::V() {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
std::uint16_t Graph<T>::E() {
|
std::uint16_t Graph<T>::E() {
|
||||||
std::uint16_t edges = 0;
|
std::uint16_t edges = 0;
|
||||||
for (const auto& v : adjMatrix) {
|
for (const auto& u : vertices()) {
|
||||||
edges += static_cast<std::uint16_t>(v.second.size());
|
edges += static_cast<std::uint16_t>(adjMatrix[u].size());
|
||||||
}
|
}
|
||||||
return static_cast<std::uint16_t>(edges);
|
return static_cast<std::uint16_t>(edges);
|
||||||
}
|
}
|
||||||
@@ -59,10 +68,10 @@ std::uint16_t Graph<T>::E() {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
std::ostream& operator<<(std::ostream& os, Graph<T>& G) {
|
std::ostream& operator<<(std::ostream& os, Graph<T>& G) {
|
||||||
os << "V: " << G.V() << " E: " << G.E() << '\n';
|
os << "V: " << G.V() << " E: " << G.E() << '\n';
|
||||||
for (const auto& u : G.adjMatrix) {
|
for (const auto& u : this->vertices()) {
|
||||||
if (!u.second.empty()) {
|
if (!this->adjMatrix[u].empty()) {
|
||||||
for (const auto& v : u.second) {
|
for (const auto& v : this->adjMatrix[u]) {
|
||||||
os << u.first << "->" << v << ' ';
|
os << u << "->" << v << ' ';
|
||||||
}
|
}
|
||||||
os << '\n';
|
os << '\n';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ TEST_SUITE("Graph") {
|
|||||||
G.insert(13, 9);
|
G.insert(13, 9);
|
||||||
G.insert(12, 10);
|
G.insert(12, 10);
|
||||||
|
|
||||||
REQUIRE_EQ(G.adjMatrix.size(), 13);
|
REQUIRE_EQ(G.V(), 13);
|
||||||
|
|
||||||
auto reverse = G.reverse();
|
auto reverse = G.reverse();
|
||||||
|
|
||||||
@@ -85,8 +85,6 @@ TEST_SUITE("Graph") {
|
|||||||
G.insert(13, 9);
|
G.insert(13, 9);
|
||||||
G.insert(12, 10);
|
G.insert(12, 10);
|
||||||
|
|
||||||
REQUIRE_EQ(G.adjMatrix.size(), 13);
|
|
||||||
|
|
||||||
CHECK_EQ(G.V(), 13);
|
CHECK_EQ(G.V(), 13);
|
||||||
CHECK_EQ(G.E(), 18);
|
CHECK_EQ(G.E(), 18);
|
||||||
}
|
}
|
||||||
@@ -116,7 +114,7 @@ TEST_SUITE("Graph") {
|
|||||||
G.insert(8, 9);
|
G.insert(8, 9);
|
||||||
G.insert(9, 5);
|
G.insert(9, 5);
|
||||||
|
|
||||||
REQUIRE_EQ(G.adjMatrix.size(), 9);
|
REQUIRE_EQ(G.V(), 9);
|
||||||
|
|
||||||
auto SCCs = algo::Tarjan<std::uint16_t>(G.adjMatrix).execute();
|
auto SCCs = algo::Tarjan<std::uint16_t>(G.adjMatrix).execute();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user