Make class Graph abstract and make derived classes Digraph and SCC
This commit is contained in:
@@ -3,8 +3,6 @@
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
namespace graph {
|
||||
@@ -12,23 +10,19 @@ namespace graph {
|
||||
template<typename T>
|
||||
class Graph {
|
||||
public:
|
||||
Graph() = default;
|
||||
|
||||
// Add vertex v
|
||||
void insert(const T& v);
|
||||
~Graph();
|
||||
|
||||
// Add edge between v and u
|
||||
void insert(const T& v, const T& u);
|
||||
|
||||
// Reverse graph directions
|
||||
Graph<T> reverse();
|
||||
virtual void insert(const T& v, const T& u);
|
||||
|
||||
// Number of vertices
|
||||
std::uint16_t V();
|
||||
virtual std::uint16_t V();
|
||||
|
||||
// Number of edges
|
||||
// TODO: Calculate bidirectional edges once
|
||||
std::uint16_t E();
|
||||
virtual std::uint16_t E();
|
||||
|
||||
// Output graph
|
||||
virtual void output();
|
||||
|
||||
// Adjacency matrix representation
|
||||
std::set<T> vertices;
|
||||
@@ -36,36 +30,44 @@ public:
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void Graph<T>::insert(const T& v) {
|
||||
vertices.insert(v);
|
||||
Graph<T>::~Graph() {
|
||||
vertices.clear();
|
||||
adjMatrix.clear();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Graph<T>::insert(const T& v, const T& u) {
|
||||
vertices.insert(v);
|
||||
vertices.insert(u);
|
||||
adjMatrix[v].insert(u);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Graph<T> Graph<T>::reverse() {
|
||||
return Graph<T>();
|
||||
inline void Graph<T>::insert(const T& v, const T& u) {
|
||||
Graph<T>::vertices.insert(v);
|
||||
Graph<T>::vertices.insert(u);
|
||||
Graph<T>::adjMatrix[v].insert(u);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::uint16_t Graph<T>::V() {
|
||||
return vertices.size();
|
||||
return static_cast<std::uint16_t>(vertices.size());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::uint16_t Graph<T>::E() {
|
||||
std::uint16_t edges = 0;
|
||||
for (const auto& v : vertices) {
|
||||
edges += adjMatrix[v].size();
|
||||
edges += static_cast<std::uint16_t>(adjMatrix[v].size());
|
||||
}
|
||||
return edges;
|
||||
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 << '|';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
} // namespace graph
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user