#ifndef GRAPH_H_ #define GRAPH_H_ #include #include #include namespace graph { // Forward declerations template class Graph; template std::ostream& operator<<(std::ostream& os, Graph& G); template 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) = 0; // Remove edge e(u,v) virtual void remove(const T& u, const T& v) = 0; // Return num. of vertices std::uint16_t V(); // Return num. of edges std::uint16_t E(); // Adjacency matrix representation std::map> adjMatrix; friend std::ostream& operator<<<>(std::ostream& os, Graph& G); }; template Graph::~Graph() { adjMatrix.clear(); } template std::uint16_t Graph::V() { return static_cast(adjMatrix.size()); } template std::uint16_t Graph::E() { std::uint16_t edges = 0; for (const auto& v : adjMatrix) { edges += static_cast(v.second.size()); } return static_cast(edges); } template std::ostream& operator<<(std::ostream& os, Graph& 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'; } } os << '\n'; return os; } } // namespace graph #endif