aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
DFSTree.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Headers of the DFSTree class.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_DFS_TREE_H
30 #define GUM_DFS_TREE_H
31 
32 #include <list>
33 #include <ostream>
34 #include <utility>
35 #include <vector>
36 
37 #include <agrum/tools/core/math/math_utils.h>
38 #include <agrum/tools/core/bijection.h>
39 #include <agrum/tools/core/sequence.h>
40 #include <agrum/tools/core/set.h>
41 
42 #include <agrum/tools/graphs/diGraph.h>
43 
44 #include <agrum/tools/graphs/algorithms/triangulations/partialOrderedTriangulation.h>
45 
46 #include <agrum/PRM/gspan/edgeGrowth.h>
47 #include <agrum/PRM/gspan/interfaceGraph.h>
48 #include <agrum/PRM/gspan/pattern.h>
49 
50 #include <agrum/PRM/gspan/searchStrategy.h>
51 
52 namespace gum {
53  namespace prm {
54 
55  template < typename GUM_SCALAR >
56  class GSpan;
57 
58  namespace gspan {
59  template < typename GUM_SCALAR >
60  class SearchStrategy;
61 
62  /**
63  * @class DFSTree
64  * @headerfile DFSTree.h <agrum/PRM/gspan/DFSTree.h>
65  *
66  * A DFSTree is used by gspan to sort lexicographically patterns
67  * discovered in an interface graph.
68  */
69  template < typename GUM_SCALAR >
70  class DFSTree: private DiGraph {
71  public:
72  // =========================================================================
73  /// @name Constructor and destructor.
74  // ==========================================================================
75  /// @{
76 
77  /// Default constructor.
78  // DFSTree(InterfaceGraph<GUM_SCALAR>* graph);
79  DFSTree(const InterfaceGraph< GUM_SCALAR >& graph,
80  SearchStrategy< GUM_SCALAR >* strategy = 0);
81 
82  /// Destructor.
83  ~DFSTree();
84 
85  /// @}
86  // =========================================================================
87  /// @name DFSTree getters and setters.
88  // ==========================================================================
89  /// @{
90 
91  const InterfaceGraph< GUM_SCALAR >& graph() const;
92 
93  struct PatternData {
94  /// Constructor.
95  explicit PatternData(Pattern* p);
96  /// Copy constructor.
97  PatternData(const PatternData& from);
98  /// Destructor.
99  ~PatternData();
100  /// The pattern.
101  Pattern* pattern;
102  /// The list of the pattern's children, sorted lexicographically.
103  std::list< NodeId > children;
104  /// The isomorphism graph of the pattern.
105  UndiGraph iso_graph;
106  /// The instances matching p in the interface graph.
107  NodeProperty< Sequence< PRMInstance< GUM_SCALAR >* >* > iso_map;
108  /// The maximal independent set of p.
109  Set< NodeId > max_indep_set;
110  /// The cost of this Pattern
111  Size cost;
112  /// The gain of this Pattern
113  Size gain;
114  };
115 
116  /// Returns the list of root patterns in this DFSTree.
117  std::list< NodeId >& roots();
118 
119  /// Returns the list of root patterns in this DFSTree.
120  const std::list< NodeId >& roots() const;
121 
122  /// Returns the parent of p in this DFSTree.
123  Pattern& parent(const Pattern& p);
124 
125  /// Returns the parent of p in this DFSTree.
126  const Pattern& parent(const Pattern& p) const;
127 
128  /// Returns the list of p children in this DFSTree.
129  std::list< NodeId >& children(const Pattern& p);
130 
131  /// Returns the list of p children in this DFSTree.
132  const std::list< NodeId >& children(const Pattern& p) const;
133 
134  /// Returns the pattern represented by id in this DFSTree.
135  Pattern& pattern(NodeId id);
136 
137  /// Returns the pattern represented by id in this DFSTree.
138  const Pattern& pattern(NodeId id) const;
139 
140  /**
141  * @brief Add a one edge Pattern in this DFSTree.
142  *
143  * @param data Data over the edge used to create a root of this DFSTree.
144  * @return Returns the Pattern added as a root of this DFSTree.
145  */
146  void addRoot(LabelData& data);
147 
148  /**
149  * @brief Add a one edge growth of p as one of its child.
150  *
151  * The child is inserted lexicographically among the children of p.
152  * However if the child is found to be not minimal an
153  * OperationNotAllowed is raised.
154  *
155  * @param p The Pattern from which a one edge growth is spawned.
156  * @param edge_growth The data about the edge growth of p.
157  * @param min_freq minimum number of occurrence to be used as a pattern
158  *
159  * @throw FatalError Raised if the grow is an illegal backedge growth.
160  * @throw OperationNotAllowed Raised if the grow is found to be not
161  *minimal.
162  */
163  Pattern& growPattern(Pattern& p, EdgeGrowth< GUM_SCALAR >& edge_growth, Size min_freq);
164 
165  /// @}
166  // =========================================================================
167  /// @name Isomorphisms for patterns in this DFSTree.
168  // ==========================================================================
169  /// @{
170 
171  /**
172  * @brief Returns the isomorphism graph of p in the interface graph.
173  *
174  * The isomorphism graph is a undirected graph in which each node
175  *represents
176  * a set of PRMInstance<GUM_SCALAR> matching p in the interface graph.
177  *
178  * If there exists an edge between two nodes in the isomorphism graph,
179  *then
180  * the two respective set of instances are not disjoint.
181  *
182  * @param p The pattern for which we want the isomorphism graph.
183  * @return The isomorphism graph of p.
184  *
185  * @throw NotFound Raised if p is not a node in this DFSTree.
186  */
187  UndiGraph& iso_graph(const Pattern& p);
188 
189  /**
190  * @brief Given a pattern and a node in its isomorphism graph, this
191  *methods
192  * returns the sequence of instance matching p in the interface
193  *graph.
194  *
195  * The sequence of instances respect DSF subscripting. Each node in the
196  * pattern's graph have a DSF subscript from 1 to n, where n is the
197  *number
198  * of nodes in the pattern's graph.
199  *
200  * If for a given match you want the k-th instance repecting p's DFS
201  *subscripting,
202  * then it will be the (k - 1)th element in the sequence.
203  *
204  * @param p The pattern for which we want a match in the interface
205  *graph.
206  * @param node The node in p isomorphism graph for which we want the
207  *matching
208  * set if instances.
209  * @return Returns the sequence of instances matching p and node.
210  *
211  * @throw NotFound Raised if p or node does not exists.
212  */
213  Sequence< PRMInstance< GUM_SCALAR >* >& iso_map(const Pattern& p, NodeId node);
214 
215  /**
216  * @brief Returns the maximal independent set of p isomorphism graph.
217  *
218  * @param p The pattern for which we want its maximal independent set.
219  *
220  * @throw NotFound Raised if p is not a node in this DFSTree.
221  */
222  Set< NodeId >& max_indep_set(const Pattern& p);
223 
224  /// Returns the frequency of p respecting it's maximal independent set.
225  /// @param p The pattern
226  double frequency(const Pattern& p) const;
227 
228  /// @param p The pattern
229  PatternData& data(const Pattern& p);
230  /// @param p The pattern
231  const PatternData& data(const Pattern& p) const;
232 
233  /// strategy getter
234  SearchStrategy< GUM_SCALAR >& strategy();
235 
236  /// strategy getter
237  const SearchStrategy< GUM_SCALAR >& strategy() const;
238 
239  /** @class NeighborDegreeSort
240  * @headerfile DFSTree.h <agrum/PRM/gspan/DFSTree.h>
241  * @brief This is used to generate the max_indep_set of a Pattern.
242  */
243  struct NeighborDegreeSort {
244  /// Constructor
245  explicit NeighborDegreeSort(UndiGraph& graph);
246  /// Copy constructor.
247  NeighborDegreeSort(const NeighborDegreeSort& source);
248  /// Destructor.
249  ~NeighborDegreeSort();
250  /// The operator used to sort stuff.
251  bool operator()(NodeId i, NodeId j);
252  /// The isomorphism graph.
253  UndiGraph& g;
254  };
255 
256  /// @}
257 
258  private:
259  /// The interface graph on which this DFSTree applies.
260  const InterfaceGraph< GUM_SCALAR >* _graph_;
261 
262  /// The list of root patterns in this DFSTree.
263  std::list< NodeId > _roots_;
264 
265  /// The mapping between nodes in this DFSTree and the patterns they
266  /// represents.
267  Bijection< NodeId, Pattern* > _node_map_;
268 
269  /// Data about patterns in this DFSTree.
270  HashTable< Pattern*, PatternData* > _data_;
271 
272  /// The strategy used to prune the search tree.
273  SearchStrategy< GUM_SCALAR >* _strategy_;
274 
275  /// Raise different exceptions if child is invalid or illegal
276  void _checkGrowth_(Pattern& p, Pattern* child, EdgeGrowth< GUM_SCALAR >& edge_growth);
277 
278  /// Add a child to this DFSTree.
279  void _addChild_(Pattern& p, Pattern* child, EdgeGrowth< GUM_SCALAR >& edge_growth);
280 
281  /// Check if an instance match is redundant.
282  bool _is_new_seq_(Sequence< PRMInstance< GUM_SCALAR >* >& seq,
283  NodeProperty< Sequence< PRMInstance< GUM_SCALAR >* >* >& iso_map);
284 
285  /// This initialize the DSFTree with a new root.
286  /// @param p A Pattern.
287  /// @param seq A sequence of EdgeData<GUM_SCALAR>.
288  void _initialiaze_root_(Pattern* p, Sequence< EdgeData< GUM_SCALAR >* >& seq);
289 
290  // Used by _find_sub_pattern_.
291  bool _test_equality_(HashTable< PRMClassElement< GUM_SCALAR >*, Size >& x,
292  HashTable< PRMClassElement< GUM_SCALAR >*, Size >& y);
293  };
294 
295 
296 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
297  extern template class DFSTree< double >;
298 #endif
299 
300 
301  } /* namespace gspan */
302  } /* namespace prm */
303 } /* namespace gum */
304 
305 #include <agrum/PRM/gspan/DFSTree_tpl.h>
306 
307 #endif /* GUM_DFS_TREE_H */