aGrUM 2.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-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
49
50#ifdef GUM_NO_INLINE
52#endif // GU%_NO_INLINE
53#include <cstdio>
54#include <iostream>
55
56#include "graphElements.h"
57
58namespace gum {
59
61 UndiGraph g;
62 g.addNodes(n);
63
64 for (int j = 0; j < n; ++j) {
65 for (int k = j + 1; k < n; ++k) {
66 g.addEdge(j, k);
67 }
68 }
69 return g;
70 }
71
73 bool nodes_resize_policy,
74 Size edges_size,
75 bool edges_resize_policy) :
76 NodeGraphPart(nodes_size, nodes_resize_policy),
77 EdgeGraphPart(edges_size, edges_resize_policy) {
78 GUM_CONSTRUCTOR(UndiGraph);
79 }
80
82 GUM_CONS_CPY(UndiGraph);
83 }
84
86 GUM_DESTRUCTOR(UndiGraph);
87 ;
88 }
89
92 NodeProperty< bool > examined_nodes = nodesPropertyFromVal(false);
93 std::pair< NodeId, NodeId > thePair;
94 NodeId current, from_current;
95
96 for (const auto node: nodes()) {
97 // check if the node has already been examined (if this is not the case,
98 // this means that we are on a new connected component)
99 if (!examined_nodes[node]) {
100 // indicates that we are examining a new node
101 examined_nodes[node] = true;
102
103 // check recursively all the nodes of node's connected component
104 thePair.first = node;
105 thePair.second = node;
106 open_nodes.insert(thePair);
107
108 while (!open_nodes.empty()) {
109 // get a node to propagate
110 thePair = open_nodes.front();
111 open_nodes.popFront();
112
113 current = thePair.first;
114 from_current = thePair.second;
115
116 // check the neighbours
117 for (const auto new_node: neighbours(current))
118
119 // avoid to check the node we are coming from
120 if (new_node != from_current) {
121 if (examined_nodes[new_node]) return true;
122 else {
123 examined_nodes[new_node] = true;
124 thePair.first = new_node;
125 thePair.second = current;
126 open_nodes.insert(thePair);
127 }
128 }
129 }
130 }
131 }
132
133 return false;
134 }
135
136 std::string UndiGraph::toString() const {
137 std::string s = NodeGraphPart::toString();
138 s += " , ";
140 return s;
141 }
142
143 std::string UndiGraph::toDot() const {
144 std::stringstream output;
145 std::stringstream nodeStream;
146 std::stringstream edgeStream;
147 List< NodeId > treatedNodes;
148 output << "digraph \""
149 << "no_name\" {" << std::endl
150 << "edge [dir=none]" << std::endl;
151 nodeStream << "node [shape = ellipse];" << std::endl;
152 std::string tab = " ";
153
154 for (const auto node: nodes()) {
155 nodeStream << tab << node << ";";
156
157 if (neighbours(node).size() > 0)
158 for (const auto nei: neighbours(node))
159 if (!treatedNodes.exists(nei))
160 edgeStream << tab << node << " -> " << nei << ";" << std::endl;
161
162 treatedNodes.insert(node);
163 }
164
165 output << nodeStream.str() << std::endl << edgeStream.str() << std::endl << "}" << std::endl;
166
167 return output.str();
168 }
169
172 UndiGraph partialGraph;
173
174 for (const auto node: nodes) {
175 partialGraph.addNodeWithId(node);
176
177 for (const auto nei: neighbours(node))
178 if (nodes.contains(nei) && partialGraph.existsNode(nei)) partialGraph.addEdge(node, nei);
179 }
180
181 return partialGraph;
182 }
183
186
187 NodeId numCC = 0;
188 for (const auto node: nodes()) {
189 if (res.exists(node)) continue;
190 NodeSet nodes{node};
191 while (!nodes.empty()) {
192 auto actual = *(nodes.begin());
193 nodes.erase(actual);
194 res.insert(actual, numCC);
195 for (const auto nei: neighbours(actual)) {
196 if (!res.exists(nei))
197 if (!nodes.exists(nei)) nodes.insert(nei);
198 }
199 }
200 numCC += 1;
201 }
202
203 return res;
204 }
205
207 std::ostream& operator<<(std::ostream& stream, const UndiGraph& g) {
208 stream << g.toString();
209 return stream;
210 }
211
212} /* 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
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Generic doubly linked lists.
Definition list.h:379
bool exists(const Val &val) const
Checks whether there exists a given element in the list.
Definition list_tpl.h:1725
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 & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
Definition list_tpl.h:1515
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
NodeProperty< VAL > nodesPropertyFromVal(const VAL &a, Size size=0) const
a method to create a hashMap with key:NodeId and value:VAL
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:128
virtual std::string toDot() const
to friendly display graph in DOT format
bool hasUndirectedCycle() const
checks whether the graph contains cycles
Definition undiGraph.cpp:90
static UndiGraph completeGraph(int n)
create a complete UndiGraph with n nodes
Definition undiGraph.cpp:60
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
virtual ~UndiGraph()
destructor
Definition undiGraph.cpp:85
NodeProperty< NodeId > nodes2ConnectedComponent() const
returns a property {node:id of connected component}
std::string toString() const override
to friendly display the content of the graph
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: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.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
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
Base classes for undirected graphs.
Inline implementation of Base classes for undirected graphs.