aGrUM  0.14.2
undiGraph.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  ***************************************************************************/
26 #include <agrum/graphs/undiGraph.h>
27 
28 #ifdef GUM_NO_INLINE
30 #endif // GUM_NOINLINE
31 #include "graphElements.h"
32 #include <cstdio>
33 #include <iostream>
34 
35 namespace gum {
36 
38  bool nodes_resize_policy,
39  Size edges_size,
40  bool edges_resize_policy) :
41  NodeGraphPart(nodes_size, nodes_resize_policy),
42  EdgeGraphPart(edges_size, edges_resize_policy) {
43  GUM_CONSTRUCTOR(UndiGraph);
44  }
45 
47  GUM_CONS_CPY(UndiGraph);
48  }
49 
50  UndiGraph::~UndiGraph() { GUM_DESTRUCTOR(UndiGraph); }
51 
54  NodeProperty< bool > examined_nodes = nodesProperty(false);
55  std::pair< NodeId, NodeId > thePair;
56  NodeId current, from_current;
57 
58  for (const auto node : nodes()) {
59  // check if the node has already been examined (if this is not the case,
60  // this means that we are on a new connected component)
61  if (!examined_nodes[node]) {
62  // indicates that we are examining a new node
63  examined_nodes[node] = true;
64 
65  // check recursively all the nodes of node's connected component
66  thePair.first = node;
67  thePair.second = node;
68  open_nodes.insert(thePair);
69 
70  while (!open_nodes.empty()) {
71  // get a node to propagate
72  thePair = open_nodes.front();
73  open_nodes.popFront();
74 
75  current = thePair.first;
76  from_current = thePair.second;
77 
78  // check the neighbours
79  for (const auto new_node : neighbours(current))
80 
81  // avoid to check the node we are coming from
82  if (new_node != from_current) {
83  if (examined_nodes[new_node])
84  return true;
85  else {
86  examined_nodes[new_node] = true;
87  thePair.first = new_node;
88  thePair.second = current;
89  open_nodes.insert(thePair);
90  }
91  }
92  }
93  }
94  }
95 
96  return false;
97  }
98 
99  const std::string UndiGraph::toString() const {
100  std::string s = NodeGraphPart::toString();
101  s += " , ";
103  return s;
104  }
105 
106  const std::string UndiGraph::toDot() const {
107  std::stringstream output;
108  std::stringstream nodeStream;
109  std::stringstream edgeStream;
110  List< NodeId > treatedNodes;
111  output << "digraph \""
112  << "no_name\" {" << std::endl
113  << "edge [dir=none]" << std::endl;
114  nodeStream << "node [shape = ellipse];" << std::endl;
115  std::string tab = " ";
116 
117  for (const auto node : nodes()) {
118  nodeStream << tab << node << ";";
119 
120  if (neighbours(node).size() > 0)
121  for (const auto nei : neighbours(node))
122  if (!treatedNodes.exists(nei))
123  edgeStream << tab << node << " -> " << nei << ";" << std::endl;
124 
125  treatedNodes.insert(node);
126  }
127 
128  output << nodeStream.str() << std::endl
129  << edgeStream.str() << std::endl
130  << "}" << std::endl;
131 
132  return output.str();
133  }
134 
137  UndiGraph partialGraph;
138 
139  for (const auto node : nodesSet) {
140  partialGraph.addNodeWithId(node);
141 
142  for (const auto nei : neighbours(node))
143  if (nodesSet.contains(nei) && partialGraph.existsNode(nei))
144  partialGraph.addEdge(node, nei);
145  }
146 
147  return partialGraph;
148  }
149 
151  std::ostream& operator<<(std::ostream& stream, const UndiGraph& g) {
152  stream << g.toString();
153  return stream;
154  }
155 
156 } /* namespace gum */
Inline implementation of Base classes for undirected graphs.
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition: list_tpl.h:1967
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
Size size() const
alias for sizeNodes
virtual void addEdge(const NodeId first, const NodeId second)
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:32
NodeProperty< VAL > nodesProperty(VAL(*f)(const NodeId &), Size size=0) const
a method to create a HashTable with key:NodeId and value:VAL
Base classes for undirected graphs.
Classes for undirected edge sets.
Definition: edgeGraphPart.h:72
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 ~UndiGraph()
destructor
Definition: undiGraph.cpp:50
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 NodeGraphPart & nodes() const
return *this as a NodeGraphPart
std::string toString() const
a function to display the set of nodes
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
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
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:37
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
Base class for undirected graphs.
Definition: undiGraph.h:106
bool hasUndirectedCycle() const
checks whether the graph contains cycles
Definition: undiGraph.cpp:52
virtual const std::string toString() const
to friendly display the content of the graph
Definition: undiGraph.cpp:99
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
Size NodeId
Type for node ids.
Definition: graphElements.h:97
some utils for topology : NodeId, Edge, Arc and consorts ...
virtual const std::string toDot() const
to friendly display graph in DOT format
Definition: undiGraph.cpp:106
virtual UndiGraph partialUndiGraph(NodeSet nodesSet)
returns the partial graph formed by the nodes given in parameter
Definition: undiGraph.cpp:136