Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
GraphAlgorithms.h
Go to the documentation of this file.
1 
8 #ifndef INCLUDE_MOLASSEMBLER_OUTER_GRAPH_ALGORITHMS_H
9 #define INCLUDE_MOLASSEMBLER_OUTER_GRAPH_ALGORITHMS_H
10 
12 #include "Types.h"
13 #include "boost/functional/hash.hpp"
14 
15 #include <unordered_map>
16 #include <vector>
17 #include <memory>
18 #include <limits>
19 
20 namespace Scine {
21 namespace Molassembler {
22 
23 // Forward-declarations
24 class Graph;
25 
38 MASM_EXPORT std::vector<unsigned> distance(AtomIndex source, const Graph& graph);
39 
40 struct MASM_EXPORT PredecessorMap {
41  std::vector<AtomIndex> predecessors;
42 
51  std::vector<AtomIndex> path(AtomIndex target) const;
52 };
53 
64 MASM_EXPORT PredecessorMap shortestPaths(AtomIndex source, const Graph& graph);
65 
74 struct MASM_EXPORT EditCost {
75  virtual ~EditCost() = default;
76 
77  // Alteration means insertion or deletion
78  virtual unsigned vertexAlteration() const = 0;
79  virtual unsigned edgeAlteration() const = 0;
80  virtual unsigned elementSubstitution(Utils::ElementType from, Utils::ElementType to) const = 0;
81  virtual unsigned bondSubstitution(BondType from, BondType to) const = 0;
82 };
83 
91 struct MASM_EXPORT FuzzyCost : public EditCost {
92  unsigned vertexAlteration() const override { return 1; }
93  unsigned edgeAlteration() const override { return 1; }
94  unsigned elementSubstitution(Utils::ElementType from, Utils::ElementType to) const override {
95  return (from != to) ? 1 : 0;
96  }
97  unsigned bondSubstitution(BondType from, BondType to) const override {
98  return (from != to) ? 1 : 0;
99  }
100 };
101 
109 struct MASM_EXPORT ElementsConservedCost : public FuzzyCost {
110  unsigned vertexAlteration() const override { return 100; }
111  unsigned elementSubstitution(Utils::ElementType from, Utils::ElementType to) const override {
112  return (from != to) ? 100 : 0;
113  }
114 };
115 
119 struct MASM_EXPORT MinimalGraphEdits {
121  using IndexMap = std::vector<AtomIndex>;
122 
124  struct VertexEdit {
125  VertexEdit() = default;
126  VertexEdit(AtomIndex i, AtomIndex j, unsigned c) : first(i), second(j), cost(c) {}
127 
133  unsigned cost;
134  };
135 
137  struct EdgeEdit {
138  EdgeEdit() = default;
139  EdgeEdit(BondIndex i, BondIndex j, unsigned c) : first(i), second(j), cost(c) {}
140 
146  unsigned cost;
147  };
148 
150  static constexpr AtomIndex epsilon = std::numeric_limits<AtomIndex>::max();
151 
153  unsigned distance;
157  std::vector<VertexEdit> vertexEdits;
159  std::vector<EdgeEdit> edgeEdits;
160 };
161 
176 MASM_EXPORT MinimalGraphEdits minimalEdits(const Graph& a, const Graph& b, const EditCost& cost = FuzzyCost {});
177 
179 struct MASM_EXPORT MinimalReactionEdits {
181  using ComponentIndexPair = std::pair<unsigned, AtomIndex>;
182 
184  struct VertexEdit {
185  VertexEdit() = default;
187  : first(std::move(i)), second(std::move(j)), cost(c) {}
188 
194  unsigned cost;
195  };
196 
198  struct EdgeEdit {
200  using ComponentBondIndex = std::pair<ComponentIndexPair, ComponentIndexPair>;
201 
202  EdgeEdit() = default;
204  : first(std::move(i)), second(std::move(j)), cost(c) {}
205 
211  unsigned cost;
212  };
213 
215  unsigned distance;
217  std::unordered_map<ComponentIndexPair, ComponentIndexPair, boost::hash<ComponentIndexPair>> indexMap;
219  std::vector<VertexEdit> vertexEdits;
221  std::vector<EdgeEdit> edgeEdits;
222 };
223 
225 using GraphList = std::vector<std::reference_wrapper<Graph>>;
226 
247 MASM_EXPORT MinimalReactionEdits reactionEdits(const GraphList& lhsGraphs, const GraphList& rhsGraphs);
248 
256 std::string reactionGraphvizSvg(
257  const GraphList& lhs,
258  const GraphList& rhs,
259  const MinimalReactionEdits& edits
260 );
261 
262 } // namespace Molassembler
263 } // namespace Scine
264 
265 #endif
Type for non-zero cost edge edits in the result set.
Definition: GraphAlgorithms.h:198
std::unordered_map< ComponentIndexPair, ComponentIndexPair, boost::hash< ComponentIndexPair > > indexMap
Vertex and component mapping.
Definition: GraphAlgorithms.h:217
std::vector< EdgeEdit > edgeEdits
List of non-zero-cost edge edits.
Definition: GraphAlgorithms.h:159
std::vector< VertexEdit > vertexEdits
List of non-zero-cost vertex edits.
Definition: GraphAlgorithms.h:219
Defines basic types widely shared across the project.
Fuzzy-matching cost function that prefers element substitutions to alterations.
Definition: GraphAlgorithms.h:91
std::vector< EdgeEdit > edgeEdits
List of non-zero-cost edge edits.
Definition: GraphAlgorithms.h:221
std::string reactionGraphvizSvg(const GraphList &lhs, const GraphList &rhs, const MinimalReactionEdits &edits)
MinimalReactionEdits reactionEdits(const GraphList &lhsGraphs, const GraphList &rhsGraphs)
Graph edit distance calculation for reactions.
Type for non-zero cost edge edits in the result set.
Definition: GraphAlgorithms.h:137
ComponentIndexPair first
Left graph index and component.
Definition: GraphAlgorithms.h:190
unsigned cost
Cost of the edit per the cost function.
Definition: GraphAlgorithms.h:211
Type for non-zero cost vertex edits in the result set.
Definition: GraphAlgorithms.h:124
Represents the connectivity of atoms of a molecule.
Definition: Graph.h:54
std::vector< unsigned > distance(AtomIndex source, const Graph &graph)
Calculates the graph distance from a single atom index to all others.
MinimalGraphEdits minimalEdits(const Graph &a, const Graph &b, const EditCost &cost=FuzzyCost{})
Graph edit distance calculation.
Result type for single-pair graph edit distance calculation.
Definition: GraphAlgorithms.h:119
ComponentBondIndex second
Right bond.
Definition: GraphAlgorithms.h:209
std::vector< VertexEdit > vertexEdits
List of non-zero-cost vertex edits.
Definition: GraphAlgorithms.h:157
unsigned cost
Cost of the edit per the cost function.
Definition: GraphAlgorithms.h:194
constexpr Get< 1 > second
Calls std::get&lt;1&gt; on any argument it is invoked with.
Definition: Functor.h:125
unsigned cost
Cost of the edit per the cost function.
Definition: GraphAlgorithms.h:146
unsigned distance
Total distance between graphs according to cost function.
Definition: GraphAlgorithms.h:153
std::pair< unsigned, AtomIndex > ComponentIndexPair
Input component and atom index therein.
Definition: GraphAlgorithms.h:181
std::size_t AtomIndex
Unsigned integer atom index type. Used to refer to particular atoms.
Definition: Types.h:51
AtomIndex first
Left graph index (possibly epsilon)
Definition: GraphAlgorithms.h:129
BondType
Discrete bond type numeration.
Definition: Types.h:26
Element type conserving cost function, really allowing only edge alterations.
Definition: GraphAlgorithms.h:109
Type for non-zero cost vertex edits in the result set.
Definition: GraphAlgorithms.h:184
BondIndex second
Right graph bond index (possibly containing epsilon values)
Definition: GraphAlgorithms.h:144
constexpr Get< 0 > first
Calls std::get&lt;0&gt; on any argument it is invoked with.
Definition: Functor.h:123
Type used to refer to particular bonds. Orders first &lt; second.
Definition: Types.h:54
unsigned cost
Cost of the edit per the cost function.
Definition: GraphAlgorithms.h:133
Definition: GraphAlgorithms.h:40
ComponentBondIndex first
Left bond.
Definition: GraphAlgorithms.h:207
Base class for edit cost functors for graph edit distance methods.
Definition: GraphAlgorithms.h:74
std::pair< ComponentIndexPair, ComponentIndexPair > ComponentBondIndex
Type for two component and index pairs.
Definition: GraphAlgorithms.h:200
unsigned distance
Minimal edit cost between graphs.
Definition: GraphAlgorithms.h:215
Aggregate for multiple-graph graph edit distance.
Definition: GraphAlgorithms.h:179
IndexMap indexMap
Vertex mapping, possibly including epsilon values.
Definition: GraphAlgorithms.h:155
BondIndex first
Left graph bond index (possibly containing epsilon values)
Definition: GraphAlgorithms.h:142
std::vector< std::reference_wrapper< Graph >> GraphList
Input type for multiple graphs.
Definition: GraphAlgorithms.h:225
ComponentIndexPair second
Right graph index and component.
Definition: GraphAlgorithms.h:192
std::vector< AtomIndex > IndexMap
Flat vertex index map between the graphs.
Definition: GraphAlgorithms.h:121
PredecessorMap shortestPaths(AtomIndex source, const Graph &graph)
Generates shortest paths to each vertex in a graph.
AtomIndex second
Right graph index (possibly epsilon)
Definition: GraphAlgorithms.h:131