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