Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
PrivateGraph.h
Go to the documentation of this file.
1 
8 #ifndef INCLUDE_MOLASSEMBLER_INNER_GRAPH_H
9 #define INCLUDE_MOLASSEMBLER_INNER_GRAPH_H
10 
11 #include "boost/graph/adjacency_list.hpp"
13 
14 #include "Utils/Typenames.h"
15 
16 #include "Molassembler/Cycles.h"
17 
18 #include <limits>
19 
20 namespace Scine {
21 namespace Molassembler {
22 
26 class PrivateGraph {
27 public:
31  struct VertexData {
32  Utils::ElementType elementType;
33  };
34 
36  struct EdgeData {
37  BondType bondType;
38  };
39 
44  using BglType = boost::adjacency_list<
45  /* OutEdgeListS = Type of Container for edges of a vertex
46  * Options: vector, list, slist, set, multiset, unordered_set
47  * Choice: vecS, we have to rigorously test that no parallel edges are
48  * created
49  */
50  boost::vecS,
51  /* VertexListS = Type of Container for vertices
52  * Options: vector, list, slist, set, multiset, unordered_set
53  * Choice: vecS, removing vertices is rare, keep memory use limited
54  * Consequence: operation remove_vertex() invalidates:
55  * - Vertex descriptors / iterators
56  * - Edge descriptors / iterators
57  * - Adjacency iterators
58  */
59  boost::vecS,
60  // Molecular graphs are undirected
61  boost::undirectedS,
62  // VertexProperty: What information is stored about vertices?
63  VertexData,
64  // EdgeProperty: What information is stored about edges?
65  EdgeData
66  // GraphProperty: Omitted, defaults
67  // EdgeListS: Omitted, defaults
68  >;
69 
70  using Vertex = BglType::vertex_descriptor;
71  using Edge = BglType::edge_descriptor;
72 
77 
87  std::unordered_set<PrivateGraph::Vertex> articulationVertices;
89  std::set<PrivateGraph::Edge> bridges;
90  };
92 
96  PrivateGraph();
100 
103  PrivateGraph(const PrivateGraph& other);
104  PrivateGraph(PrivateGraph&& other);
105  PrivateGraph& operator = (const PrivateGraph& other);
106  PrivateGraph& operator = (PrivateGraph&& other) noexcept;
107  ~PrivateGraph();
109 
113  static constexpr Vertex removalPlaceholder = std::numeric_limits<Vertex>::max();
115 
118 
124 
129  Vertex addVertex(Utils::ElementType elementType);
130 
135  void applyPermutation(const std::vector<Vertex>& permutation);
136 
142  BondType& bondType(const Edge& edge);
143 
145  BglType& bgl();
146 
151  void clearVertex(Vertex a);
152 
158  Utils::ElementType& elementType(Vertex a);
159 
164  void removeEdge(const Edge& e);
165 
171  void removeVertex(Vertex a);
172 
183  std::unordered_map<Vertex, Vertex> merge(
184  const PrivateGraph& other,
185  const std::vector<Vertex>& copyVertices = {}
186  );
188 
192  bool adjacent(Vertex a, Vertex b) const;
193 
199  Utils::ElementType elementType(Vertex a) const;
205  BondType bondType(const Edge& edge) const;
207  const BglType& bgl() const;
208 
219  bool canRemove(Vertex a) const;
228  bool canRemove(const Edge& edge) const;
229 
234  unsigned connectedComponents() const;
235 
240  unsigned connectedComponents(std::vector<unsigned>& componentMap) const;
241 
247  Edge edge(Vertex a, Vertex b) const;
248 
253  boost::optional<Edge> edgeOption(Vertex a, Vertex b) const;
254 
256 
258  Vertex source(const Edge& edge) const;
260  Vertex target(const Edge& edge) const;
261 
266  unsigned degree(Vertex a) const;
267 
268  std::string graphviz() const;
269 
271  [[deprecated("Prefer V")]]
272  Vertex N() const;
274  [[deprecated("Prefer E")]]
275  Vertex B() const;
276 
278  Vertex V() const;
280  unsigned E() const;
281 
287  boost::optional<std::vector<AtomIndex>> modularIsomorphism(
288  const PrivateGraph& other,
289  AtomEnvironmentComponents components = AtomEnvironmentComponents::All
290  ) const;
291 
296  bool identicalGraph(const PrivateGraph& other) const;
297 
303  std::pair<
304  std::vector<AtomIndex>,
305  std::vector<AtomIndex>
306  > splitAlongBridge(Edge bridge) const;
307 
309  std::pair<
310  std::vector<AtomIndex>,
311  std::vector<AtomIndex>
312  > splitAlongBridge(AtomIndex left, const std::vector<AtomIndex>& right) const;
314 
324  void populateProperties() const;
326  const Cycles& cycles() const;
328  const RemovalSafetyData& removalSafetyData() const;
330  const Cycles& etaPreservedCycles() const;
332 
335  VertexRange vertices() const;
336  EdgeRange edges() const;
337  AdjacentVertexRange adjacents(Vertex a) const;
338  IncidentEdgeRange edges(Vertex a) const;
340 
344  bool operator == (const PrivateGraph& other) const;
345  inline bool operator != (const PrivateGraph& other) const {
346  return !(*this == other);
347  }
349 
350 private:
353  struct Properties {
354  boost::optional<RemovalSafetyData> removalSafetyDataOption;
355  boost::optional<Cycles> cyclesOption;
356  boost::optional<Cycles> etaPreservedCyclesOption;
357 
358  inline void invalidate() {
359  removalSafetyDataOption = boost::none;
360  cyclesOption = boost::none;
361  etaPreservedCyclesOption = boost::none;
362  }
363  };
364 
365  RemovalSafetyData generateRemovalSafetyData_() const;
366  Cycles generateCycles_() const;
367  Cycles generateEtaPreservedCycles_() const;
369 
377 };
378 
379 } // namespace Molassembler
380 } // namespace Scine
381 #endif
Definition: PrivateGraph.h:353
boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS, VertexData, EdgeData > BglType
Definition: PrivateGraph.h:68
Vertex V() const
Number of vertices in the graph.
std::unordered_set< PrivateGraph::Vertex > articulationVertices
Articulation vertices cannot be removed without disconnecting the graph.
Definition: PrivateGraph.h:87
std::unordered_map< Vertex, Vertex > merge(const PrivateGraph &other, const std::vector< Vertex > &copyVertices={})
Copy a vertices and edges into another graph.
const Cycles & cycles() const
Access cycle information of the graph.
std::set< PrivateGraph::Edge > bridges
Bridges are edges that cannot be removed without disconnecting the graph.
Definition: PrivateGraph.h:89
Information stored at each graph vertex.
Definition: PrivateGraph.h:31
BglType & bgl()
Referential access to the underlying BGL graph.
const Cycles & etaPreservedCycles() const
Access cycle information of the graph with eta bonds preserved.
bool operator==(const PrivateGraph &other) const
Full isomorphism comparison including element types and bond orders.
Class to explore cyclic structure of molecules.
unsigned E() const
Number of edges in the graph.
Edge edge(Vertex a, Vertex b) const
Make an edge descriptor from two vertex descriptors.
Vertex N() const
Number of vertices in the graph.
AtomEnvironmentComponents
For bitmasks grouping components of immediate atom environments.
Definition: Types.h:103
const RemovalSafetyData & removalSafetyData() const
Access removal safety information of the graph.
Data class to return removal safety information on the graph.
Definition: PrivateGraph.h:85
Wrapper class to make working with RDL in C++ more pleasant.
Definition: Cycles.h:44
void applyPermutation(const std::vector< Vertex > &permutation)
Apply a permutation to the graph.
unsigned connectedComponents() const
Number of connected components.
Information stored at each graph edge.
Definition: PrivateGraph.h:36
Homogeneous pair of iterators with begin and end member fns.
Definition: IteratorRange.h:26
void removeEdge(const Edge &e)
Removes an edge from the graph.
PrivateGraph()
Empty constructor.
std::pair< std::vector< AtomIndex >, std::vector< AtomIndex > > splitAlongBridge(Edge bridge) const
Determine which vertices belong to which side of a bridge edge.
BglType graph_
A directly owned Boost Library Graph.
Definition: PrivateGraph.h:373
std::string graphviz() const
Returns whether two vertices are adjacent.
void populateProperties() const
Access cycle information of the graph.
Vertex target(const Edge &edge) const
Target of an edge.
std::size_t AtomIndex
Unsigned integer atom index type. Used to refer to particular atoms.
Definition: Types.h:51
boost::optional< Edge > edgeOption(Vertex a, Vertex b) const
Get an edge descriptor from two vertex descriptors, get None if the edge doesn&#39;t exist.
Library internal graph class wrapping BGL types.
Definition: PrivateGraph.h:26
IteratorRange< BglType::adjacency_iterator > AdjacentVertexRange
Definition: PrivateGraph.h:75
bool canRemove(Vertex a) const
Determine whether a vertex can be safely removed.
BondType & bondType(const Edge &edge)
Fetch the bond type of an edge.
BondType
Discrete bond type numeration.
Definition: Types.h:26
BglType::edge_descriptor Edge
Definition: PrivateGraph.h:71
IteratorRange< BglType::out_edge_iterator > IncidentEdgeRange
Definition: PrivateGraph.h:76
Properties properties_
Property caching.
Definition: PrivateGraph.h:375
IteratorRange< BglType::edge_iterator > EdgeRange
Definition: PrivateGraph.h:74
unsigned degree(Vertex a) const
Number of substituents of a vertex.
static constexpr Vertex removalPlaceholder
Placeholder vertex index used to indicate that a vertex has been removed.
Definition: PrivateGraph.h:113
bool adjacent(Vertex a, Vertex b) const
Returns whether two vertices are adjacent.
Edge addEdge(Vertex a, Vertex b, BondType bondType)
Add an edge to the graph.
void removeVertex(Vertex a)
Removes a vertex from the graph.
Utils::ElementType & elementType(Vertex a)
Fetches the element type of a vertex.
bool identicalGraph(const PrivateGraph &other) const
Checks whether all edges present in *this are present in other.
IteratorRange< BglType::vertex_iterator > VertexRange
Definition: PrivateGraph.h:73
boost::optional< std::vector< AtomIndex > > modularIsomorphism(const PrivateGraph &other, AtomEnvironmentComponents components=AtomEnvironmentComponents::All) const
Modular isomorphism comparison.
BglType::vertex_descriptor Vertex
Definition: PrivateGraph.h:70
Vertex source(const Edge &edge) const
Source of an edge.
Vertex B() const
Number of edges in the graph.
Utils::ElementTypeCollection elementCollection() const
Returns whether two vertices are adjacent.
void clearVertex(Vertex a)
Removes all bonds involving a vertex.
bool operator!=(const PrivateGraph &other) const
Full isomorphism comparison including element types and bond orders.
Definition: PrivateGraph.h:345
Vertex addVertex(Utils::ElementType elementType)
Add a (disconnected) vertex to the graph.