Make class Graph abstract and make derived classes Digraph and SCC

This commit is contained in:
stefiosif
2022-06-12 00:49:42 +03:00
parent b5b031db7f
commit 72741a6a5b
7 changed files with 113 additions and 113 deletions

26
graph/digraph.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef DIGRAPH_H_
#define DIGRAPH_H_
#include "graph.h"
#include <algorithm>
namespace graph {
template<typename T>
class Digraph : public Graph<T> {
public:
Digraph() = default;
// Reverse graph directions
Digraph<T> reverse();
};
template<typename T>
Digraph<T> Digraph<T>::reverse() {
return Digraph<T>();
}
} // namespace graph
#endif

View File

@@ -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

View File

@@ -1,45 +1,27 @@
#ifndef SCC_H_
#define SCC_H_
#include "graph/graph.h"
#include "graph.h"
#include <ranges>
namespace graph {
template<typename T>
class SCC {
class SCC : public Graph<T> {
public:
SCC(std::vector<T> scc);
SCC(std::map<T, std::set<T>> scc);
// Construct shortest path
std::vector<T> SPT();
// Convert SCC into a Graph
Graph<T> convert();
private:
std::vector<T> scc;
// Representative - Root(SPT) of this SCC
T root;
};
template<typename T>
SCC<T>::SCC(std::vector<T> component) {
scc = component;
root = scc[0];
}
template<typename T>
std::vector<T> SCC<T>::SPT() {
// BFS
return std::vector<T>();
}
template<typename T>
Graph<T> SCC<T>::convert() {
return Graph<T>();
SCC<T>::SCC(std::map<T, std::set<T>> scc) {
Graph<T>::adjMatrix = scc;
auto kv = std::views::keys(Graph<T>::adjMatrix);
Graph<T>::vertices = std::set<T>{ kv.begin(), kv.end() };
root = scc.begin()->first;
}
}; // namespace graph