Add operator<< for graph types, add contains query for digraph types and normalize scc
This commit is contained in:
@@ -3,38 +3,38 @@
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
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 {
|
||||
public:
|
||||
~Graph();
|
||||
|
||||
// Return true if there is a path from u to v
|
||||
virtual bool contains(const T& u, const T& v) = 0;
|
||||
|
||||
// Add edge e(u,v)
|
||||
virtual void insert(const T& u, const T& v);
|
||||
|
||||
// Return true if e(u,v) exists
|
||||
virtual bool contains(const T& u, const T& v);
|
||||
|
||||
// Return true if vertex u exists
|
||||
virtual bool contains(const T& u);
|
||||
virtual void insert(const T& u, const T& v) = 0;
|
||||
|
||||
// Remove edge e(u,v)
|
||||
virtual void remove(const T& u, const T& v);
|
||||
virtual void remove(const T& u, const T& v) = 0;
|
||||
|
||||
// Return num. of vertices
|
||||
virtual std::uint16_t V();
|
||||
std::uint16_t V();
|
||||
|
||||
// Return num. of edges
|
||||
virtual std::uint16_t E();
|
||||
|
||||
// Output graph
|
||||
virtual void output();
|
||||
std::uint16_t E();
|
||||
|
||||
// Adjacency matrix representation
|
||||
std::map<T, std::set<T>> adjMatrix;
|
||||
|
||||
friend std::ostream& operator<<<>(std::ostream& os, Graph<T>& G);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
@@ -42,27 +42,6 @@ Graph<T>::~Graph() {
|
||||
adjMatrix.clear();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void Graph<T>::insert(const T& u, const T& v) {
|
||||
Graph<T>::adjMatrix[u].insert(v);
|
||||
Graph<T>::adjMatrix[v];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Graph<T>::contains(const T& u, const T& v) {
|
||||
return adjMatrix[u].contains(v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Graph<T>::contains(const T& u) {
|
||||
return adjMatrix.contains(u);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Graph<T>::remove(const T& u, const T& v) {
|
||||
adjMatrix[u].erase(v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::uint16_t Graph<T>::V() {
|
||||
return static_cast<std::uint16_t>(adjMatrix.size());
|
||||
@@ -78,14 +57,18 @@ std::uint16_t Graph<T>::E() {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Graph<T>::output() {
|
||||
for (const auto& v : adjMatrix) {
|
||||
for (const auto& u : v.second) {
|
||||
std::cout << v.first << "->" << u << '|';
|
||||
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 << ' ';
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
std::cout << '\n';
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace graph
|
||||
|
||||
Reference in New Issue
Block a user