indigoX
base_graph.hpp
Go to the documentation of this file.
1 
2 #ifndef INDIGOX_GRAPH_BASE_HPP
3 #define INDIGOX_GRAPH_BASE_HPP
4 
5 #include "../utils/fwd_declares.hpp"
6 #include "../utils/simple_bimap.hpp"
7 #include <boost/graph/adjacency_list.hpp>
8 
9 #include <cstdint>
10 #include <memory>
11 #include <vector>
12 
14 namespace indigox::graph {
15 
17  struct Directed {
19  using is_directed_t = boost::bidirectionalS;
21  static constexpr bool is_directed = true;
22  };
23 
25  struct Undirected {
27  using is_directed_t = boost::undirectedS;
29  static constexpr bool is_directed = false;
30  };
31 
33  struct GraphLabel {
35  union {
37  int32_t component;
39  uint64_t isomorphism;
40  };
41  };
42 
54  template <class V, class E, class S, class D = Undirected,
55  class VP = GraphLabel, class EP = GraphLabel>
56  class BaseGraph {
58  friend class cereal::access;
60  friend struct algorithm::access;
61 
62  public:
64  using graph_type =
65  boost::adjacency_list<boost::setS, // Edge container
66  boost::listS, // Vertex container
67  typename D::is_directed_t, // Directed nature
68  VP, // Vertex Properties
69  EP>; // Edge Properties
70 
71  // May need to replace graph_t:: with boost::graph_traits<graph_t>::
73  using VertType = typename graph_type::vertex_descriptor;
75  using VertIter = typename graph_type::vertex_iterator;
77  using NbrsIter = typename graph_type::adjacency_iterator;
79  using PredIter = typename graph_type::inv_adjacency_iterator;
81  using EdgeType = typename graph_type::edge_descriptor;
83  using EdgeIter = typename graph_type::edge_iterator;
84 
90  using VertContain = std::vector<V>;
92  using EdgeContain = std::vector<E>;
94  using NbrsContain = std::map<V, VertContain>;
96  using ComponentContain = std::vector<VertContain>;
98  using CycleVertContain = std::vector<VertContain>;
100  using CycleEdgeContain = std::vector<EdgeContain>;
101  using SubgraphType = S;
102 
103  protected:
104  struct BaseImpl;
105  std::shared_ptr<BaseImpl> m_basedata;
106 
107  // //! \brief Underlying boost graph.
108  // graph_type _g;
109  // //! \brief Map vertices to their descriptors.
110  // VertMap _vm;
111  // //! \brief Map edges to their descriptors.
112  // EdgeMap _em;
113  // //! \brief Container for giving access to all vertices
114  // VertContain _v;
115  // //! \brief Container for giving access to all edges
116  // EdgeContain _e;
117  // //! \brief Container for predecessors of a vertex (v such that edge
118  // u->v
119  // //! exists)
120  // NbrsContain _pre;
121  // //! \brief Container for successors of a vertex. Only used in directed
122  // //! graphs
123  // NbrsContain _suc;
124  // //! \brief Components container
125  // ComponentContain _comp_cache;
126  // //! \brief State when components were last calculated
127  // utils::ModifiableObject::State _comp_state;
128  // //! \brief Cyclic vertices container
129  // VertContain _vcyclic_cache;
130  // //! \brief Cyclic edges container
131  // EdgeContain _ecyclic_cache;
132  // //! \brief Cycles container
133  // CycleEdgeContain _cycles_cache;
134  // //! \brief State when cycles were last calculated
135  // utils::ModifiableObject::State _cycle_state;
136 
137  private:
138  template <typename Archive>
139  void serialise(Archive &archive, const uint32_t);
140 
141  public:
143  BaseGraph() : m_basedata(std::make_shared<BaseImpl>()){};
144 
145  virtual ~BaseGraph() {}
146 
147  protected:
148  // Modification methods protected.
149  // void Clear();
150 
157  void AddVertex(const V &v);
158 
164  void RemoveVertex(const V &v);
165 
172  void AddEdge(const V &u, const V &v, const E &e);
173 
178  void RemoveEdge(const E &e);
179 
184  void RemoveEdge(const V &u, const V &v);
185 
186  public:
190  bool HasVertex(const V &v) const;
191 
195  bool HasEdge(const E &e) const;
196 
200  bool HasEdge(const V &u, const V &v) const;
201 
204  int64_t NumVertices() const;
205 
208  int64_t NumEdges() const;
209 
215  int64_t Degree(const V &v) const;
216 
224  int64_t InDegree(const V &v) const;
225 
234  const VertContain &GetNeighbours(const V &v);
235 
245  const VertContain &GetPredecessors(const V &v);
246 
247  const VertContain &GetSuccessors(const V &v);
248 
255  std::pair<V, V> GetVertices(const E &e) const;
256 
261  const VertContain &GetVertices() const;
262 
267  const EdgeContain &GetEdges() const;
268 
274  E GetEdge(const V &u, const V &v) const;
275 
281  V GetSourceVertex(const E &e) const;
282 
288  V GetTargetVertex(const E &e) const;
289 
292  bool IsConnected();
293 
296  int64_t NumConnectedComponents();
297 
301 
305  bool IsCyclic(const V &v);
306 
307  bool IsCyclic(const V &v, uint32_t sz);
308 
312  bool IsCyclic(const E &e);
313 
314  bool IsCyclic(const E &e, uint32_t sz);
315 
318  const CycleEdgeContain &GetCycles();
319 
320  int64_t NumCycles();
321 
322  virtual S Subgraph(std::vector<V> &verts) = 0;
323  virtual S Subgraph(std::vector<V> &verts, std::vector<E> &edges) = 0;
324 
325  private:
329  VertType GetDescriptor(const V &v) const;
330 
334  V GetV(VertType v) const;
335 
339  EdgeType GetDescriptor(const E &e) const;
340 
344  E GetE(EdgeType e) const;
345 
349  int64_t OutDegree(VertType v) const;
350 
354  int64_t InDegree(VertType v) const;
355  };
356 
357 } // namespace indigox::graph
358 
359 #include "base_graph_impl.hpp"
360 
361 #endif /* INDIGOX_GRAPH_BASE_HPP */
int64_t NumVertices() const
Number of vertices in the graph.
Definition: base_graph_impl.hpp:204
Definition: simple_bimap.hpp:7
virtual ~BaseGraph()
Definition: base_graph.hpp:145
bool HasVertex(const V &v) const
Is the vertex in the graph.
Definition: base_graph_impl.hpp:187
std::vector< CMGEdge > EdgeContain
Type for storing edges.
Definition: base_graph.hpp:92
std::vector< VertContain > CycleVertContain
Type for storing vertex cycles.
Definition: base_graph.hpp:98
Definition: assignment.hpp:16
const EdgeContain & GetEdges() const
Get the edges of the graph.
Definition: base_graph_impl.hpp:263
BaseGraph()
Default constructor.
Definition: base_graph.hpp:143
static constexpr bool is_directed
Boolean that the type is not directed.
Definition: base_graph.hpp:29
const VertContain & GetVertices() const
Get the vertices of the graph.
Definition: base_graph_impl.hpp:259
Definition: condensed.hpp:131
int64_t Degree(const V &v) const
Degree of a vertex.
Definition: base_graph_impl.hpp:212
V GetSourceVertex(const E &e) const
Get the source vertex of an edge.
Definition: base_graph_impl.hpp:275
bool IsConnected()
Determine if the graph is connected.
Definition: base_graph_impl.hpp:315
V GetTargetVertex(const E &e) const
Get the target vertex of an edge.
Definition: base_graph_impl.hpp:279
int64_t InDegree(const V &v) const
Indegree of a vertex.
Definition: base_graph_impl.hpp:216
Type for specifying that a graph is directed.
Definition: base_graph.hpp:17
Definition: access.hpp:8
typename graph_type::inv_adjacency_iterator PredIter
Type for iterator over predecessors of a vertex descriptor.
Definition: base_graph.hpp:79
bool IsCyclic(const V &v)
Determine if a vertex of this graph is cyclic.
Definition: base_graph_impl.hpp:335
typename graph_type::vertex_descriptor VertType
Type of the graph vertex descriptor.
Definition: base_graph.hpp:73
std::shared_ptr< BaseImpl > m_basedata
Definition: base_graph.hpp:104
bool HasEdge(const E &e) const
Is the edge in the graph.
Definition: base_graph_impl.hpp:192
std::map< CMGVertex, VertContain > NbrsContain
Type for storing neighbours.
Definition: base_graph.hpp:94
const VertContain & GetNeighbours(const V &v)
Get the neighbouring vertices of a vertex.
Definition: base_graph_impl.hpp:222
Type for applying a numerical label to a vertex or edge.
Definition: base_graph.hpp:33
std::vector< VertContain > ComponentContain
Type for storing components.
Definition: base_graph.hpp:96
void RemoveEdge(const E &e)
Remove an edge from the graph.
Definition: base_graph_impl.hpp:173
const VertContain & GetSuccessors(const V &v)
Definition: base_graph_impl.hpp:242
void RemoveVertex(const V &v)
Remove a vertex from the graph.
Definition: base_graph_impl.hpp:129
static constexpr bool is_directed
Boolean that the type is directed.
Definition: base_graph.hpp:21
int64_t NumEdges() const
Number of edges in the graph.
Definition: base_graph_impl.hpp:208
const CycleEdgeContain & GetCycles()
Get the cycles of the graph.
Definition: base_graph_impl.hpp:373
std::vector< CMGVertex > VertContain
Type for storing vertices.
Definition: base_graph.hpp:90
int64_t NumConnectedComponents()
Get the number of connected components of the graph.
Definition: base_graph_impl.hpp:320
boost::bidirectionalS is_directed_t
Underlying boost type of a directed graph.
Definition: base_graph.hpp:19
virtual S Subgraph(std::vector< V > &verts)=0
boost::adjacency_list< boost::setS, boost::listS, typename Undirected ::is_directed_t, GraphLabel, GraphLabel > graph_type
Type of the underlying boost graph.
Definition: base_graph.hpp:69
std::vector< EdgeContain > CycleEdgeContain
Type for storing edge cycles.
Definition: base_graph.hpp:100
const VertContain & GetPredecessors(const V &v)
Get the predecessor vertices of a vertex.
Definition: base_graph_impl.hpp:232
E GetEdge(const V &u, const V &v) const
Get the edge between two vertices.
Definition: base_graph_impl.hpp:267
Type for specifying that a graph is undirected.
Definition: base_graph.hpp:25
typename graph_type::edge_iterator EdgeIter
Type for iterator over edges.
Definition: base_graph.hpp:83
void AddEdge(const V &u, const V &v, const E &e)
Add a new edge to the graph.
Definition: base_graph_impl.hpp:166
void AddVertex(const V &v)
Add a new vertex to the graph.
Definition: base_graph_impl.hpp:124
uint64_t isomorphism
Label used for (sub-)graph isomorphism.
Definition: base_graph.hpp:39
boost::undirectedS is_directed_t
Underlying boost type of an undirected graph.
Definition: base_graph.hpp:27
Template base class for all graphs used in the indigoX library.
Definition: base_graph.hpp:56
typename graph_type::vertex_iterator VertIter
Type for iterator over graph vertex descriptors.
Definition: base_graph.hpp:75
int64_t NumCycles()
Definition: base_graph_impl.hpp:403
const ComponentContain & GetConnectedComponents()
Get the connected components of the graph.
Definition: base_graph_impl.hpp:325
typename graph_type::edge_descriptor EdgeType
Type of the graph edge descriptor.
Definition: base_graph.hpp:81
typename graph_type::adjacency_iterator NbrsIter
Type for iterator over neighbours of vertex descriptor.
Definition: base_graph.hpp:77
int32_t component
Label used by the connected components algorithm.
Definition: base_graph.hpp:37