libDAI
graph.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
11 
12 
13 #ifndef __defined_libdai_graph_h
14 #define __defined_libdai_graph_h
15 
16 
17 #include <ostream>
18 #include <vector>
19 #include <algorithm>
20 #include <dai/util.h>
21 #include <dai/exceptions.h>
22 #include <dai/smallset.h>
23 
24 
25 namespace dai {
26 
27 
29 
73 struct Neighbor {
75  size_t iter;
77  size_t node;
79  size_t dual;
80 
82  Neighbor() {}
84  Neighbor( size_t iter, size_t node, size_t dual ) : iter(iter), node(node), dual(dual) {}
85 
87  operator size_t () const { return node; }
88 };
89 
90 
92 typedef std::vector<Neighbor> Neighbors;
93 
94 
96 
100 typedef std::pair<size_t,size_t> Edge;
101 
102 
104 
114 class GraphAL {
115  private:
117  std::vector<Neighbors> _nb;
118 
119  public:
121 
122  GraphAL() : _nb() {}
124 
126  GraphAL( size_t nr ) : _nb( nr ) {}
127 
129 
135  template<typename EdgeInputIterator>
136  GraphAL( size_t nr, EdgeInputIterator begin, EdgeInputIterator end, bool check=true ) : _nb() {
137  construct( nr, begin, end, check );
138  }
140 
142 
143  const Neighbor & nb( size_t n1, size_t _n2 ) const {
145  DAI_DEBASSERT( n1 < _nb.size() );
146  DAI_DEBASSERT( _n2 < _nb[n1].size() );
147  return _nb[n1][_n2];
148  }
150  Neighbor & nb( size_t n1, size_t _n2 ) {
151  DAI_DEBASSERT( n1 < _nb.size() );
152  DAI_DEBASSERT( _n2 < _nb[n1].size() );
153  return _nb[n1][_n2];
154  }
155 
157  const Neighbors & nb( size_t n ) const {
158  DAI_DEBASSERT( n < _nb.size() );
159  return _nb[n];
160  }
162  Neighbors & nb( size_t n ) {
163  DAI_DEBASSERT( n < _nb.size() );
164  return _nb[n];
165  }
167 
169 
170 
177  template<typename EdgeInputIterator>
178  void construct( size_t nr, EdgeInputIterator begin, EdgeInputIterator end, bool check=true );
179 
181  size_t addNode() { _nb.push_back( Neighbors() ); return _nb.size() - 1; }
182 
184 
190  template <typename NodeInputIterator>
191  size_t addNode( NodeInputIterator begin, NodeInputIterator end, size_t sizeHint = 0 ) {
192  Neighbors nbsnew;
193  nbsnew.reserve( sizeHint );
194  size_t iter = 0;
195  for( NodeInputIterator it = begin; it != end; ++it ) {
196  DAI_ASSERT( *it < nrNodes() );
197  Neighbor nb1new( iter, *it, nb(*it).size() );
198  Neighbor nb2new( nb(*it).size(), nrNodes(), iter++ );
199  nbsnew.push_back( nb1new );
200  nb( *it ).push_back( nb2new );
201  }
202  _nb.push_back( nbsnew );
203  return _nb.size() - 1;
204  }
205 
207 
209  GraphAL& addEdge( size_t n1, size_t n2, bool check = true );
211 
213 
214  void eraseNode( size_t n );
216 
218  void eraseEdge( size_t n1, size_t n2 );
220 
222 
223  size_t nrNodes() const { return _nb.size(); }
225 
227  size_t nrEdges() const {
228  size_t sum = 0;
229  for( size_t i = 0; i < nrNodes(); i++ )
230  sum += nb(i).size();
231  return sum / 2;
232  }
233 
235 
237  bool hasEdge( size_t n1, size_t n2 ) const {
238  if( nb(n1).size() < nb(n2).size() ) {
239  for( size_t _n2 = 0; _n2 < nb(n1).size(); _n2++ )
240  if( nb( n1, _n2 ) == n2 )
241  return true;
242  } else {
243  for( size_t _n1 = 0; _n1 < nb(n2).size(); _n1++ )
244  if( nb( n2, _n1 ) == n1 )
245  return true;
246  }
247  return false;
248  }
249 
251 
254  size_t findNb( size_t n1, size_t n2 ) {
255  for( size_t _n2 = 0; _n2 < nb(n1).size(); _n2++ )
256  if( nb( n1, _n2 ) == n2 )
257  return _n2;
258  DAI_THROW(OBJECT_NOT_FOUND);
259  return nb(n1).size();
260  }
261 
263  SmallSet<size_t> nbSet( size_t n ) const;
264 
266  bool isConnected() const;
267 
269  bool isTree() const;
270 
272  void checkConsistency() const;
273 
275 
279  bool operator==( const GraphAL& x ) const {
280  if( nrNodes() != x.nrNodes() )
281  return false;
282  for( size_t n1 = 0; n1 < nrNodes(); n1++ ) {
283  if( nb(n1).size() != x.nb(n1).size() )
284  return false;
285  bforeach( const Neighbor &n2, nb(n1) )
286  if( !x.hasEdge( n1, n2 ) )
287  return false;
288  bforeach( const Neighbor &n2, x.nb(n1) )
289  if( !hasEdge( n1, n2 ) )
290  return false;
291  }
292  return true;
293  }
295 
297 
298  void printDot( std::ostream& os ) const;
300 
302  friend std::ostream& operator<<( std::ostream& os, const GraphAL& g ) {
303  g.printDot( os );
304  return os;
305  }
306 
308  std::string toString() const {
309  std::stringstream ss;
310  ss << *this;
311  return ss.str();
312  }
314 };
315 
316 
317 template<typename EdgeInputIterator>
318 void GraphAL::construct( size_t nr, EdgeInputIterator begin, EdgeInputIterator end, bool check ) {
319  _nb.clear();
320  _nb.resize( nr );
321 
322  for( EdgeInputIterator e = begin; e != end; e++ )
323  addEdge( e->first, e->second, check );
324 }
325 
326 
328 GraphAL createGraphFull( size_t N );
330 GraphAL createGraphGrid( size_t N1, size_t N2, bool periodic );
332 GraphAL createGraphGrid3D( size_t N1, size_t N2, size_t N3, bool periodic );
334 GraphAL createGraphLoop( size_t N );
336 GraphAL createGraphTree( size_t N );
338 
344 GraphAL createGraphRegular( size_t N, size_t d );
345 
346 
347 } // end of namespace dai
348 
349 
350 #endif
void checkConsistency() const
Asserts internal consistency.
Definition: graph.cpp:218
std::vector< Neighbor > Neighbors
Describes the set of neighbors of some node in a graph.
Definition: graph.h:92
void eraseEdge(size_t n1, size_t n2)
Removes edge between node n1 and node n2.
Definition: graph.cpp:63
void printDot(std::ostream &os) const
Writes a GraphAL to an output stream in GraphViz .dot syntax.
Definition: graph.cpp:205
GraphAL createGraphFull(size_t N)
Creates a fully-connected graph with N nodes.
Definition: graph.cpp:233
size_t findNb(size_t n1, size_t n2)
Returns the index of a given node n2 amongst the neighbors of n1.
Definition: graph.h:254
size_t nrNodes() const
Returns number of nodes.
Definition: graph.h:224
friend std::ostream & operator<<(std::ostream &os, const GraphAL &g)
Writes a GraphAL to an output stream.
Definition: graph.h:302
GraphAL & addEdge(size_t n1, size_t n2, bool check=true)
Adds an edge between node n1 and node n2.
Definition: graph.cpp:18
bool isConnected() const
Returns true if the graph is connected.
Definition: graph.cpp:104
GraphAL createGraphGrid(size_t N1, size_t N2, bool periodic)
Creates a two-dimensional rectangular grid of N1 by N2 nodes, which can be periodic.
Definition: graph.cpp:242
size_t iter
Corresponds to the index of this Neighbor entry in the vector of neighbors.
Definition: graph.h:75
std::string toString() const
Formats a GraphAL as a string.
Definition: graph.h:308
size_t dual
Contains the "dual" index (i.e., the index of this node in the Neighbors vector of the neighboring no...
Definition: graph.h:79
GraphAL createGraphRegular(size_t N, size_t d)
Creates a random regular graph of N nodes with uniform connectivity d.
Definition: graph.cpp:291
void eraseNode(size_t n)
Removes node n and all incident edges; indices of other nodes are changed accordingly.
Definition: graph.cpp:40
void construct(size_t nr, EdgeInputIterator begin, EdgeInputIterator end, bool check=true)
(Re)constructs GraphAL from a range of edges.
Definition: graph.h:318
Neighbor & nb(size_t n1, size_t _n2)
Returns reference to the _n2 'th neighbor of node n1.
Definition: graph.h:150
GraphAL createGraphGrid3D(size_t N1, size_t N2, size_t N3, bool periodic)
Creates a three-dimensional rectangular grid of N1 by N2 by N3 nodes, which can be periodic...
Definition: graph.cpp:257
size_t addNode()
Adds a node without neighbors and returns the index of the added node.
Definition: graph.h:181
#define bforeach
An alias to the BOOST_FOREACH macro from the boost::bforeach library.
Definition: util.h:48
Defines the Exception class and macros for throwing exceptions and doing assertions.
GraphAL createGraphTree(size_t N)
Creates a random tree-structured graph of N nodes.
Definition: graph.cpp:281
Neighbors & nb(size_t n)
Returns reference to all neighbors of node n.
Definition: graph.h:162
Defines general utility functions and adds an abstraction layer for platform-dependent functionality...
Neighbor(size_t iter, size_t node, size_t dual)
Constructor that allows setting the values of the member variables.
Definition: graph.h:84
bool hasEdge(size_t n1, size_t n2) const
Returns true if the graph contains an edge between nodes n1 and n2.
Definition: graph.h:237
size_t node
Contains the absolute index of the neighboring node.
Definition: graph.h:77
std::vector< Neighbors > _nb
Contains for each node a vector of its neighbors.
Definition: graph.h:117
size_t addNode(NodeInputIterator begin, NodeInputIterator end, size_t sizeHint=0)
Adds a node, with neighbors specified by a range of nodes.
Definition: graph.h:191
Defines the SmallSet<> class, which represents a set (optimized for a small number of elements)...
GraphAL createGraphLoop(size_t N)
Creates a graph consisting of a single loop of N nodes.
Definition: graph.cpp:273
Describes the neighbor relationship of two nodes in a graph.
Definition: graph.h:73
const Neighbors & nb(size_t n) const
Returns constant reference to all neighbors of node n.
Definition: graph.h:157
bool operator==(const GraphAL &x) const
Comparison operator which returns true if two graphs are identical.
Definition: graph.h:279
Namespace for libDAI.
Definition: alldai.cpp:16
const Neighbor & nb(size_t n1, size_t _n2) const
Returns constant reference to the _n2 'th neighbor of node n1.
Definition: graph.h:144
size_t nrEdges() const
Calculates the number of edges, time complexity: O(nrNodes())
Definition: graph.h:227
#define DAI_ASSERT(condition)
Assertion mechanism, similar to the standard assert() macro. It is always active, even if NDEBUG is d...
Definition: exceptions.h:60
#define DAI_DEBASSERT(x)
Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined.
Definition: exceptions.h:65
Represents the neighborhood structure of nodes in an undirected graph.
Definition: graph.h:114
std::pair< size_t, size_t > Edge
Represents an edge in a graph: an Edge(i,j) corresponds to the edge between node i and node j...
Definition: graph.h:100
GraphAL(size_t nr, EdgeInputIterator begin, EdgeInputIterator end, bool check=true)
Constructs GraphAL from a range of edges.
Definition: graph.h:136
SmallSet< size_t > nbSet(size_t n) const
Returns neighbors of node n as a SmallSet.
Definition: graph.cpp:96
Neighbor()
Default constructor.
Definition: graph.h:82
GraphAL(size_t nr)
Constructs GraphAL with nr nodes and no edges.
Definition: graph.h:126
bool isTree() const
Returns true if the graph is a tree, i.e., if it is singly connected and connected.
Definition: graph.cpp:160
GraphAL()
Default constructor (creates an empty graph).
Definition: graph.h:123