Wrote exceptions, factorgraph unit tests and several other improvements
[libdai.git] / include / dai / bipgraph.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2010 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 /// \file
13 /// \brief Defines the BipartiteGraph class, which represents a bipartite graph
14
15
16 #ifndef __defined_libdai_bipgraph_h
17 #define __defined_libdai_bipgraph_h
18
19
20 #include <ostream>
21 #include <vector>
22 #include <algorithm>
23 #include <dai/util.h>
24 #include <dai/smallset.h>
25 #include <dai/exceptions.h>
26
27
28 namespace dai {
29
30
31 /// Represents the neighborhood structure of nodes in an undirected, bipartite graph.
32 /** A bipartite graph has two types of nodes: type 1 and type 2. Edges can occur only between
33 * nodes of different type. Nodes are indexed by an unsigned integer. If there are nrNodes1()
34 * nodes of type 1 and nrNodes2() nodes of type 2, the nodes of type 1 are numbered
35 * 0,1,2,...,nrNodes1()-1 and the nodes of type 2 are numbered 0,1,2,...,nrNodes2()-1. An edge
36 * between node \a n1 of type 1 and node \a n2 of type 2 is represented by a BipartiteGraph::Edge(\a n1,\a n2).
37 *
38 * A BipartiteGraph is implemented as a sparse adjacency list, i.e., it stores for each node a list of
39 * its neighboring nodes. More precisely: it stores for each node of type 1 a vector of Neighbor structures
40 * (accessible by the nb1() method) describing the neighboring nodes of type 2; similarly, for each node
41 * of type 2 it stores a vector of Neighbor structures (accessibly by the nb2() method) describing the
42 * neighboring nodes of type 1.
43 * Thus, each node has an associated variable of type BipartiteGraph::Neighbors, which is a vector of
44 * Neighbor structures, describing its neighboring nodes of the other type.
45 * \idea Cache second-order neighborhoods in BipartiteGraph.
46 */
47 class BipartiteGraph {
48 public:
49 /// Describes the neighbor relationship of two nodes in a BipartiteGraph.
50 /** Sometimes we want to do an action, such as sending a
51 * message, for all edges in a graph. However, most graphs
52 * will be sparse, so we need some way of storing a set of
53 * the neighbors of a node, which is both fast and
54 * memory-efficient. We also need to be able to go between
55 * viewing node \a a as a neighbor of node \a b, and node \a b
56 * as a neighbor of node \a a. The Neighbor struct solves
57 * both of these problems. Each node has a list of neighbors,
58 * stored as a std::vector<\link Neighbor \endlink>, and
59 * extra information is included in the Neighbor struct which
60 * allows us to access a node as a neighbor of its neighbor
61 * (the \c dual member).
62 *
63 * By convention, variable identifiers naming indices into a
64 * vector of neighbors are prefixed with an underscore ("_").
65 * The neighbor list which they point into is then understood
66 * from context. For example:
67 *
68 * \code
69 * void BP::calcNewMessage( size_t i, size_t _I )
70 * \endcode
71 *
72 * Here, \a i is the "absolute" index of node i, but \a _I is
73 * understood as a "relative" index, giving node \a I 's entry in
74 * <tt>nb1(i)</tt>. The corresponding Neighbor structure can be
75 * accessed as <tt>nb1(i,_I)</tt> or <tt>nb1(i)[_I]</tt>. The
76 * absolute index of \a _I, which would be called \a I, can be
77 * recovered from the \c node member: <tt>nb1(i,_I).node</tt>.
78 * The \c iter member gives the relative index \a _I, and the
79 * \c dual member gives the "dual" relative index, i.e., the
80 * index of \a i in \a I 's neighbor list.
81 *
82 * \code
83 * Neighbor n = nb1(i,_I);
84 * n.node == I &&
85 * n.iter == _I &&
86 * nb2(n.node,n.dual).node == i
87 * \endcode
88 *
89 * In a FactorGraph, the nodes of type 1 represent variables, and
90 * the nodes of type 2 represent factors. For convenience, nb1() is
91 * called FactorGraph::nbV(), and nb2() is called FactorGraph::nbF().
92 *
93 * There is no easy way to transform a pair of absolute node
94 * indices \a i and \a I into a Neighbor structure relative
95 * to one of the nodes. Such a feature has never yet been
96 * found to be necessary. Iteration over edges can always be
97 * accomplished using the Neighbor lists, and by writing
98 * functions that accept relative indices:
99 * \code
100 * for( size_t i = 0; i < nrVars(); ++i )
101 * foreach( const Neighbor &I, nbV(i) )
102 * calcNewMessage( i, I.iter );
103 * \endcode
104 */
105 struct Neighbor {
106 /// Corresponds to the index of this Neighbor entry in the vector of neighbors
107 size_t iter;
108 /// Contains the number of the neighboring node
109 size_t node;
110 /// Contains the "dual" iter
111 size_t dual;
112
113 /// Default constructor
114 Neighbor() {}
115 /// Constructor that sets the Neighbor members according to the parameters
116 Neighbor( size_t iter, size_t node, size_t dual ) : iter(iter), node(node), dual(dual) {}
117
118 /// Cast to \c size_t returns \c node member
119 operator size_t () const { return node; }
120 };
121
122 /// Describes the neighbors of some node.
123 typedef std::vector<Neighbor> Neighbors;
124
125 /// Represents an edge: an Edge(\a n1,\a n2) corresponds to the edge between node \a n1 of type 1 and node \a n2 of type 2.
126 typedef std::pair<size_t,size_t> Edge;
127
128 private:
129 /// Contains for each node of type 1 a vector of its neighbors
130 std::vector<Neighbors> _nb1;
131
132 /// Contains for each node of type 2 a vector of its neighbors
133 std::vector<Neighbors> _nb2;
134
135 /// Used internally by isTree()
136 struct levelType {
137 /// Indices of nodes of type 1
138 std::vector<size_t> ind1;
139 /// Indices of nodes of type 2
140 std::vector<size_t> ind2;
141 };
142
143 public:
144 /// \name Constructors and destructors
145 //@{
146 /// Default constructor (creates an empty bipartite graph)
147 BipartiteGraph() : _nb1(), _nb2() {}
148
149 /// Constructs BipartiteGraph with \a nr1 nodes of type 1, \a nr2 nodes of type 2 and no edges.
150 BipartiteGraph( size_t nr1, size_t nr2 ) : _nb1(nr1), _nb2(nr2) {}
151
152 /// Constructs BipartiteGraph from a range of edges.
153 /** \tparam EdgeInputIterator Iterator that iterates over instances of BipartiteGraph::Edge.
154 * \param nrNodes1 The number of nodes of type 1.
155 * \param nrNodes2 The number of nodes of type 2.
156 * \param begin Points to the first edge.
157 * \param end Points just beyond the last edge.
158 */
159 template<typename EdgeInputIterator>
160 BipartiteGraph( size_t nrNodes1, size_t nrNodes2, EdgeInputIterator begin, EdgeInputIterator end ) : _nb1(), _nb2() {
161 construct( nrNodes1, nrNodes2, begin, end );
162 }
163 //@}
164
165 /// \name Accessors and mutators
166 //@{
167 /// Returns constant reference to the \a _i2 'th neighbor of node \a i1 of type 1
168 const Neighbor & nb1( size_t i1, size_t _i2 ) const {
169 DAI_DEBASSERT( i1 < _nb1.size() );
170 DAI_DEBASSERT( _i2 < _nb1[i1].size() );
171 return _nb1[i1][_i2];
172 }
173 /// Returns reference to the \a _i2 'th neighbor of node \a i1 of type 1
174 Neighbor & nb1( size_t i1, size_t _i2 ) {
175 DAI_DEBASSERT( i1 < _nb1.size() );
176 DAI_DEBASSERT( _i2 < _nb1[i1].size() );
177 return _nb1[i1][_i2];
178 }
179
180 /// Returns constant reference to the \a _i1 'th neighbor of node \a i2 of type 2
181 const Neighbor & nb2( size_t i2, size_t _i1 ) const {
182 DAI_DEBASSERT( i2 < _nb2.size() );
183 DAI_DEBASSERT( _i1 < _nb2[i2].size() );
184 return _nb2[i2][_i1];
185 }
186 /// Returns reference to the \a _i1 'th neighbor of node \a i2 of type 2
187 Neighbor & nb2( size_t i2, size_t _i1 ) {
188 DAI_DEBASSERT( i2 < _nb2.size() );
189 DAI_DEBASSERT( _i1 < _nb2[i2].size() );
190 return _nb2[i2][_i1];
191 }
192
193 /// Returns constant reference to all neighbors of node \a i1 of type 1
194 const Neighbors & nb1( size_t i1 ) const {
195 DAI_DEBASSERT( i1 < _nb1.size() );
196 return _nb1[i1];
197 }
198 /// Returns reference to all neighbors of node \a i1 of type 1
199 Neighbors & nb1( size_t i1 ) {
200 DAI_DEBASSERT( i1 < _nb1.size() );
201 return _nb1[i1];
202 }
203
204 /// Returns constant reference to all neighbors of node \a i2 of type 2
205 const Neighbors & nb2( size_t i2 ) const {
206 DAI_DEBASSERT( i2 < _nb2.size() );
207 return _nb2[i2];
208 }
209 /// Returns reference to all neighbors of node \a i2 of type 2
210 Neighbors & nb2( size_t i2 ) {
211 DAI_DEBASSERT( i2 < _nb2.size() );
212 return _nb2[i2];
213 }
214 //@}
215
216 /// \name Adding nodes and edges
217 //@{
218 /// (Re)constructs BipartiteGraph from a range of edges.
219 /** \tparam EdgeInputIterator Iterator that iterates over instances of BipartiteGraph::Edge.
220 * \param nrNodes1 The number of nodes of type 1.
221 * \param nrNodes2 The number of nodes of type 2.
222 * \param begin Points to the first edge.
223 * \param end Points just beyond the last edge.
224 */
225 template<typename EdgeInputIterator>
226 void construct( size_t nrNodes1, size_t nrNodes2, EdgeInputIterator begin, EdgeInputIterator end );
227
228 /// Adds a node of type 1 without neighbors and returns the index of the added node.
229 size_t addNode1() { _nb1.push_back( Neighbors() ); return _nb1.size() - 1; }
230
231 /// Adds a node of type 2 without neighbors and returns the index of the added node.
232 size_t addNode2() { _nb2.push_back( Neighbors() ); return _nb2.size() - 1; }
233
234
235 /// Adds a node of type 1, with neighbors specified by a range of nodes of type 2.
236 /** \tparam NodeInputIterator Iterator that iterates over instances of \c size_t.
237 * \param begin Points to the first index of the nodes of type 2 that should become neighbors of the added node.
238 * \param end Points just beyond the last index of the nodes of type 2 that should become neighbors of the added node.
239 * \param sizeHint For improved efficiency, the size of the range may be specified by \a sizeHint.
240 * \returns Index of the added node.
241 */
242 template <typename NodeInputIterator>
243 size_t addNode1( NodeInputIterator begin, NodeInputIterator end, size_t sizeHint = 0 ) {
244 Neighbors nbs1new;
245 nbs1new.reserve( sizeHint );
246 size_t iter = 0;
247 for( NodeInputIterator it = begin; it != end; ++it ) {
248 DAI_ASSERT( *it < nrNodes2() );
249 Neighbor nb1new( iter, *it, nb2(*it).size() );
250 Neighbor nb2new( nb2(*it).size(), nrNodes1(), iter++ );
251 nbs1new.push_back( nb1new );
252 nb2( *it ).push_back( nb2new );
253 }
254 _nb1.push_back( nbs1new );
255 return _nb1.size() - 1;
256 }
257
258 /// Adds a node of type 2, with neighbors specified by a range of nodes of type 1.
259 /** \tparam NodeInputIterator Iterator that iterates over instances of \c size_t.
260 * \param begin Points to the first index of the nodes of type 1 that should become neighbors of the added node.
261 * \param end Points just beyond the last index of the nodes of type 1 that should become neighbors of the added node.
262 * \param sizeHint For improved efficiency, the size of the range may be specified by \a sizeHint.
263 * \returns Index of the added node.
264 */
265 template <typename NodeInputIterator>
266 size_t addNode2( NodeInputIterator begin, NodeInputIterator end, size_t sizeHint = 0 ) {
267 Neighbors nbs2new;
268 nbs2new.reserve( sizeHint );
269 size_t iter = 0;
270 for( NodeInputIterator it = begin; it != end; ++it ) {
271 DAI_ASSERT( *it < nrNodes1() );
272 Neighbor nb2new( iter, *it, nb1(*it).size() );
273 Neighbor nb1new( nb1(*it).size(), nrNodes2(), iter++ );
274 nbs2new.push_back( nb2new );
275 nb1( *it ).push_back( nb1new );
276 }
277 _nb2.push_back( nbs2new );
278 return _nb2.size() - 1;
279 }
280
281 /// Adds an edge between node \a n1 of type 1 and node \a n2 of type 2.
282 /** If \a check == \c true, only adds the edge if it does not exist already.
283 */
284 void addEdge( size_t n1, size_t n2, bool check = true );
285 //@}
286
287 /// \name Erasing nodes and edges
288 //@{
289 /// Removes node \a n1 of type 1 and all incident edges; indices of other nodes are changed accordingly.
290 void eraseNode1( size_t n1 );
291
292 /// Removes node \a n2 of type 2 and all incident edges; indices of other nodes are changed accordingly.
293 void eraseNode2( size_t n2 );
294
295 /// Removes edge between node \a n1 of type 1 and node \a n2 of type 2.
296 void eraseEdge( size_t n1, size_t n2 );
297 //@}
298
299 /// \name Queries
300 //@{
301 /// Returns number of nodes of type 1
302 size_t nrNodes1() const { return _nb1.size(); }
303 /// Returns number of nodes of type 2
304 size_t nrNodes2() const { return _nb2.size(); }
305
306 /// Calculates the number of edges, time complexity: O(nrNodes1())
307 size_t nrEdges() const {
308 size_t sum = 0;
309 for( size_t i1 = 0; i1 < nrNodes1(); i1++ )
310 sum += nb1(i1).size();
311 return sum;
312 }
313
314 /// Returns true if the graph contains an edge between node \a n1 of type 1 and node \a n2 of type 2.
315 /** \note The time complexity is linear in the number of neighbors of \a n1 or \a n2
316 */
317 bool hasEdge( size_t n1, size_t n2 ) const {
318 if( nb1(n1).size() < nb2(n2).size() ) {
319 for( size_t _n2 = 0; _n2 < nb1(n1).size(); _n2++ )
320 if( nb1( n1, _n2 ) == n2 )
321 return true;
322 } else {
323 for( size_t _n1 = 0; _n1 < nb2(n2).size(); _n1++ )
324 if( nb2( n2, _n1 ) == n1 )
325 return true;
326 }
327 return false;
328 }
329
330 /// Returns the index of a given node \a n2 of type 2 amongst the neighbors of node \a n1 of type 1
331 /** \note The time complexity is linear in the number of neighbors of \a n1
332 * \throw OBJECT_NOT_FOUND if \a n2 is not a neighbor of \a n1
333 */
334 size_t findNb1( size_t n1, size_t n2 ) {
335 for( size_t _n2 = 0; _n2 < nb1(n1).size(); _n2++ )
336 if( nb1( n1, _n2 ) == n2 )
337 return _n2;
338 DAI_THROW(OBJECT_NOT_FOUND);
339 return nb1(n1).size();
340 }
341
342 /// Returns the index of a given node \a n1 of type 1 amongst the neighbors of node \a n2 of type 2
343 /** \note The time complexity is linear in the number of neighbors of \a n2
344 * \throw OBJECT_NOT_FOUND if \a n1 is not a neighbor of \a n2
345 */
346 size_t findNb2( size_t n1, size_t n2 ) {
347 for( size_t _n1 = 0; _n1 < nb2(n2).size(); _n1++ )
348 if( nb2( n2, _n1 ) == n1 )
349 return _n1;
350 DAI_THROW(OBJECT_NOT_FOUND);
351 return nb2(n2).size();
352 }
353
354 /// Calculates second-order neighbors (i.e., neighbors of neighbors) of node \a n1 of type 1.
355 /** If \a include == \c true, includes \a n1 itself, otherwise excludes \a n1.
356 * \note In libDAI versions 0.2.4 and earlier, this function used to return a std::vector<size_t>
357 */
358 SmallSet<size_t> delta1( size_t n1, bool include = false ) const;
359
360 /// Calculates second-order neighbors (i.e., neighbors of neighbors) of node \a n2 of type 2.
361 /** If \a include == \c true, includes \a n2 itself, otherwise excludes \a n2.
362 * \note In libDAI versions 0.2.4 and earlier, this function used to return a std::vector<size_t>
363 */
364 SmallSet<size_t> delta2( size_t n2, bool include = false ) const;
365
366 /// Returns true if the graph is connected
367 bool isConnected() const;
368
369 /// Returns true if the graph is a tree, i.e., if it is singly connected and connected.
370 bool isTree() const;
371
372 /// Comparison operator which returns true if two graphs are identical
373 /** \note Two graphs are called identical if they have the same number of nodes
374 * of both types and the same edges (i.e., \a x has an edge between nodes
375 * n1 and n2 if and only if \c *this has an edge between nodes n1 and n2).
376 */
377 bool operator==( const BipartiteGraph& x ) const {
378 if( nrNodes1() != x.nrNodes1() )
379 return false;
380 if( nrNodes2() != x.nrNodes2() )
381 return false;
382 for( size_t n1 = 0; n1 < nrNodes1(); n1++ ) {
383 if( nb1(n1).size() != x.nb1(n1).size() )
384 return false;
385 foreach( const Neighbor &n2, nb1(n1) )
386 if( !x.hasEdge( n1, n2 ) )
387 return false;
388 foreach( const Neighbor &n2, x.nb1(n1) )
389 if( !hasEdge( n1, n2 ) )
390 return false;
391 }
392 return true;
393 }
394
395 /// Asserts internal consistency
396 void checkConsistency() const;
397 //@}
398
399 /// \name Input and output
400 //@{
401 /// Writes this BipartiteGraph to an output stream in GraphViz .dot syntax
402 void printDot( std::ostream& os ) const;
403 //@}
404 };
405
406
407 template<typename EdgeInputIterator>
408 void BipartiteGraph::construct( size_t nrNodes1, size_t nrNodes2, EdgeInputIterator begin, EdgeInputIterator end ) {
409 _nb1.clear();
410 _nb1.resize( nrNodes1 );
411 _nb2.clear();
412 _nb2.resize( nrNodes2 );
413
414 for( EdgeInputIterator e = begin; e != end; e++ ) {
415 #ifdef DAI_DEBUG
416 addEdge( e->first, e->second, true );
417 #else
418 addEdge( e->first, e->second, false );
419 #endif
420 }
421 }
422
423
424 } // end of namespace dai
425
426
427 /** \example example_bipgraph.cpp
428 * This example deals with the following bipartite graph:
429 * \dot
430 * graph example {
431 * ordering=out;
432 * subgraph cluster_type1 {
433 * node[shape=circle,width=0.4,fixedsize=true,style=filled];
434 * 12 [label="2"];
435 * 11 [label="1"];
436 * 10 [label="0"];
437 * }
438 * subgraph cluster_type2 {
439 * node[shape=polygon,regular=true,sides=4,width=0.4,fixedsize=true,style=filled];
440 * 21 [label="1"];
441 * 20 [label="0"];
442 * }
443 * 10 -- 20;
444 * 11 -- 20;
445 * 12 -- 20;
446 * 11 -- 21;
447 * 12 -- 21;
448 * }
449 * \enddot
450 * It has three nodes of type 1 (drawn as circles) and two nodes of type 2 (drawn as rectangles).
451 * Node 0 of type 1 has only one neighbor (node 0 of type 2), but node 0 of type 2 has three neighbors (nodes 0,1,2 of type 1).
452 * The example code shows how to construct a BipartiteGraph object representing this bipartite graph and
453 * how to iterate over nodes and their neighbors.
454 *
455 * \section Output
456 * \verbinclude examples/example_bipgraph.out
457 *
458 * \section Source
459 */
460
461
462 #endif