Move Graph::vertices to Digraph
This commit is contained in:
@@ -4,8 +4,6 @@
|
||||
#include "graph.h"
|
||||
#include "algorithm/breadth_first_search.h"
|
||||
|
||||
#include <ranges>
|
||||
|
||||
namespace graph {
|
||||
|
||||
template<typename T>
|
||||
@@ -26,9 +24,6 @@ public:
|
||||
|
||||
// Reverse graph directions
|
||||
auto reverse();
|
||||
|
||||
// Return vertices
|
||||
auto vertices();
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
@@ -56,7 +51,7 @@ template<typename T>
|
||||
auto Digraph<T>::reverse() {
|
||||
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]) {
|
||||
revMatrix[v].insert(u);
|
||||
}
|
||||
@@ -65,11 +60,6 @@ auto Digraph<T>::reverse() {
|
||||
return revMatrix;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
auto Digraph<T>::vertices() {
|
||||
return std::views::keys(this->adjMatrix);
|
||||
}
|
||||
|
||||
} // namespace graph
|
||||
|
||||
#endif
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <ostream>
|
||||
#include <ranges>
|
||||
|
||||
namespace graph {
|
||||
|
||||
@@ -17,13 +18,16 @@ public:
|
||||
~Graph();
|
||||
|
||||
// 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)
|
||||
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)
|
||||
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
|
||||
std::uint16_t V();
|
||||
@@ -42,6 +46,11 @@ Graph<T>::~Graph() {
|
||||
adjMatrix.clear();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
auto Graph<T>::vertices() {
|
||||
return std::views::keys(adjMatrix);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::uint16_t Graph<T>::V() {
|
||||
return static_cast<std::uint16_t>(adjMatrix.size());
|
||||
@@ -50,8 +59,8 @@ std::uint16_t Graph<T>::V() {
|
||||
template<typename T>
|
||||
std::uint16_t Graph<T>::E() {
|
||||
std::uint16_t edges = 0;
|
||||
for (const auto& v : adjMatrix) {
|
||||
edges += static_cast<std::uint16_t>(v.second.size());
|
||||
for (const auto& u : vertices()) {
|
||||
edges += static_cast<std::uint16_t>(adjMatrix[u].size());
|
||||
}
|
||||
return static_cast<std::uint16_t>(edges);
|
||||
}
|
||||
@@ -59,10 +68,10 @@ std::uint16_t Graph<T>::E() {
|
||||
template<typename T>
|
||||
std::ostream& operator<<(std::ostream& os, Graph<T>& G) {
|
||||
os << "V: " << G.V() << " E: " << G.E() << '\n';
|
||||
for (const auto& u : G.adjMatrix) {
|
||||
if (!u.second.empty()) {
|
||||
for (const auto& v : u.second) {
|
||||
os << u.first << "->" << v << ' ';
|
||||
for (const auto& u : this->vertices()) {
|
||||
if (!this->adjMatrix[u].empty()) {
|
||||
for (const auto& v : this->adjMatrix[u]) {
|
||||
os << u << "->" << v << ' ';
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user