Use clang-format
This commit is contained in:
@@ -5,30 +5,28 @@
|
||||
|
||||
namespace graph {
|
||||
|
||||
template<typename T>
|
||||
class BreadthFirstTree : public Digraph<T> {
|
||||
template <typename T> class BreadthFirstTree : public Digraph<T> {
|
||||
public:
|
||||
BreadthFirstTree() = default;
|
||||
|
||||
BreadthFirstTree(std::unordered_map<T, std::unordered_set<T>> G, T root)
|
||||
: BreadthFirstTree<T>(Digraph<T>(G), root) {}
|
||||
: BreadthFirstTree<T>(Digraph<T>(G), root) {}
|
||||
|
||||
BreadthFirstTree(Digraph<T> G, T root);
|
||||
|
||||
void removeEdgeTo(const T& u);
|
||||
void removeEdgeTo(const T &u);
|
||||
|
||||
T root{};
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G, T root) {
|
||||
this->adjList = algo::BreadthFirstSearch<T>(G.adjList).execute(root);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void BreadthFirstTree<T>::removeEdgeTo(const T& u) {
|
||||
for (const auto& x : this->vertices()) {
|
||||
for (const auto& y : this->adjList[x]) {
|
||||
template <typename T> void BreadthFirstTree<T>::removeEdgeTo(const T &u) {
|
||||
for (const auto &x : this->vertices()) {
|
||||
for (const auto &y : this->adjList[x]) {
|
||||
if (y == u) {
|
||||
this->remove(x, u);
|
||||
return;
|
||||
|
||||
@@ -1,64 +1,59 @@
|
||||
#ifndef DIGRAPH_H_
|
||||
#define DIGRAPH_H_
|
||||
|
||||
#include "graph.h"
|
||||
#include "algorithm/breadth_first_search.h"
|
||||
#include "graph.h"
|
||||
|
||||
namespace graph {
|
||||
|
||||
template<typename T>
|
||||
class Digraph : public Graph<T> {
|
||||
template <typename T> class Digraph : public Graph<T> {
|
||||
public:
|
||||
Digraph() = default;
|
||||
|
||||
explicit Digraph(std::unordered_map<T, std::unordered_set<T>> G);
|
||||
|
||||
// Return true if there is a path from u to v
|
||||
bool contains(const T& u, const T& v);
|
||||
bool contains(const T &u, const T &v);
|
||||
|
||||
// Add edge e(u,v)
|
||||
void insert(const T& u, const T& v);
|
||||
void insert(const T &u, const T &v);
|
||||
|
||||
// Remove edge e(u,v)
|
||||
void remove(const T& u, const T& v);
|
||||
void remove(const T &u, const T &v);
|
||||
|
||||
// Reverse graph directions
|
||||
auto reverse();
|
||||
|
||||
//
|
||||
auto contains(const T& u);
|
||||
|
||||
friend std::ostream& operator<<<>(std::ostream& os, Digraph<T>& G);
|
||||
auto contains(const T &u);
|
||||
|
||||
template <typename U>
|
||||
friend std::ostream &operator<<(std::ostream &os, const Digraph<U> &G);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
Digraph<T>::Digraph(std::unordered_map<T, std::unordered_set<T>> G) {
|
||||
this->adjList = G;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Digraph<T>::contains(const T& u, const T& v) {
|
||||
template <typename T> bool Digraph<T>::contains(const T &u, const T &v) {
|
||||
return algo::BreadthFirstSearch<T>(this->adjList).query(u, v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Digraph<T>::insert(const T& u, const T& v) {
|
||||
template <typename T> void Digraph<T>::insert(const T &u, const T &v) {
|
||||
this->adjList[u].insert(v);
|
||||
this->adjList[v];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Digraph<T>::remove(const T& u, const T& v) {
|
||||
template <typename T> void Digraph<T>::remove(const T &u, const T &v) {
|
||||
this->adjList[u].erase(v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
auto Digraph<T>::reverse() {
|
||||
template <typename T> auto Digraph<T>::reverse() {
|
||||
std::unordered_map<T, std::unordered_set<T>> revMatrix;
|
||||
|
||||
for (const auto& u : this->vertices()) {
|
||||
for (const auto& v : this->adjList[u]) {
|
||||
|
||||
for (const auto &u : this->vertices()) {
|
||||
for (const auto &v : this->adjList[u]) {
|
||||
revMatrix[v].insert(u);
|
||||
}
|
||||
}
|
||||
@@ -66,17 +61,16 @@ auto Digraph<T>::reverse() {
|
||||
return revMatrix;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
auto Digraph<T>::contains(const T& u) {
|
||||
template <typename T> auto Digraph<T>::contains(const T &u) {
|
||||
return this->adjList.count(u);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::ostream& operator<<(std::ostream& os, Digraph<T>& G) {
|
||||
template <typename T>
|
||||
std::ostream &operator<<(std::ostream &os, Digraph<T> &G) {
|
||||
os << "V: " << G.V() << " E: " << G.E() << '\n';
|
||||
for (const auto& u : G.vertices()) {
|
||||
for (const auto &u : G.vertices()) {
|
||||
if (!G.adjList[u].empty()) {
|
||||
for (const auto& v : G.adjList[u]) {
|
||||
for (const auto &v : G.adjList[u]) {
|
||||
os << u << "->" << v << ' ';
|
||||
}
|
||||
os << '\n';
|
||||
|
||||
@@ -1,30 +1,29 @@
|
||||
#ifndef GRAPH_H_
|
||||
#define GRAPH_H_
|
||||
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <ostream>
|
||||
#include <ranges>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace graph {
|
||||
|
||||
// Forward declerations
|
||||
template<typename T> class Graph;
|
||||
template<typename T> std::ostream& operator<<(std::ostream& os, Graph<T>& G);
|
||||
template <typename T> class Graph;
|
||||
template <typename T> std::ostream &operator<<(std::ostream &os, Graph<T> &G);
|
||||
|
||||
template<typename T>
|
||||
class Graph {
|
||||
template <typename T> class Graph {
|
||||
public:
|
||||
virtual ~Graph() = default;
|
||||
|
||||
|
||||
// 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() const;
|
||||
@@ -37,31 +36,24 @@ public:
|
||||
|
||||
// Adjacency matrix representation
|
||||
std::unordered_map<T, std::unordered_set<T>> adjList;
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
auto Graph<T>::vertices() const{
|
||||
template <typename T> auto Graph<T>::vertices() const {
|
||||
return std::views::keys(adjList);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::uint16_t Graph<T>::V() {
|
||||
template <typename T> std::uint16_t Graph<T>::V() {
|
||||
return static_cast<std::uint16_t>(adjList.size());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::uint16_t Graph<T>::E() {
|
||||
template <typename T> std::uint16_t Graph<T>::E() {
|
||||
std::uint16_t edges = 0;
|
||||
for (const auto& u : vertices()) {
|
||||
for (const auto &u : vertices()) {
|
||||
edges += static_cast<std::uint16_t>(adjList[u].size());
|
||||
}
|
||||
return edges;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace graph
|
||||
|
||||
|
||||
#endif
|
||||
@@ -8,35 +8,36 @@
|
||||
|
||||
namespace graph {
|
||||
|
||||
template<typename T>
|
||||
class SCC : public Digraph<T> {
|
||||
template <typename T> class SCC : public Digraph<T> {
|
||||
public:
|
||||
SCC() = default;
|
||||
|
||||
SCC(std::unordered_map<T, std::unordered_set<T>> G, T id)
|
||||
: Digraph<T>(G), id(id) { normalize(); }
|
||||
: Digraph<T>(G), id(id) {
|
||||
normalize();
|
||||
}
|
||||
|
||||
SCC(Digraph<T> G, T id) : id(id) { normalize(); }
|
||||
|
||||
// Return true if u is part of this SCC
|
||||
bool contains(const T& u) const;
|
||||
bool contains(const T &u) const;
|
||||
|
||||
// Representative vertex of this SCC
|
||||
T id{};
|
||||
|
||||
//
|
||||
//
|
||||
std::unordered_map<T, std::unordered_set<T>> neighboorList;
|
||||
|
||||
bool operator==(const SCC& o) const;
|
||||
bool operator==(const SCC &o) const;
|
||||
|
||||
private:
|
||||
// Erase all edges that include vertices outside this SCC
|
||||
void normalize();
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void SCC<T>::normalize() {
|
||||
for (const auto& u : this->vertices()) {
|
||||
for (const auto& v : this->adjList[u]) {
|
||||
template <typename T> void SCC<T>::normalize() {
|
||||
for (const auto &u : this->vertices()) {
|
||||
for (const auto &v : this->adjList[u]) {
|
||||
if (!this->contains(v)) {
|
||||
this->adjList[u].erase(v);
|
||||
this->adjList.erase(v);
|
||||
@@ -46,19 +47,16 @@ void SCC<T>::normalize() {
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool SCC<T>::contains(const T& u) const {
|
||||
template <typename T> bool SCC<T>::contains(const T &u) const {
|
||||
return this->adjList.count(u);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool SCC<T>::operator==(const SCC& o) const {
|
||||
template <typename T> bool SCC<T>::operator==(const SCC &o) const {
|
||||
return id == o.id;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct HashSCC {
|
||||
std::size_t operator()(const SCC<T>& C) const {
|
||||
template <typename T> struct HashSCC {
|
||||
std::size_t operator()(const SCC<T> &C) const {
|
||||
return static_cast<std::size_t>(C.id);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user