aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DFSTree.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
49#ifndef GUM_DFS_TREE_H
50#define GUM_DFS_TREE_H
51
52#include <list>
53#include <ostream>
54#include <utility>
55#include <vector>
56
58
59namespace gum {
60 namespace prm {
61
62 template < typename GUM_SCALAR >
63 class GSpan;
64
65 namespace gspan {
66 template < typename GUM_SCALAR >
67 class SearchStrategy;
68
76 template < typename GUM_SCALAR >
77 class DFSTree: private DiGraph {
78 public:
79 // =========================================================================
81 // ==========================================================================
83
85 // DFSTree(InterfaceGraph<GUM_SCALAR>* graph);
88
90 ~DFSTree();
91
93 // =========================================================================
95 // ==========================================================================
97
99
122
124 std::list< NodeId >& roots();
125
127 const std::list< NodeId >& roots() const;
128
130 Pattern& parent(const Pattern& p);
131
133 const Pattern& parent(const Pattern& p) const;
134
136 std::list< NodeId >& children(const Pattern& p);
137
139 const std::list< NodeId >& children(const Pattern& p) const;
140
143
145 const Pattern& pattern(NodeId id) const;
146
153 void addRoot(LabelData& data);
154
170 Pattern& growPattern(Pattern& p, EdgeGrowth< GUM_SCALAR >& edge_growth, Size min_freq);
171
173 // =========================================================================
175 // ==========================================================================
177
194 UndiGraph& iso_graph(const Pattern& p);
195
221
230
233 double frequency(const Pattern& p) const;
234
236 PatternData& data(const Pattern& p);
238 const PatternData& data(const Pattern& p) const;
239
242
245
262
264
265 private:
268
270 std::list< NodeId > _roots_;
271
275
278
281
283 void _checkGrowth_(Pattern& p, Pattern* child, EdgeGrowth< GUM_SCALAR >& edge_growth);
284
286 void _addChild_(Pattern& p, Pattern* child, EdgeGrowth< GUM_SCALAR >& edge_growth);
287
291
296
297 // Used by _find_sub_pattern_.
300 };
301
302
303#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
304 extern template class DFSTree< double >;
305#endif
306
307
308 } /* namespace gspan */
309 } /* namespace prm */
310} /* namespace gum */
311
313
314#endif /* GUM_DFS_TREE_H */
Inline implementation of the DFSTree class.
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1594
DiGraph(Size nodes_size=HashTableConst::default_size, bool nodes_resize_policy=true, Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true)
default constructor
Definition diGraph.cpp:69
The class for generic Hash Tables.
Definition hashTable.h:637
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
Base class for undirected graphs.
Definition undiGraph.h:128
This class discovers pattern in a PRM<GUM_SCALAR>'s PRMSystem<GUM_SCALAR> to speed up structured infe...
Definition gspan.h:86
Abstract class representing an element of PRM class.
An PRMInstance is a Bayesian network fragment defined by a Class and used in a PRMSystem.
Definition PRMInstance.h:79
A DFSTree is used by gspan to sort lexicographically patterns discovered in an interface graph.
Definition DFSTree.h:77
Set< NodeId > & max_indep_set(const Pattern &p)
Returns the maximal independent set of p isomorphism graph.
Pattern & pattern(NodeId id)
Returns the pattern represented by id in this DFSTree.
std::list< NodeId > & children(const Pattern &p)
Returns the list of p children in this DFSTree.
const InterfaceGraph< GUM_SCALAR > & graph() const
Returns the list of root patterns in this DFSTree.
SearchStrategy< GUM_SCALAR > * _strategy_
The strategy used to prune the search tree.
Definition DFSTree.h:280
void _checkGrowth_(Pattern &p, Pattern *child, EdgeGrowth< GUM_SCALAR > &edge_growth)
Raise different exceptions if child is invalid or illegal.
std::list< NodeId > & roots()
Returns the list of root patterns in this DFSTree.
bool _test_equality_(HashTable< PRMClassElement< GUM_SCALAR > *, Size > &x, HashTable< PRMClassElement< GUM_SCALAR > *, Size > &y)
Pattern & parent(const Pattern &p)
Returns the parent of p in this DFSTree.
PatternData & data(const Pattern &p)
UndiGraph & iso_graph(const Pattern &p)
Returns the isomorphism graph of p in the interface graph.
void addRoot(LabelData &data)
Add a one edge Pattern in this DFSTree.
Definition DFSTree_tpl.h:69
Sequence< PRMInstance< GUM_SCALAR > * > & iso_map(const Pattern &p, NodeId node)
Given a pattern and a node in its isomorphism graph, this methods returns the sequence of instance ma...
Pattern & growPattern(Pattern &p, EdgeGrowth< GUM_SCALAR > &edge_growth, Size min_freq)
Add a one edge growth of p as one of its child.
void _addChild_(Pattern &p, Pattern *child, EdgeGrowth< GUM_SCALAR > &edge_growth)
Add a child to this DFSTree.
void _initialiaze_root_(Pattern *p, Sequence< EdgeData< GUM_SCALAR > * > &seq)
This initialize the DSFTree with a new root.
HashTable< Pattern *, PatternData * > _data_
Data about patterns in this DFSTree.
Definition DFSTree.h:277
bool _is_new_seq_(Sequence< PRMInstance< GUM_SCALAR > * > &seq, NodeProperty< Sequence< PRMInstance< GUM_SCALAR > * > * > &iso_map)
Check if an instance match is redundant.
double frequency(const Pattern &p) const
Returns the frequency of p respecting it's maximal independent set.
SearchStrategy< GUM_SCALAR > & strategy()
strategy getter
Bijection< NodeId, Pattern * > _node_map_
The mapping between nodes in this DFSTree and the patterns they represents.
Definition DFSTree.h:274
const InterfaceGraph< GUM_SCALAR > * _graph_
The interface graph on which this DFSTree applies.
Definition DFSTree.h:267
std::list< NodeId > _roots_
The list of root patterns in this DFSTree.
Definition DFSTree.h:270
DFSTree(const InterfaceGraph< GUM_SCALAR > &graph, SearchStrategy< GUM_SCALAR > *strategy=0)
Default constructor.
Inner class to handle data about edges in graph.
This class is used to define an edge growth of a pattern in this DFSTree.
Definition edgeGrowth.h:73
This class represent the interface graph of a given gum::prm::PRMSystem<GUM_SCALAR>.
This contains all the information we want for a node in a DFSTree.
Definition pattern.h:90
This is an abstract class used to tune search strategies in the gspan algorithm.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Headers of the SearchStrategy class and child.
NeighborDegreeSort(UndiGraph &graph)
Constructor.
bool operator()(NodeId i, NodeId j)
The operator used to sort stuff.
UndiGraph & g
The isomorphism graph.
Definition DFSTree.h:260
Pattern * pattern
The pattern.
Definition DFSTree.h:108
Size cost
The cost of this Pattern.
Definition DFSTree.h:118
Size gain
The gain of this Pattern.
Definition DFSTree.h:120
NodeProperty< Sequence< PRMInstance< GUM_SCALAR > * > * > iso_map
The instances matching p in the interface graph.
Definition DFSTree.h:114
UndiGraph iso_graph
The isomorphism graph of the pattern.
Definition DFSTree.h:112
PatternData(Pattern *p)
Constructor.
Set< NodeId > max_indep_set
The maximal independent set of p.
Definition DFSTree.h:116
std::list< NodeId > children
The list of the pattern's children, sorted lexicographically.
Definition DFSTree.h:110
Inner class to handle data about labels in this interface graph.