aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
mixedGraph.cpp
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
49
50#ifdef GUM_NO_INLINE
52#endif // GU%_NO_INLINE
53
54namespace gum {
55
57 bool nodes_resize_policy,
58 Size arcs_size,
59 bool arcs_resize_policy,
60 Size edges_size,
61 bool edges_resize_policy) :
62 // Note that we need to initialize the NodeGraphPart by ourselves
63 // because
64 // it is a virtual inherited class (see C++ FAQ Lite #25.12 for details)
65 NodeGraphPart(nodes_size, nodes_resize_policy), UndiGraph(edges_size, edges_resize_policy),
66 DiGraph(arcs_size, arcs_resize_policy) { // for debugging purposes
67 GUM_CONSTRUCTOR(MixedGraph);
68 }
69
71 GUM_CONSTRUCTOR(MixedGraph);
72 }
73
75 GUM_CONSTRUCTOR(MixedGraph);
76 }
77
79 NodeGraphPart(g), UndiGraph(g), DiGraph(g) { // for debugging purposes
80 GUM_CONS_CPY(MixedGraph);
81 }
82
83 MixedGraph::~MixedGraph() { // for debugging purposes
84 GUM_DESTRUCTOR(MixedGraph);
85 }
86
87 std::string MixedGraph::toString() const {
88 std::string s = NodeGraphPart::toString();
89 s += " , ";
91 s += " , ";
93 return s;
94 }
95
96 std::vector< NodeId > MixedGraph::mixedOrientedPath(NodeId n1, NodeId n2) const {
97 std::vector< NodeId > v;
98 // not recursive version => use a FIFO for simulating the recursion
99 List< NodeId > node_fifo;
100 node_fifo.pushBack(n2);
101
102 // mark[node] = successor if visited, else mark[node] does not exist
104 mark.insert(n2, n2);
105
106 NodeId current;
107 while (!node_fifo.empty()) {
108 current = node_fifo.front();
109 node_fifo.popFront();
110
111 // check the neighbours
112 for (const auto new_one: neighbours(current)) {
113 if (mark.exists(new_one)) // if the node has already been visited
114 continue; // do not check it again
115
116 mark.insert(new_one, current);
117
118 if (new_one == n1) {
119 for (current = n1; current != n2; current = mark[current])
120 v.push_back(current);
121 v.push_back(n2);
122 return v;
123 }
124
125 node_fifo.pushBack(new_one);
126 }
127
128 // check the parents
129 for (const auto new_one: parents(current)) {
130 if (mark.exists(new_one)) // if this node is already marked, do not
131 continue; // check it again
132
133 mark.insert(new_one, current);
134
135 if (new_one == n1) {
136 for (current = n1; current != n2; current = mark[current])
137 v.push_back(current);
138 v.push_back(n2);
139 return v;
140 }
141
142 node_fifo.pushBack(new_one);
143 }
144 }
145
146 return v;
147 }
148
150 // not recursive version => use a FIFO for simulating the recursion
151 List< NodeId > node_fifo;
152 node_fifo.pushBack(n2);
153
154 // mark[node] = successor if visited, else mark[node] does not exist
156 mark.insert(n2, n2);
157
158 NodeId current;
159 while (!node_fifo.empty()) {
160 current = node_fifo.front();
161 node_fifo.popFront();
162
163 // check the neighbours
164 for (const auto new_one: neighbours(current)) {
165 if (mark.exists(new_one)) // if the node has already been visited
166 continue; // do not check it again
167
168 mark.insert(new_one, current);
169
170 if (new_one == n1) { return true; }
171
172 node_fifo.pushBack(new_one);
173 }
174
175 // check the parents
176 for (const auto new_one: parents(current)) {
177 if (mark.exists(new_one)) // if this node is already marked, do not
178 continue; // check it again
179
180 mark.insert(new_one, current);
181
182 if (new_one == n1) { return true; }
183
184 node_fifo.pushBack(new_one);
185 }
186 }
187
188 return false;
189 }
190
191 std::vector< NodeId > MixedGraph::mixedUnorientedPath(NodeId n1, NodeId n2) const {
192 std::vector< NodeId > v;
193 // not recursive version => use a FIFO for simulating the recursion
194 List< NodeId > node_fifo;
195 node_fifo.pushBack(n2);
196
197 // mark[node] = successor if visited, else mark[node] does not exist
199 mark.insert(n2, n2);
200
201 NodeId current;
202
203 while (!node_fifo.empty()) {
204 current = node_fifo.front();
205 node_fifo.popFront();
206
207 // check the neighbours
208 for (const auto new_one: neighbours(current)) {
209 if (mark.exists(new_one)) // if the node has already been visited
210 continue; // do not check it again
211
212 mark.insert(new_one, current);
213
214 if (new_one == n1) {
215 for (current = n1; current != n2; current = mark[current])
216 v.push_back(current);
217 v.push_back(n2);
218 return v;
219 }
220
221 node_fifo.pushBack(new_one);
222 }
223
224 // check the parents
225 for (const auto new_one: parents(current)) {
226 if (mark.exists(new_one)) // the node has already been visited
227 continue;
228
229 mark.insert(new_one, current);
230 if (new_one == n1) {
231 for (current = n1; current != n2; current = mark[current])
232 v.push_back(current);
233 v.push_back(n2);
234 return v;
235 }
236 node_fifo.pushBack(new_one);
237 }
238
239 // check the children
240 for (const auto new_one: children(current)) {
241 if (mark.exists(new_one)) // the node has already been visited
242 continue;
243
244 mark.insert(new_one, current);
245
246 if (new_one == n1) {
247 for (current = n1; current != n2; current = mark[current])
248 v.push_back(current);
249 v.push_back(n2);
250 return v;
251 }
252
253 node_fifo.pushBack(new_one);
254 }
255 }
256
257 return v;
258 }
259
261 NodeSet res;
262 NodeSet stack{node};
263
264 while (!stack.empty()) {
265 const NodeId n = *(stack.begin());
266 stack.erase(n);
267 if (!res.exists(n)) {
268 res.insert(n);
269 stack += neighbours(n);
270 }
271 }
272
273 return res;
274 }
275
276 std::string MixedGraph::toDot() const {
277 std::stringstream output;
278 std::stringstream nodeStream;
279 std::stringstream edgeStream;
280
281 NodeSet treatedNodes;
282
283 output << "digraph \""
284 << "no_name\" {" << std::endl;
285 nodeStream << "node [shape = ellipse];" << std::endl;
286 std::string tab = " ";
287
288 for (const auto node: nodes()) {
289 nodeStream << tab << node << ";";
290
291 for (const auto nei: neighbours(node))
292 if (!treatedNodes.exists(nei))
293 edgeStream << tab << node << " -> " << nei << " [dir=none];" << std::endl;
294
295 for (const auto chi: children(node))
296 edgeStream << tab << node << " -> " << chi << ";" << std::endl;
297
298 treatedNodes.insert(node);
299 }
300
301 output << nodeStream.str() << std::endl << edgeStream.str() << std::endl << "}" << std::endl;
302
303 return output.str();
304 }
305
307 std::ostream& operator<<(std::ostream& stream, const MixedGraph& g) {
308 stream << g.toString();
309 return stream;
310 }
311
312
313} /* namespace gum */
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
std::string toString() const
to friendly display the content of the ArcGraphPart
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
virtual std::string toString() const
to friendly display the content of the EdgeGraphPart
const NodeSet & neighbours(NodeId id) const
returns the set of node neighbours to a given node
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Generic doubly linked lists.
Definition list.h:379
Val & front() const
Returns a reference to first element of a list, if any.
Definition list_tpl.h:1703
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition list_tpl.h:1831
void popFront()
Removes the first element of a List, if any.
Definition list_tpl.h:1825
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition list_tpl.h:1488
Base class for mixed graphs.
Definition mixedGraph.h:146
NodeSet chainComponent(NodeId node) const
returns the set of nodes reachable by undirected path
std::string toString() const override
to friendly display the content of the MixedGraph
std::string toDot() const override
to friendly display mixed graph in DOT format
std::vector< NodeId > mixedUnorientedPath(NodeId node1, NodeId node2) const
returns a mixed/directed path from node1 to node2 in the arc/edge set
MixedGraph(Size nodes_size=HashTableConst::default_size, bool nodes_resize_policy=true, Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true, Size edges_size=HashTableConst::default_size, bool edges_resize_policy=true)
default constructor
bool hasMixedOrientedPath(NodeId node1, NodeId node2) const
returns true if a mixed edge/directed arc path from node1 to node2 in the arc/edge set exists.
virtual ~MixedGraph()
destructor
std::vector< NodeId > mixedOrientedPath(NodeId node1, NodeId node2) const
returns a mixed edge/directed arc path from node1 to node2 in the arc/edge set
Class for node sets in graph.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
virtual std::string toString() const
a function to display the set of nodes
iterator begin() const
The usual unsafe begin iterator to parse the set.
Definition set_tpl.h:438
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:533
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:642
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:582
UndiGraph(Size nodes_size=HashTableConst::default_size, bool nodes_resize_policy=true, Size edges_size=HashTableConst::default_size, bool edges_resize_policy=true)
default constructor
Definition undiGraph.cpp:72
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.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Base classes for mixed directed/undirected graphs.
Inline implementation of Base classes for mixed graphs.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Definition AVLTree.h:913