aGrUM 3.1.1
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-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
51
52#ifdef GUM_NO_INLINE
54#endif // GUM_NO_INLINE
55
56namespace gum {
57
59 DiGraph g;
60 g.addNodes(n);
61
62 for (int j = 0; j < n; ++j) {
63 for (int k = j + 1; k < n; ++k) {
64 g.addArc(j, k);
65 }
66 }
67 return g;
68 }
69
71 bool nodes_resize_policy,
72 Size arcs_size,
73 bool arcs_resize_policy) :
74 NodeGraphPart(nodes_size, nodes_resize_policy), ArcGraphPart(arcs_size, arcs_resize_policy) {
75 GUM_CONSTRUCTOR(DiGraph)
76 }
77
78 DiGraph::DiGraph(const DiGraph& g) : NodeGraphPart(g), ArcGraphPart(g) { GUM_CONS_CPY(DiGraph) }
79
81 GUM_CONS_MOV(DiGraph)
82 }
83
84 DiGraph::~DiGraph() { GUM_DESTRUCTOR(DiGraph) }
85
86 std::string DiGraph::toString() const {
87 std::string s = NodeGraphPart::toString();
88 s += " , ";
90 return s;
91 }
92
93 std::string DiGraph::toDot() const {
94 std::string strBuff = "digraph {\n";
95
96 for (const auto node: nodes())
97 strBuff += std::format(" {}{};\n", node, dotNodeLabel(node));
98
99 strBuff += "\n";
100
101 for (const auto& arc: arcs())
102 strBuff += std::format(" {} -> {};\n", arc.tail(), arc.head());
103
104 strBuff += "}\n\n";
105 return strBuff;
106 }
107
109 std::ostream& operator<<(std::ostream& stream, const DiGraph& g) {
110 stream << g.toString();
111 return stream;
112 }
113
114 bool DiGraph::hasDirectedPath(const NodeId from, const NodeId to) const {
115 return graph::hasDirectedPath(*this, from, to);
116 }
117
118} /* namespace gum */
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:132
bool hasDirectedPath(NodeId from, NodeId to) const
checks whether there exists a directed path from from to to
Definition diGraph.cpp:114
static DiGraph completeGraph(int n)
Build a complete DiGraph with n nodes.
Definition diGraph.cpp:58
virtual std::string toDot() const
to friendly display the content of the graph in the DOT syntax
Definition diGraph.cpp:93
void addArc(const NodeId tail, const NodeId head) override
insert a new arc into the directed graph
Definition diGraph_inl.h:59
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
~DiGraph() override
destructor
Definition diGraph.cpp:84
std::string toString() const override
to friendly display the content of the graph
Definition diGraph.cpp:86
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
NodeGraphPart(Size holes_size=HashTableConst::default_size, bool holes_resize_policy=true)
default constructor
std::vector< NodeId > addNodes(Size n)
insert n nodes
Base classes for oriented graphs.
Inline implementation of Base classes for oriented graphs.
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.
bool hasDirectedPath(const G &g, NodeId from, NodeId to)
Returns true if there is a directed path from from to to.
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.
Generic BFS-based path-finding algorithms for aGrUM graphs.