aGrUM 3.1.1
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
50
51#ifdef GUM_NO_INLINE
53#endif // GU%_NO_INLINE
54#include <cstdio>
55#include <iostream>
56
57#include "graphElements.h"
58
59namespace gum {
60
62 UndiGraph g;
63 g.addNodes(n);
64
65 for (int j = 0; j < n; ++j) {
66 for (int k = j + 1; k < n; ++k) {
67 g.addEdge(j, k);
68 }
69 }
70 return g;
71 }
72
74 bool nodes_resize_policy,
75 Size edges_size,
76 bool edges_resize_policy) :
77 NodeGraphPart(nodes_size, nodes_resize_policy),
78 EdgeGraphPart(edges_size, edges_resize_policy) {
79 GUM_CONSTRUCTOR(UndiGraph);
80 }
81
83 GUM_CONS_CPY(UndiGraph);
84 }
85
87 GUM_CONS_MOV(UndiGraph);
88 }
89
91 GUM_DESTRUCTOR(UndiGraph);
92 ;
93 }
94
95 std::string UndiGraph::toString() const {
96 std::string s = NodeGraphPart::toString();
97 s += " , ";
99 return s;
100 }
101
102 std::string UndiGraph::toDot() const {
103 std::stringstream output;
104 std::stringstream nodeStream;
105 std::stringstream edgeStream;
106 List< NodeId > treatedNodes;
107 output << "digraph \"no_name\" {\nedge [dir=none]\n";
108 nodeStream << "node [shape = ellipse];\n";
109 std::string tab = " ";
110
111 for (const auto node: nodes()) {
112 nodeStream << std::format("{}{}{};", tab, node, dotNodeLabel(node));
113
114 if (neighbours(node).size() > 0)
115 for (const auto nei: neighbours(node))
116 if (!treatedNodes.exists(nei)) edgeStream << std::format("{}{} -> {};\n", tab, node, nei);
117
118 treatedNodes.insert(node);
119 }
120
121 output << nodeStream.str() << '\n' << edgeStream.str() << '\n' << "}\n";
122
123 return output.str();
124 }
125
128 UndiGraph partialGraph;
129
130 for (const auto node: nodes) {
131 partialGraph.addNodeWithId(node);
132
133 for (const auto nei: neighbours(node))
134 if (nodes.contains(nei) && partialGraph.existsNode(nei)) partialGraph.addEdge(node, nei);
135 }
136
137 return partialGraph;
138 }
139
141 std::ostream& operator<<(std::ostream& stream, const UndiGraph& g) {
142 stream << g.toString();
143 return stream;
144 }
145
146} /* namespace gum */
EdgeGraphPart(Size edges_size=HashTableConst::default_size, bool edges_resize_policy=true)
default constructor
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
Generic doubly linked lists.
Definition list.h:378
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
Definition list_tpl.h:1508
bool exists(const Val &val) const
Checks whether there exists a given element in the list.
Definition list_tpl.h:1716
Size size() const
alias for sizeNodes
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
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
Base class for undirected graphs.
Definition undiGraph.h:130
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:61
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
~UndiGraph() override
destructor
Definition undiGraph.cpp:90
std::string toString() const override
to friendly display the content of the graph
Definition undiGraph.cpp:95
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:73
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 ...
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.
Base classes for undirected graphs.
Inline implementation of Base classes for undirected graphs.