aGrUM  0.14.2
mixedGraph.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 
28 #ifdef GUM_NO_INLINE
30 #endif // GUM_NOINLINE
31 
32 namespace gum {
33 
35  bool nodes_resize_policy,
36  Size arcs_size,
37  bool arcs_resize_policy,
38  Size edges_size,
39  bool edges_resize_policy) :
40  // Note that we need to initialize the NodeGraphPart by ourselves
41  // because
42  // it is a virtual inherited class (see C++ FAQ Lite #25.12 for details)
43  NodeGraphPart(nodes_size, nodes_resize_policy),
44  UndiGraph(edges_size, edges_resize_policy),
45  DiGraph(arcs_size, arcs_resize_policy) {
46  // for debugging purposes
47  GUM_CONSTRUCTOR(MixedGraph);
48  }
49 
51  NodeGraphPart(g), UndiGraph(g), DiGraph(g) {
52  // for debugging purposes
53  GUM_CONS_CPY(MixedGraph);
54  }
55 
57  // for debugging purposes
58  GUM_DESTRUCTOR(MixedGraph);
59  }
60 
61  const std::string MixedGraph::toString() const {
62  std::string s = NodeGraphPart::toString();
63  s += " , ";
65  s += " , ";
67  return s;
68  }
69 
70  const std::vector< NodeId >
71  MixedGraph::mixedOrientedPath(const NodeId n1, const NodeId n2) const {
72  // not recursive version => use a FIFO for simulating the recursion
73  List< NodeId > nodeFIFO;
74  nodeFIFO.pushBack(n2);
75 
76  // mark[node] = successor if visited, else mark[node] does not exist
78  mark.insert(n2, n2);
79 
80  NodeId current;
81 
82  while (!nodeFIFO.empty()) {
83  current = nodeFIFO.front();
84  nodeFIFO.popFront();
85 
86  // check the neighbours
87  for (const auto new_one : neighbours(current)) {
88  if (mark.exists(new_one)) // if the node has already been visited
89  continue; // do not check it again
90 
91  mark.insert(new_one, current);
92 
93  if (new_one == n1) {
94  std::vector< NodeId > v;
95 
96  for (current = n1; current != n2; current = mark[current])
97  v.push_back(current);
98 
99  v.push_back(n2);
100 
101  return v;
102  }
103 
104  nodeFIFO.pushBack(new_one);
105  }
106 
107  // check the parents
108  for (const auto new_one : parents(current)) {
109  if (mark.exists(new_one)) // if this node is already marked, do not
110  continue; // check it again
111 
112  mark.insert(new_one, current);
113 
114  if (new_one == n1) {
115  std::vector< NodeId > v;
116 
117  for (current = n1; current != n2; current = mark[current])
118  v.push_back(current);
119 
120  v.push_back(n2);
121 
122  return v;
123  }
124 
125  nodeFIFO.pushBack(new_one);
126  }
127  }
128 
129  GUM_ERROR(NotFound, "no path found");
130  }
131 
132  const std::vector< NodeId >
133  MixedGraph::mixedUnorientedPath(const NodeId n1, const NodeId n2) const {
134  // not recursive version => use a FIFO for simulating the recursion
135  List< NodeId > nodeFIFO;
136  nodeFIFO.pushBack(n2);
137 
138  // mark[node] = successor if visited, else mark[node] does not exist
140  mark.insert(n2, n2);
141 
142  NodeId current;
143 
144  while (!nodeFIFO.empty()) {
145  current = nodeFIFO.front();
146  nodeFIFO.popFront();
147 
148  // check the neighbours
149  for (const auto new_one : neighbours(current)) {
150  if (mark.exists(new_one)) // if the node has already been visited
151  continue; // do not check it again
152 
153  mark.insert(new_one, current);
154 
155  if (new_one == n1) {
156  std::vector< NodeId > v;
157 
158  for (current = n1; current != n2; current = mark[current])
159  v.push_back(current);
160 
161  v.push_back(n2);
162 
163  return v;
164  }
165 
166  nodeFIFO.pushBack(new_one);
167  }
168 
169  // check the parents
170  for (const auto new_one : parents(current)) {
171  if (mark.exists(new_one)) // the node has already been visited
172  continue;
173 
174  mark.insert(new_one, current);
175 
176  if (new_one == n1) {
177  std::vector< NodeId > v;
178 
179  for (current = n1; current != n2; current = mark[current])
180  v.push_back(current);
181 
182  v.push_back(n2);
183 
184  return v;
185  }
186 
187  nodeFIFO.pushBack(new_one);
188  }
189 
190  // check the children
191  for (const auto new_one : children(current)) {
192  if (mark.exists(new_one)) // the node has already been visited
193  continue;
194 
195  mark.insert(new_one, current);
196 
197  if (new_one == n1) {
198  std::vector< NodeId > v;
199 
200  for (current = n1; current != n2; current = mark[current])
201  v.push_back(current);
202 
203  v.push_back(n2);
204  return v;
205  }
206 
207  nodeFIFO.pushBack(new_one);
208  }
209  }
210 
211  GUM_ERROR(NotFound, "no path found");
212  }
213 
214  const std::string MixedGraph::toDot() const {
215  std::stringstream output;
216  std::stringstream nodeStream;
217  std::stringstream edgeStream;
218  List< NodeId > treatedNodes;
219  output << "digraph \""
220  << "no_name\" {" << std::endl;
221  nodeStream << "node [shape = ellipse];" << std::endl;
222  std::string tab = " ";
223 
224  for (const auto node : nodes()) {
225  nodeStream << tab << node << ";";
226 
227  for (const auto nei : neighbours(node))
228  if (!treatedNodes.exists(nei))
229  edgeStream << tab << node << " -> " << nei << " [dir=none];"
230  << std::endl;
231 
232  for (const auto chi : children(node))
233  edgeStream << tab << node << " -> " << chi << ";" << std::endl;
234 
235  treatedNodes.insert(node);
236  }
237 
238  output << nodeStream.str() << std::endl
239  << edgeStream.str() << std::endl
240  << "}" << std::endl;
241 
242  return output.str();
243  }
244 
246  std::ostream& operator<<(std::ostream& stream, const MixedGraph& g) {
247  stream << g.toString();
248  return stream;
249  }
250 
251 } /* namespace gum */
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition: list_tpl.h:1967
const std::vector< NodeId > mixedUnorientedPath(const NodeId node1, const NodeId node2) const
returns a mixed/directed path from node1 to node2 in the arc/edge set
Definition: mixedGraph.cpp:133
const std::string toString() const
to friendly display the content of the ArcGraphPart
virtual const std::string toString() const
to friendly display the content of the MixedGraph
Definition: mixedGraph.cpp:61
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Generic doubly linked lists.
Definition: list.h:369
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void popFront()
Removes the first element of a List, if any.
Definition: list_tpl.h:1961
The class for generic Hash Tables.
Definition: hashTable.h:676
const NodeSet & neighbours(const NodeId id) const
returns the set of edges adjacent to a given node
virtual const std::string toDot() const
to friendly display mixed graph in DOT format
Definition: mixedGraph.cpp:214
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:583
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition: list_tpl.h:1589
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
Base class for all oriented graphs.
Definition: diGraph.h:108
std::string toString() const
a function to display the set of nodes
Inline implementation of Base classes for mixed graphs.
Class for node sets in graph.
bool exists(const Val &val) const
Checks whether there exists a given element in the list.
Definition: list_tpl.h:1854
MixedGraph(Size nodes_size=HashTableConst::default_size, bool nodes_resize_policy=true, Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true, Size edges_size=HashTableConst::default_size, bool edges_resize_policy=true)
default constructor
Definition: mixedGraph.cpp:34
const std::string toString() const
to friendly display the content of the EdgeGraphPart
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
Definition: list_tpl.h:1616
Val & front() const
Returns a reference to first element of a list, if any.
Definition: list_tpl.h:1828
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Base classes for mixed directed/undirected graphs.
Base class for undirected graphs.
Definition: undiGraph.h:106
const std::vector< NodeId > mixedOrientedPath(const NodeId node1, const NodeId node2) const
returns a mixed edge/directed arc path from node1 to node2 in the arc/edge set
Definition: mixedGraph.cpp:71
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
virtual ~MixedGraph()
destructor
Definition: mixedGraph.cpp:56
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
Base class for mixed graphs.
Definition: mixedGraph.h:124