aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
mixedGraph.cpp
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief Source implementation of Base classes for undirected graphs
24  *
25  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26  *
27  */
28 #include <agrum/tools/graphs/mixedGraph.h>
29 
30 #ifdef GUM_NO_INLINE
31 # include <agrum/tools/graphs/mixedGraph_inl.h>
32 #endif // GUM_NOINLINE
33 
34 namespace gum {
35 
39  bool arcs_resize_policy,
41  bool edges_resize_policy) :
42  // Note that we need to initialize the NodeGraphPart by ourselves
43  // because
44  // it is a virtual inherited class (see C++ FAQ Lite #25.12 for details)
48  // for debugging purposes
50  }
51 
55  }
56 
60  }
61 
64  // for debugging purposes
66  }
67 
69  // for debugging purposes
71  }
72 
75  s += " , ";
76  s += ArcGraphPart::toString();
77  s += " , ";
78  s += EdgeGraphPart::toString();
79  return s;
80  }
81 
82  const std::vector< NodeId >
83  MixedGraph::mixedOrientedPath(const NodeId n1, const NodeId n2) const {
84  // not recursive version => use a FIFO for simulating the recursion
85  List< NodeId > nodeFIFO;
87 
88  // mark[node] = successor if visited, else mark[node] does not exist
90  mark.insert(n2, n2);
91 
93 
94  while (!nodeFIFO.empty()) {
97 
98  // check the neighbours
99  for (const auto new_one: neighbours(current)) {
100  if (mark.exists(new_one)) // if the node has already been visited
101  continue; // do not check it again
102 
104 
105  if (new_one == n1) {
106  std::vector< NodeId > v;
107 
108  for (current = n1; current != n2; current = mark[current])
110 
111  v.push_back(n2);
112 
113  return v;
114  }
115 
117  }
118 
119  // check the parents
120  for (const auto new_one: parents(current)) {
121  if (mark.exists(new_one)) // if this node is already marked, do not
122  continue; // check it again
123 
125 
126  if (new_one == n1) {
127  std::vector< NodeId > v;
128 
129  for (current = n1; current != n2; current = mark[current])
131 
132  v.push_back(n2);
133 
134  return v;
135  }
136 
138  }
139  }
140 
141  GUM_ERROR(NotFound, "no path found");
142  }
143 
144  const std::vector< NodeId >
146  // not recursive version => use a FIFO for simulating the recursion
147  List< NodeId > nodeFIFO;
149 
150  // mark[node] = successor if visited, else mark[node] does not exist
152  mark.insert(n2, n2);
153 
154  NodeId current;
155 
156  while (!nodeFIFO.empty()) {
157  current = nodeFIFO.front();
158  nodeFIFO.popFront();
159 
160  // check the neighbours
161  for (const auto new_one: neighbours(current)) {
162  if (mark.exists(new_one)) // if the node has already been visited
163  continue; // do not check it again
164 
166 
167  if (new_one == n1) {
168  std::vector< NodeId > v;
169 
170  for (current = n1; current != n2; current = mark[current])
172 
173  v.push_back(n2);
174 
175  return v;
176  }
177 
179  }
180 
181  // check the parents
182  for (const auto new_one: parents(current)) {
183  if (mark.exists(new_one)) // the node has already been visited
184  continue;
185 
187 
188  if (new_one == n1) {
189  std::vector< NodeId > v;
190 
191  for (current = n1; current != n2; current = mark[current])
193 
194  v.push_back(n2);
195 
196  return v;
197  }
198 
200  }
201 
202  // check the children
203  for (const auto new_one: children(current)) {
204  if (mark.exists(new_one)) // the node has already been visited
205  continue;
206 
208 
209  if (new_one == n1) {
210  std::vector< NodeId > v;
211 
212  for (current = n1; current != n2; current = mark[current])
214 
215  v.push_back(n2);
216  return v;
217  }
218 
220  }
221  }
222 
223  GUM_ERROR(NotFound, "no path found");
224  }
225 
231  output << "digraph \""
232  << "no_name\" {" << std::endl;
233  nodeStream << "node [shape = ellipse];" << std::endl;
234  std::string tab = " ";
235 
236  for (const auto node: nodes()) {
237  nodeStream << tab << node << ";";
238 
239  for (const auto nei: neighbours(node))
240  if (!treatedNodes.exists(nei))
241  edgeStream << tab << node << " -> " << nei << " [dir=none];"
242  << std::endl;
243 
244  for (const auto chi: children(node))
245  edgeStream << tab << node << " -> " << chi << ";" << std::endl;
246 
248  }
249 
250  output << nodeStream.str() << std::endl
251  << edgeStream.str() << std::endl
252  << "}" << std::endl;
253 
254  return output.str();
255  }
256 
257  /// for friendly displaying the content of directed graphs
259  stream << g.toString();
260  return stream;
261  }
262 
263 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
std::ostream & operator<<(std::ostream &aStream, const Instantiation &i)
Print information of the instantiation in the stream.