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