aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
edgeGraphPart.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 classes for undirected edge sets
24  *
25  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26  *
27  */
28 #include <agrum/tools/graphs/parts/edgeGraphPart.h>
29 
30 #ifdef GUM_NO_INLINE
31 # include <agrum/tools/graphs/parts/edgeGraphPart_inl.h>
32 #endif // GUM_NOINLINE
33 #include "agrum/tools/graphs/graphElements.h"
34 
35 namespace gum {
36 
37  ///////////////////// EdgeGraphPart
41  }
42 
45 
46  // copy the set of neighbours
48 
49  for (const auto& elt: s.neighbours__) {
52  }
53 
54  // send signals to indicate that there are new edges
56  for (const auto& edge: edges__)
58  }
59 
62  // be sure to deallocate all the neighbours sets
63  clearEdges();
64  }
65 
67  for (const auto& elt: neighbours__)
68  delete elt.second;
69 
71 
72  if (onEdgeDeleted.hasListener()) {
74  edges__.clear();
75 
76  for (const auto& edge: tmp)
78  } else {
79  edges__.clear();
80  }
81  }
82 
84  // avoid self assignment
85  if (this != &s) {
86  clearEdges();
87 
88  edges__ = s.edges__;
89 
90  // copy the set of neighbours
92 
93  for (const auto& elt: s.neighbours__) {
96  }
97 
99  for (const auto& edge: edges__)
101  }
102 
103  return *this;
104  }
105 
107  std::stringstream s;
108  bool first = true;
109  s << "{";
110 
111  for (const auto& edge: edges__) {
112  if (first)
113  first = false;
114  else
115  s << ",";
116 
117  s << edge;
118  }
119 
120  s << "}";
121 
122  return s.str();
123  }
124 
125  const std::vector< NodeId >
127  // not recursive version => use a FIFO for simulating the recursion
128  List< NodeId > nodeFIFO;
130 
131  // mark[node] = predecessor if visited, else mark[node] does not exist
133  mark.insert(n2, n2);
134 
135  NodeId current;
136 
137  while (!nodeFIFO.empty()) {
138  current = nodeFIFO.front();
139  nodeFIFO.popFront();
140 
141  // check the neighbour
142  for (const auto new_one: neighbours(current)) {
143  if (mark.exists(new_one)) // if this node is already marked, stop
144  continue;
145 
147 
148  if (new_one == n1) {
149  std::vector< NodeId > v;
150 
151  for (current = n1; current != n2; current = mark[current])
153 
154  v.push_back(n2);
155 
156  return v;
157  }
158 
160  }
161  }
162 
163  GUM_ERROR(NotFound, "no path found");
164  }
165 
166  bool EdgeGraphPart::hasUndirectedPath(const NodeId n1, const NodeId n2) const {
168  NodeSet temp;
169 
170  temp.insert(n1);
171  while (!temp.empty()) {
172  NodeId current = *(temp.begin());
173  if (current == n2) return true;
174  temp.erase(current);
176  for (auto next: neighbours(current)) {
178  }
179  }
180  return false;
181  }
182 
184  const NodeId n2,
185  const NodeSet& except) const {
187  NodeSet temp;
188 
189  if (except.contains(n2)) return false;
190 
191  temp.insert(n1);
192  while (!temp.empty()) {
193  NodeId current = *(temp.begin());
194  if (current == n2) return true;
195  temp.erase(current);
197  for (auto next: neighbours(current)) {
199  && !except.contains(next))
200  temp.insert(next);
201  }
202  }
203  return false;
204  }
205 
207  const NodeSet& n2,
208  const NodeSet& except) const {
210  NodeSet temp;
211 
212  for (auto n: n1)
213  temp.insert(n);
214 
215  while (!temp.empty()) {
216  NodeId current = *(temp.begin());
217  if (n2.contains(current)) return true;
218  temp.erase(current);
220  for (auto next: neighbours(current)) {
222  && !except.contains(next))
223  temp.insert(next);
224  }
225  }
226  return false;
227  }
228 
230  stream << set.toString();
231  return stream;
232  }
233 
234 } /* 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.