Move Graph::vertices to Digraph

This commit is contained in:
stefiosif
2022-10-12 09:32:40 +03:00
parent 3d9651c474
commit 8b6d341d18
3 changed files with 21 additions and 24 deletions

View File

@@ -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';
}