aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
undiGraph.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 // GU%_NO_INLINE
55#include <cstdio>
56#include <iostream>
57
58#include "graphElements.h"
59
60namespace gum {
61
63 UndiGraph g;
64 g.addNodes(n);
65
66 for (int j = 0; j < n; ++j) {
67 for (int k = j + 1; k < n; ++k) {
68 g.addEdge(j, k);
69 }
70 }
71 return g;
72 }
73
75 bool nodes_resize_policy,
76 Size edges_size,
77 bool edges_resize_policy) :
78 NodeGraphPart(nodes_size, nodes_resize_policy),
79 EdgeGraphPart(edges_size, edges_resize_policy) {
80 GUM_CONSTRUCTOR(UndiGraph);
81 }
82
84 GUM_CONS_CPY(UndiGraph);
85 }
86
88 GUM_CONS_MOV(UndiGraph);
89 }
90
92 GUM_DESTRUCTOR(UndiGraph);
93 ;
94 }
95
96 std::string UndiGraph::toString() const {
97 std::string s = NodeGraphPart::toString();
98 s += " , ";
100 return s;
101 }
102
103 std::string UndiGraph::toDot() const { return toDot(dot::DotOptions()); }
104
105 std::string UndiGraph::toDot(const dot::DotOptions& options) const {
106 const auto style = dot::DotStyle::read();
107 dot::DotWriter writer(false);
108 dot::writePreamble(writer, style, options);
109 writeDotNodes(writer, style, options);
110
111 for (const auto& edge: edges())
112 writer.edge(edge.first(),
113 edge.second(),
114 dot::arcAttributes(options.edge(edge.first(), edge.second())));
115
116 return writer.str();
117 }
118
121 UndiGraph partialGraph;
122
123 for (const auto node: nodes) {
124 partialGraph.addNodeWithId(node);
125
126 for (const auto nei: neighbours(node))
127 if (nodes.contains(nei) && partialGraph.existsNode(nei)) partialGraph.addEdge(node, nei);
128 }
129
130 return partialGraph;
131 }
132
134 std::ostream& operator<<(std::ostream& stream, const UndiGraph& g) {
135 stream << g.toString();
136 return stream;
137 }
138
139} /* namespace gum */
EdgeGraphPart(Size edges_size=HashTableConst::default_size, bool edges_resize_policy=true)
default constructor
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
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
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
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
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
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:468
Base class for undirected graphs.
Definition undiGraph.h:134
virtual std::string toDot() const
to friendly display graph in DOT format
static UndiGraph completeGraph(int n)
create a complete UndiGraph with n nodes
Definition undiGraph.cpp:62
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
~UndiGraph() override
destructor
Definition undiGraph.cpp:91
std::string toString() const override
to friendly display the content of the graph
Definition undiGraph.cpp:96
virtual UndiGraph partialUndiGraph(NodeSet nodes)
returns the partial graph formed by the nodes given in parameter
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:74
The options given to a toDot(const DotOptions&).
Definition dotOptions.h:110
const DotArcOptions * edge(Size a, Size b) const
the options of the undirected edge between a and b, given for (a, b) or for (b, a),...
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.
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
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
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,...
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
Base classes for undirected graphs.
Inline implementation of Base classes for undirected graphs.