aGrUM 3.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-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
52
53#ifdef GUM_NO_INLINE
55#endif // GUM_NO_INLINE
56
57namespace gum {
58
60 DiGraph g;
61 g.addNodes(n);
62
63 for (int j = 0; j < n; ++j) {
64 for (int k = j + 1; k < n; ++k) {
65 g.addArc(j, k);
66 }
67 }
68 return g;
69 }
70
72 bool nodes_resize_policy,
73 Size arcs_size,
74 bool arcs_resize_policy) :
75 NodeGraphPart(nodes_size, nodes_resize_policy), ArcGraphPart(arcs_size, arcs_resize_policy) {
76 GUM_CONSTRUCTOR(DiGraph)
77 }
78
79 DiGraph::DiGraph(const DiGraph& g) : NodeGraphPart(g), ArcGraphPart(g) { GUM_CONS_CPY(DiGraph) }
80
82 GUM_CONS_MOV(DiGraph)
83 }
84
85 DiGraph::~DiGraph() { GUM_DESTRUCTOR(DiGraph) }
86
87 std::string DiGraph::toString() const {
88 std::string s = NodeGraphPart::toString();
89 s += " , ";
91 return s;
92 }
93
94 std::string DiGraph::toDot() const { return toDot(dot::DotOptions()); }
95
96 std::string DiGraph::toDot(const dot::DotOptions& options) const {
97 const auto style = dot::DotStyle::read();
98 dot::DotWriter writer(true);
99 dot::writePreamble(writer, style, options);
100 writeDotNodes(writer, style, options);
101
102 for (const auto& arc: arcs())
103 writer.edge(arc.tail(), arc.head(), dot::arcAttributes(options.arc(arc.tail(), arc.head())));
104
105 return writer.str();
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:136
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:59
virtual std::string toDot() const
to friendly display the content of the graph in the DOT syntax
Definition diGraph.cpp:94
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:71
~DiGraph() override
destructor
Definition diGraph.cpp:85
std::string toString() const override
to friendly display the content of the graph
Definition diGraph.cpp:87
void writeDotNodes(dot::DotWriter &writer, const dot::DotStyle &style, const dot::DotOptions &options) const
writes the nodes in the dot language, see writeDotNode()
virtual std::string toString() const
a function to display the set of nodes
NodeGraphPart(Size holes_size=HashTableConst::default_size, bool holes_resize_policy=true)
default constructor
std::vector< NodeId > addNodes(Size n)
insert n nodes
The options given to a toDot(const DotOptions&).
Definition dotOptions.h:110
const DotArcOptions * arc(Size tail, Size head) const
the options of the arc from tail to head, or nullptr if none was given for it
Writes a graph in the dot language, statement after statement, in the order of the calls.
Definition dotWriter.h:100
void edge(std::string_view from, std::string_view to, const DotAttributes &attributes={})
an edge from from to to (-> in a directed graph, -- in an undirected one).
std::string str() const
The graph written so far, closed. It can be called at any moment, and the writer can be used after.
Base classes for oriented graphs.
Inline implementation of Base classes for oriented graphs.
The statements common to the graphs drawn with graphviz : preamble, attributes of nodes and arcs.
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.
void writePreamble(DotWriter &writer, const DotStyle &style, const DotOptions &options, std::string_view defaultLabel)
Writes the beginning of a graph, as pyAgrum displays it.
DotAttributes arcAttributes(const DotArcOptions *options)
The attributes of an arc or an edge given by the options : color, penwidth, label,...
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.
static DotStyle read()
the style of the graphs of the section notebook
Definition dotStyle.cpp:89