Miscellaneous smaller improvements
[libdai.git] / src / bipgraph.cpp
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 #include <dai/bipgraph.h>
13
14
15 namespace dai {
16
17
18 using namespace std;
19
20
21 void BipartiteGraph::addEdge( size_t n1, size_t n2, bool check ) {
22 DAI_ASSERT( n1 < nrNodes1() );
23 DAI_ASSERT( n2 < nrNodes2() );
24 bool exists = false;
25 if( check ) {
26 // Check whether the edge already exists
27 foreach( const Neighbor &nb2, nb1(n1) )
28 if( nb2 == n2 ) {
29 exists = true;
30 break;
31 }
32 }
33 if( !exists ) { // Add edge
34 Neighbor nb_1( nb1(n1).size(), n2, nb2(n2).size() );
35 Neighbor nb_2( nb_1.dual, n1, nb_1.iter );
36 nb1(n1).push_back( nb_1 );
37 nb2(n2).push_back( nb_2 );
38 }
39 }
40
41
42 void BipartiteGraph::eraseNode1( size_t n1 ) {
43 DAI_ASSERT( n1 < nrNodes1() );
44 // Erase neighbor entry of node n1
45 _nb1.erase( _nb1.begin() + n1 );
46 // Adjust neighbor entries of nodes of type 2
47 for( size_t n2 = 0; n2 < nrNodes2(); n2++ ) {
48 for( size_t iter = 0; iter < nb2(n2).size(); ) {
49 Neighbor &m1 = nb2(n2, iter);
50 if( m1.node == n1 ) {
51 // delete this entry, because it points to the deleted node
52 nb2(n2).erase( nb2(n2).begin() + iter );
53 } else if( m1.node > n1 ) {
54 // update this entry and the corresponding dual of the neighboring node of type 1
55 m1.iter = iter;
56 m1.node--;
57 nb1( m1.node, m1.dual ).dual = iter;
58 iter++;
59 } else {
60 // skip
61 iter++;
62 }
63 }
64 }
65 }
66
67
68 void BipartiteGraph::eraseNode2( size_t n2 ) {
69 DAI_ASSERT( n2 < nrNodes2() );
70 // Erase neighbor entry of node n2
71 _nb2.erase( _nb2.begin() + n2 );
72 // Adjust neighbor entries of nodes of type 1
73 for( size_t n1 = 0; n1 < nrNodes1(); n1++ ) {
74 for( size_t iter = 0; iter < nb1(n1).size(); ) {
75 Neighbor &m2 = nb1(n1, iter);
76 if( m2.node == n2 ) {
77 // delete this entry, because it points to the deleted node
78 nb1(n1).erase( nb1(n1).begin() + iter );
79 } else if( m2.node > n2 ) {
80 // update this entry and the corresponding dual of the neighboring node of type 2
81 m2.iter = iter;
82 m2.node--;
83 nb2( m2.node, m2.dual ).dual = iter;
84 iter++;
85 } else {
86 // skip
87 iter++;
88 }
89 }
90 }
91 }
92
93
94 void BipartiteGraph::eraseEdge( size_t n1, size_t n2 ) {
95 DAI_ASSERT( n1 < nrNodes1() );
96 DAI_ASSERT( n2 < nrNodes2() );
97 size_t iter;
98 // Search for edge among neighbors of n1
99 for( iter = 0; iter < nb1(n1).size(); iter++ )
100 if( nb1(n1, iter).node == n2 ) {
101 // Remove it
102 nb1(n1).erase( nb1(n1).begin() + iter );
103 break;
104 }
105 // Change the iter and dual values of the subsequent neighbors
106 for( ; iter < nb1(n1).size(); iter++ ) {
107 Neighbor &m2 = nb1( n1, iter );
108 m2.iter = iter;
109 nb2( m2.node, m2.dual ).dual = iter;
110 }
111 // Search for edge among neighbors of n2
112 for( iter = 0; iter < nb2(n2).size(); iter++ )
113 if( nb2(n2, iter).node == n1 ) {
114 // Remove it
115 nb2(n2).erase( nb2(n2).begin() + iter );
116 break;
117 }
118 // Change the iter and node values of the subsequent neighbors
119 for( ; iter < nb2(n2).size(); iter++ ) {
120 Neighbor &m1 = nb2( n2, iter );
121 m1.iter = iter;
122 nb1( m1.node, m1.dual ).dual = iter;
123 }
124 }
125
126
127 std::vector<size_t> BipartiteGraph::delta1( size_t n1, bool include ) const {
128 // get all second-order neighbors
129 std::vector<size_t> result;
130 foreach( const Neighbor &n2, nb1(n1) )
131 foreach( const Neighbor &m1, nb2(n2) )
132 if( include || (m1 != n1) )
133 result.push_back( m1 );
134 // remove duplicates
135 std::vector<size_t>::iterator it = std::unique( result.begin(), result.end() );
136 result.erase( it, result.end() );
137 return result;
138 }
139
140
141 std::vector<size_t> BipartiteGraph::delta2( size_t n2, bool include ) const {
142 // store all second-order neighbors
143 std::vector<size_t> result;
144 foreach( const Neighbor &n1, nb2(n2) )
145 foreach( const Neighbor &m2, nb1(n1) )
146 if( include || (m2 != n2) )
147 result.push_back( m2 );
148 // remove duplicates
149 std::vector<size_t>::iterator it = std::unique( result.begin(), result.end() );
150 result.erase( it, result.end() );
151 return result;
152 }
153
154
155 bool BipartiteGraph::isConnected() const {
156 if( nrNodes1() == 0 ) {
157 return true;
158 } else {
159 /*
160 // The BGL implementation is significantly slower...
161 using namespace boost;
162 typedef adjacency_list< vecS, vecS, undirectedS, property<vertex_distance_t, int> > boostGraph;
163 typedef pair<size_t, size_t> E;
164
165 // Copy graph structure into boostGraph object
166 size_t N = nrNodes1();
167 vector<E> edges;
168 edges.reserve( nrEdges() );
169 for( size_t n1 = 0; n1 < nrNodes1(); n1++ )
170 foreach( const Neighbor &n2, nb1(n1) )
171 edges.push_back( E( n1, n2.node + N ) );
172 boostGraph g( edges.begin(), edges.end(), nrNodes1() + nrNodes2() );
173
174 // Construct connected components using Boost Graph Library
175 std::vector<int> component( num_vertices( g ) );
176 int num_comp = connected_components( g, make_iterator_property_map(component.begin(), get(vertex_index, g)) );
177
178 return (num_comp == 1);
179 */
180
181 std::vector<bool> incomponent1( nrNodes1(), false );
182 std::vector<bool> incomponent2( nrNodes2(), false );
183
184 incomponent1[0] = true;
185 bool found_new_nodes;
186 do {
187 found_new_nodes = false;
188
189 // For all nodes of type 2, check if they are connected with the (growing) component
190 for( size_t n2 = 0; n2 < nrNodes2(); n2++ )
191 if( !incomponent2[n2] ) {
192 foreach( const Neighbor &n1, nb2(n2) ) {
193 if( incomponent1[n1] ) {
194 found_new_nodes = true;
195 incomponent2[n2] = true;
196 break;
197 }
198 }
199 }
200
201 // For all nodes of type 1, check if they are connected with the (growing) component
202 for( size_t n1 = 0; n1 < nrNodes1(); n1++ )
203 if( !incomponent1[n1] ) {
204 foreach( const Neighbor &n2, nb1(n1) ) {
205 if( incomponent2[n2] ) {
206 found_new_nodes = true;
207 incomponent1[n1] = true;
208 break;
209 }
210 }
211 }
212 } while( found_new_nodes );
213
214 // Check if there are remaining nodes (not in the component)
215 bool all_connected = true;
216 for( size_t n1 = 0; (n1 < nrNodes1()) && all_connected; n1++ )
217 if( !incomponent1[n1] )
218 all_connected = false;
219 for( size_t n2 = 0; (n2 < nrNodes2()) && all_connected; n2++ )
220 if( !incomponent2[n2] )
221 all_connected = false;
222
223 return all_connected;
224 }
225 }
226
227
228 bool BipartiteGraph::isTree() const {
229 using namespace std;
230 vector<levelType> levels;
231
232 bool foundCycle = false;
233 size_t nr_1 = 0;
234 size_t nr_2 = 0;
235
236 if( nrNodes1() == 0 || nrNodes2() == 0 )
237 return true;
238 else {
239 levelType newLevel;
240 do {
241 newLevel.ind1.clear();
242 newLevel.ind2.clear();
243 if( levels.size() == 0 ) {
244 size_t n1 = 0;
245 // add n1 to ind1
246 newLevel.ind1 = vector<size_t>( 1, n1 );
247 // add all neighbors of n1 to ind2
248 newLevel.ind2.reserve( nb1(n1).size() );
249 foreach( const Neighbor &n2, nb1(n1) )
250 newLevel.ind2.push_back( n2 );
251 } else {
252 const levelType &prevLevel = levels.back();
253 // build newLevel.ind1
254 foreach( size_t n2, prevLevel.ind2 ) { // for all n2 in the previous level
255 foreach( const Neighbor &n1, nb2(n2) ) { // for all neighbors n1 of n2
256 if( find( prevLevel.ind1.begin(), prevLevel.ind1.end(), n1 ) == prevLevel.ind1.end() ) { // n1 not in previous level
257 if( find( newLevel.ind1.begin(), newLevel.ind1.end(), n1 ) != newLevel.ind1.end() )
258 foundCycle = true; // n1 already in new level: we found a cycle
259 else
260 newLevel.ind1.push_back( n1 ); // add n1 to new level
261 }
262 if( foundCycle )
263 break;
264 }
265 if( foundCycle )
266 break;
267 }
268 // build newLevel.ind2
269 foreach( size_t n1, newLevel.ind1 ) { // for all n1 in this level
270 foreach( const Neighbor &n2, nb1(n1) ) { // for all neighbors n2 of n1
271 if( find( prevLevel.ind2.begin(), prevLevel.ind2.end(), n2 ) == prevLevel.ind2.end() ) { // n2 not in previous level
272 if( find( newLevel.ind2.begin(), newLevel.ind2.end(), n2 ) != newLevel.ind2.end() )
273 foundCycle = true; // n2 already in new level: we found a cycle
274 else
275 newLevel.ind2.push_back( n2 ); // add n2 to new level
276 }
277 if( foundCycle )
278 break;
279 }
280 if( foundCycle )
281 break;
282 }
283 }
284 levels.push_back( newLevel );
285 nr_1 += newLevel.ind1.size();
286 nr_2 += newLevel.ind2.size();
287 } while( ((newLevel.ind1.size() != 0) || (newLevel.ind2.size() != 0)) && !foundCycle );
288 if( nr_1 == nrNodes1() && nr_2 == nrNodes2() && !foundCycle )
289 return true;
290 else
291 return false;
292 }
293 }
294
295
296 void BipartiteGraph::printDot( std::ostream& os ) const {
297 using namespace std;
298 os << "graph G {" << endl;
299 os << "node[shape=circle,width=0.4,fixedsize=true];" << endl;
300 for( size_t n1 = 0; n1 < nrNodes1(); n1++ )
301 os << "\tx" << n1 << ";" << endl;
302 os << "node[shape=box,width=0.3,height=0.3,fixedsize=true];" << endl;
303 for( size_t n2 = 0; n2 < nrNodes2(); n2++ )
304 os << "\ty" << n2 << ";" << endl;
305 for( size_t n1 = 0; n1 < nrNodes1(); n1++ )
306 foreach( const Neighbor &n2, nb1(n1) )
307 os << "\tx" << n1 << " -- y" << n2 << ";" << endl;
308 os << "}" << endl;
309 }
310
311
312 void BipartiteGraph::checkConsistency() const {
313 size_t N1 = nrNodes1();
314 size_t N2 = nrNodes2();
315 for( size_t n1 = 0; n1 < N1; n1++ ) {
316 size_t iter = 0;
317 foreach( const Neighbor &n2, nb1(n1) ) {
318 DAI_ASSERT( n2.iter == iter );
319 DAI_ASSERT( n2.node < N2 );
320 DAI_ASSERT( n2.dual < nb2(n2).size() );
321 DAI_ASSERT( nb2(n2, n2.dual) == n1 );
322 iter++;
323 }
324 }
325 for( size_t n2 = 0; n2 < N2; n2++ ) {
326 size_t iter = 0;
327 foreach( const Neighbor &n1, nb2(n2) ) {
328 DAI_ASSERT( n1.iter == iter );
329 DAI_ASSERT( n1.node < N1 );
330 DAI_ASSERT( n1.dual < nb1(n1).size() );
331 DAI_ASSERT( nb1(n1, n1.dual) == n2 );
332 iter++;
333 }
334 }
335 }
336
337
338 } // end of namespace dai