aGrUM 3.1.1
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-2026 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-2026 *
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
84 NodeGraphPart(std::move(g)), UndiGraph(std::move(g)), DiGraph(std::move(g)) {
85 GUM_CONS_MOV(MixedGraph);
86 }
87
88 MixedGraph::~MixedGraph() { // for debugging purposes
89 GUM_DESTRUCTOR(MixedGraph);
90 }
91
92 std::string MixedGraph::toString() const {
93 std::string s = NodeGraphPart::toString();
94 s += " , ";
96 s += " , ";
98 return s;
99 }
100
101 std::string MixedGraph::toDot() const {
102 std::stringstream output;
103 std::stringstream nodeStream;
104 std::stringstream edgeStream;
105
106 NodeSet treatedNodes;
107
108 output << "digraph \"no_name\" {\n";
109 nodeStream << "node [shape = ellipse];\n";
110 std::string tab = " ";
111
112 for (const auto node: nodes()) {
113 nodeStream << std::format("{}{}{};", tab, node, dotNodeLabel(node));
114
115 for (const auto nei: neighbours(node))
116 if (!treatedNodes.exists(nei))
117 edgeStream << std::format("{}{} -> {} [dir=none];\n", tab, node, nei);
118
119 for (const auto chi: children(node))
120 edgeStream << std::format("{}{} -> {};\n", tab, node, chi);
121
122 treatedNodes.insert(node);
123 }
124
125 output << nodeStream.str() << '\n' << edgeStream.str() << '\n' << "}\n";
126
127 return output.str();
128 }
129
131 std::ostream& operator<<(std::ostream& stream, const MixedGraph& g) {
132 stream << g.toString();
133 return stream;
134 }
135
136
137} /* namespace gum */
NodeSet children(const NodeSet &ids) const
returns the set of nodes which consists in the node and its parents returns the set of children of a ...
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:70
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
Base class for mixed graphs.
Definition mixedGraph.h:146
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
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
~MixedGraph() override
destructor
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
std::string dotNodeLabel(NodeId id) const
returns " [label=\"...\"]" with DOT-escaped name, or "" if no name
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:504
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
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:73
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
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
STL namespace.