d8fdab57098ef5a15be9254457bc346ceafaa287
[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-2010 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.node--;
56 nb1( m1.node, m1.dual ).dual = iter;
57 m1.iter = iter++;
58 } else {
59 // update this entry and the corresponding dual of the neighboring node of type 1
60 nb1( m1.node, m1.dual ).dual = iter;
61 m1.iter = 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.node--;
82 nb2( m2.node, m2.dual ).dual = iter;
83 m2.iter = iter++;
84 } else {
85 // update this entry and the corresponding dual of the neighboring node of type 2
86 nb2( m2.node, m2.dual ).dual = iter;
87 m2.iter = 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 vector<levelType> levels;
230
231 bool foundCycle = false;
232 size_t nr_1 = 0;
233 size_t nr_2 = 0;
234
235 if( nrNodes1() == 0 )
236 return (nrNodes2() < 2 );
237 else if( nrNodes2() == 0 )
238 return (nrNodes1() < 2 );
239 else {
240 levelType newLevel;
241 do {
242 newLevel.ind1.clear();
243 newLevel.ind2.clear();
244 if( levels.size() == 0 ) {
245 size_t n1 = 0;
246 // add n1 to ind1
247 newLevel.ind1 = vector<size_t>( 1, n1 );
248 // add all neighbors of n1 to ind2
249 newLevel.ind2.reserve( nb1(n1).size() );
250 foreach( const Neighbor &n2, nb1(n1) )
251 newLevel.ind2.push_back( n2 );
252 } else {
253 const levelType &prevLevel = levels.back();
254 // build newLevel.ind1
255 foreach( size_t n2, prevLevel.ind2 ) { // for all n2 in the previous level
256 foreach( const Neighbor &n1, nb2(n2) ) { // for all neighbors n1 of n2
257 if( find( prevLevel.ind1.begin(), prevLevel.ind1.end(), n1 ) == prevLevel.ind1.end() ) { // n1 not in previous level
258 if( find( newLevel.ind1.begin(), newLevel.ind1.end(), n1 ) != newLevel.ind1.end() )
259 foundCycle = true; // n1 already in new level: we found a cycle
260 else
261 newLevel.ind1.push_back( n1 ); // add n1 to new level
262 }
263 if( foundCycle )
264 break;
265 }
266 if( foundCycle )
267 break;
268 }
269 // build newLevel.ind2
270 foreach( size_t n1, newLevel.ind1 ) { // for all n1 in this level
271 foreach( const Neighbor &n2, nb1(n1) ) { // for all neighbors n2 of n1
272 if( find( prevLevel.ind2.begin(), prevLevel.ind2.end(), n2 ) == prevLevel.ind2.end() ) { // n2 not in previous level
273 if( find( newLevel.ind2.begin(), newLevel.ind2.end(), n2 ) != newLevel.ind2.end() )
274 foundCycle = true; // n2 already in new level: we found a cycle
275 else
276 newLevel.ind2.push_back( n2 ); // add n2 to new level
277 }
278 if( foundCycle )
279 break;
280 }
281 if( foundCycle )
282 break;
283 }
284 }
285 levels.push_back( newLevel );
286 nr_1 += newLevel.ind1.size();
287 nr_2 += newLevel.ind2.size();
288 } while( ((newLevel.ind1.size() != 0) || (newLevel.ind2.size() != 0)) && !foundCycle );
289 if( nr_1 == nrNodes1() && nr_2 == nrNodes2() && !foundCycle )
290 return true;
291 else
292 return false;
293 }
294 }
295
296
297 void BipartiteGraph::printDot( std::ostream& os ) const {
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