Files
reachability-algorithms/graph/graph.h
2022-06-11 19:27:03 +03:00

71 lines
1.2 KiB
C++

#ifndef GRAPH_H_
#define GRAPH_H_
#include <map>
#include <set>
#include <utility>
#include <vector>
#include <iostream>
namespace graph {
template<typename T>
class Graph {
public:
Graph() = default;
// Add vertex v
void insert(const T& v);
// Add edge between v and u
void insert(const T& v, const T& u);
// Reverse graph directions
Graph<T> reverse();
// Number of vertices
std::uint16_t V();
// Number of edges
// TODO: Calculate bidirectional edges once
std::uint16_t E();
// Adjacency matrix representation
std::set<T> vertices;
std::map<T, std::set<T>> adjMatrix;
};
template<typename T>
void Graph<T>::insert(const T& v) {
vertices.insert(v);
}
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>();
}
template<typename T>
std::uint16_t Graph<T>::V() {
return 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();
}
return edges;
}
} // namespace graph
#endif