aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
diGraph.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
50
51#ifdef GUM_NO_INLINE
53#endif // GUM_NO_INLINE
54
55namespace gum {
56
58 DiGraph g;
59 g.addNodes(n);
60
61 for (int j = 0; j < n; ++j) {
62 for (int k = j + 1; k < n; ++k) {
63 g.addArc(j, k);
64 }
65 }
66 return g;
67 }
68
70 bool nodes_resize_policy,
71 Size arcs_size,
72 bool arcs_resize_policy) :
73 NodeGraphPart(nodes_size, nodes_resize_policy), ArcGraphPart(arcs_size, arcs_resize_policy) {
74 GUM_CONSTRUCTOR(DiGraph)
75 }
76
77 DiGraph::DiGraph(const DiGraph& g) : NodeGraphPart(g), ArcGraphPart(g) { GUM_CONS_CPY(DiGraph) }
78
79 DiGraph::~DiGraph() { GUM_DESTRUCTOR(DiGraph) }
80
81 std::string DiGraph::toString() const {
82 std::string s = NodeGraphPart::toString();
83 s += " , ";
85 return s;
86 }
87
88 std::string DiGraph::toDot() const {
89 std::stringstream strBuff;
90 std::string tab = " ";
91 strBuff << "digraph {" << std::endl;
92
93 for (const auto node: nodes())
94 strBuff << tab << node << ";" << std::endl;
95
96 strBuff << std::endl;
97
98 for (const auto& arc: arcs())
99 strBuff << tab << arc.tail() << " -> " << arc.head() << ";" << std::endl;
100
101 strBuff << "}" << std::endl << std::endl;
102 return strBuff.str();
103 }
104
106 std::ostream& operator<<(std::ostream& stream, const DiGraph& g) {
107 stream << g.toString();
108 return stream;
109 }
110
113 const auto& dag = *this;
114
115 if (dag.empty()) return topologicalOrder;
116
117 auto border = std::vector< NodeId >();
118 border.reserve(dag.size() / 2);
119 auto count = dag.nodesPropertyFromVal< Size >(0, dag.size());
120 for (const auto node: dag.nodes()) {
121 if (dag.parents(node).empty()) { border.push_back(node); }
122 count[node] = dag.parents(node).size();
123 }
124
125 if (border.empty()) {
126 GUM_ERROR(InvalidDirectedCycle, "cycles prevent the creation of a topological ordering.");
127 }
128
129 while (!border.empty()) {
130 const auto root = border.back();
131 border.pop_back();
132
133 if (topologicalOrder.exists(root)) {
134 GUM_ERROR(InvalidDirectedCycle, "cycles prevent the creation of a topological ordering.");
135 }
136 topologicalOrder.insert(root);
137
138 for (const auto child: dag.children(root)) {
139 if (count[child] == 1) { border.push_back(child); }
140 if (count[child] == 0) {
141 GUM_ERROR(InvalidDirectedCycle, "cycles prevent the creation of a topological ordering.");
142 }
143 count[child]--;
144 }
145 }
146
147 GUM_ASSERT(topologicalOrder.size() == dag.size());
148 return topologicalOrder;
149 }
150
151 bool DiGraph::hasDirectedPath(const NodeId from, const NodeId to) {
152 if (!exists(from)) return false;
153
154 // not recursive version => use a FIFO for simulating the recursion
155 List< NodeId > nodeFIFO;
156 nodeFIFO.pushBack(from);
157
158 NodeSet marked;
159 marked.insert(from);
160
161 NodeId new_one;
162
163 while (!nodeFIFO.empty()) {
164 new_one = nodeFIFO.front();
165 nodeFIFO.popFront();
166
167 for (const auto chi: children(new_one)) {
168 if (chi == to) return true;
169
170 if (!marked.contains(chi)) {
171 nodeFIFO.pushBack(chi);
172 marked.insert(chi);
173 }
174 }
175 }
176
177 return false;
178 }
179
180} /* namespace gum */
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
ArcGraphPart(Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true)
default constructor
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
std::string toString() const
to friendly display the content of the ArcGraphPart
Base class for all oriented graphs.
Definition diGraph.h:130
bool hasDirectedPath(NodeId from, NodeId to)
checks whether there exists a directed path from from to to
Definition diGraph.cpp:151
static DiGraph completeGraph(int n)
Build a complete DiGraph with n nodes.
Definition diGraph.cpp:57
virtual std::string toDot() const
to friendly display the content of the graph in the DOT syntax
Definition diGraph.cpp:88
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 void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition diGraph_inl.h:55
Sequence< NodeId > topologicalOrder() const
Build and return a topological order.
Definition diGraph.cpp:111
virtual ~DiGraph()
destructor
Definition diGraph.cpp:79
virtual std::string toString() const
to friendly display the content of the graph
Definition diGraph.cpp:81
Exception : existence of a directed cycle in a graph.
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
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
virtual std::string toString() const
a function to display the set of nodes
bool exists(const NodeId id) const
alias for existsNode
NodeGraphPart(Size holes_size=HashTableConst::default_size, bool holes_resize_policy=true)
default constructor
std::vector< NodeId > addNodes(Size n)
insert n nodes
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:497
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
Base classes for oriented graphs.
Inline implementation of Base classes for oriented graphs.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
some utils for topology : NodeId, Edge, Arc and consorts ...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
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