aGrUM  0.16.0
undiGraph.cpp
Go to the documentation of this file.
1 
29 #include <agrum/graphs/undiGraph.h>
30 
31 #ifdef GUM_NO_INLINE
33 #endif // GUM_NOINLINE
34 #include "graphElements.h"
35 #include <cstdio>
36 #include <iostream>
37 
38 namespace gum {
39 
41  bool nodes_resize_policy,
42  Size edges_size,
43  bool edges_resize_policy) :
44  NodeGraphPart(nodes_size, nodes_resize_policy),
45  EdgeGraphPart(edges_size, edges_resize_policy) {
46  GUM_CONSTRUCTOR(UndiGraph);
47  }
48 
50  GUM_CONS_CPY(UndiGraph);
51  }
52 
53  UndiGraph::~UndiGraph() { GUM_DESTRUCTOR(UndiGraph); }
54 
57  NodeProperty< bool > examined_nodes = nodesProperty(false);
58  std::pair< NodeId, NodeId > thePair;
59  NodeId current, from_current;
60 
61  for (const auto node : nodes()) {
62  // check if the node has already been examined (if this is not the case,
63  // this means that we are on a new connected component)
64  if (!examined_nodes[node]) {
65  // indicates that we are examining a new node
66  examined_nodes[node] = true;
67 
68  // check recursively all the nodes of node's connected component
69  thePair.first = node;
70  thePair.second = node;
71  open_nodes.insert(thePair);
72 
73  while (!open_nodes.empty()) {
74  // get a node to propagate
75  thePair = open_nodes.front();
76  open_nodes.popFront();
77 
78  current = thePair.first;
79  from_current = thePair.second;
80 
81  // check the neighbours
82  for (const auto new_node : neighbours(current))
83 
84  // avoid to check the node we are coming from
85  if (new_node != from_current) {
86  if (examined_nodes[new_node])
87  return true;
88  else {
89  examined_nodes[new_node] = true;
90  thePair.first = new_node;
91  thePair.second = current;
92  open_nodes.insert(thePair);
93  }
94  }
95  }
96  }
97  }
98 
99  return false;
100  }
101 
102  const std::string UndiGraph::toString() const {
103  std::string s = NodeGraphPart::toString();
104  s += " , ";
106  return s;
107  }
108 
109  const std::string UndiGraph::toDot() const {
110  std::stringstream output;
111  std::stringstream nodeStream;
112  std::stringstream edgeStream;
113  List< NodeId > treatedNodes;
114  output << "digraph \""
115  << "no_name\" {" << std::endl
116  << "edge [dir=none]" << std::endl;
117  nodeStream << "node [shape = ellipse];" << std::endl;
118  std::string tab = " ";
119 
120  for (const auto node : nodes()) {
121  nodeStream << tab << node << ";";
122 
123  if (neighbours(node).size() > 0)
124  for (const auto nei : neighbours(node))
125  if (!treatedNodes.exists(nei))
126  edgeStream << tab << node << " -> " << nei << ";" << std::endl;
127 
128  treatedNodes.insert(node);
129  }
130 
131  output << nodeStream.str() << std::endl
132  << edgeStream.str() << std::endl
133  << "}" << std::endl;
134 
135  return output.str();
136  }
137 
140  UndiGraph partialGraph;
141 
142  for (const auto node : nodesSet) {
143  partialGraph.addNodeWithId(node);
144 
145  for (const auto nei : neighbours(node))
146  if (nodesSet.contains(nei) && partialGraph.existsNode(nei))
147  partialGraph.addEdge(node, nei);
148  }
149 
150  return partialGraph;
151  }
152 
154  std::ostream& operator<<(std::ostream& stream, const UndiGraph& g) {
155  stream << g.toString();
156  return stream;
157  }
158 
159 } /* namespace gum */
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition: list_tpl.h:1970
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:35
NodeProperty< VAL > nodesProperty(VAL(*f)(const NodeId &), Size size=0) const
a method to create a HashTable with key:NodeId and value:VAL
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Classes for undirected edge sets.
Definition: edgeGraphPart.h:75
Generic doubly linked lists.
Definition: list.h:372
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void popFront()
Removes the first element of a List, if any.
Definition: list_tpl.h:1964
The class for generic Hash Tables.
Definition: hashTable.h:679
const NodeSet & neighbours(const NodeId id) const
returns the set of edges adjacent to a given node
virtual ~UndiGraph()
destructor
Definition: undiGraph.cpp:53
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:605
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:1857
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:40
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:1619
Val & front() const
Returns a reference to first element of a list, if any.
Definition: list_tpl.h:1831
Base class for undirected graphs.
Definition: undiGraph.h:109
bool hasUndirectedCycle() const
checks whether the graph contains cycles
Definition: undiGraph.cpp:55
virtual const std::string toString() const
to friendly display the content of the graph
Definition: undiGraph.cpp:102
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Size NodeId
Type for node ids.
Definition: graphElements.h:98
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual const std::string toDot() const
to friendly display graph in DOT format
Definition: undiGraph.cpp:109
virtual UndiGraph partialUndiGraph(NodeSet nodesSet)
returns the partial graph formed by the nodes given in parameter
Definition: undiGraph.cpp:139