0288fe8f676d05d61b0da18e232410197a4ae547
[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 < nr1() );
23 DAI_ASSERT( n2 < nr2() );
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::erase1( size_t n1 ) {
43 DAI_ASSERT( n1 < nr1() );
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 < nr2(); 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::erase2( size_t n2 ) {
69 DAI_ASSERT( n2 < nr2() );
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 < nr1(); 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 < nr1() );
96 DAI_ASSERT( n2 < nr2() );
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 // TODO: use BGL, like:
157 // std::vector<int> component( num_vertices( g ) );
158 // int num_comp = connected_components( g, make_iterator_property_map(component.begin(), get(vertex_index, g)) );
159 if( nr1() == 0 ) {
160 return true;
161 } else {
162 std::vector<bool> incomponent1( nr1(), false );
163 std::vector<bool> incomponent2( nr2(), false );
164
165 incomponent1[0] = true;
166 bool found_new_nodes;
167 do {
168 found_new_nodes = false;
169
170 // For all nodes of type 2, check if they are connected with the (growing) component
171 for( size_t n2 = 0; n2 < nr2(); n2++ )
172 if( !incomponent2[n2] ) {
173 foreach( const Neighbor &n1, nb2(n2) ) {
174 if( incomponent1[n1] ) {
175 found_new_nodes = true;
176 incomponent2[n2] = true;
177 break;
178 }
179 }
180 }
181
182 // For all nodes of type 1, check if they are connected with the (growing) component
183 for( size_t n1 = 0; n1 < nr1(); n1++ )
184 if( !incomponent1[n1] ) {
185 foreach( const Neighbor &n2, nb1(n1) ) {
186 if( incomponent2[n2] ) {
187 found_new_nodes = true;
188 incomponent1[n1] = true;
189 break;
190 }
191 }
192 }
193 } while( found_new_nodes );
194
195 // Check if there are remaining nodes (not in the component)
196 bool all_connected = true;
197 for( size_t n1 = 0; (n1 < nr1()) && all_connected; n1++ )
198 if( !incomponent1[n1] )
199 all_connected = false;
200 for( size_t n2 = 0; (n2 < nr2()) && all_connected; n2++ )
201 if( !incomponent2[n2] )
202 all_connected = false;
203
204 return all_connected;
205 }
206 }
207
208
209 bool BipartiteGraph::isTree() const {
210 using namespace std;
211 vector<levelType> levels;
212
213 bool foundCycle = false;
214 size_t nr_1 = 0;
215 size_t nr_2 = 0;
216
217 if( nr1() == 0 || nr2() == 0 )
218 return true;
219 else {
220 levelType newLevel;
221 do {
222 newLevel.ind1.clear();
223 newLevel.ind2.clear();
224 if( levels.size() == 0 ) {
225 size_t n1 = 0;
226 // add n1 to ind1
227 newLevel.ind1 = vector<size_t>( 1, n1 );
228 // add all neighbors of n1 to ind2
229 newLevel.ind2.reserve( nb1(n1).size() );
230 foreach( const Neighbor &n2, nb1(n1) )
231 newLevel.ind2.push_back( n2 );
232 } else {
233 const levelType &prevLevel = levels.back();
234 // build newLevel.ind1
235 foreach( size_t n2, prevLevel.ind2 ) { // for all n2 in the previous level
236 foreach( const Neighbor &n1, nb2(n2) ) { // for all neighbors n1 of n2
237 if( find( prevLevel.ind1.begin(), prevLevel.ind1.end(), n1 ) == prevLevel.ind1.end() ) { // n1 not in previous level
238 if( find( newLevel.ind1.begin(), newLevel.ind1.end(), n1 ) != newLevel.ind1.end() )
239 foundCycle = true; // n1 already in new level: we found a cycle
240 else
241 newLevel.ind1.push_back( n1 ); // add n1 to new level
242 }
243 if( foundCycle )
244 break;
245 }
246 if( foundCycle )
247 break;
248 }
249 // build newLevel.ind2
250 foreach( size_t n1, newLevel.ind1 ) { // for all n1 in this level
251 foreach( const Neighbor &n2, nb1(n1) ) { // for all neighbors n2 of n1
252 if( find( prevLevel.ind2.begin(), prevLevel.ind2.end(), n2 ) == prevLevel.ind2.end() ) { // n2 not in previous level
253 if( find( newLevel.ind2.begin(), newLevel.ind2.end(), n2 ) != newLevel.ind2.end() )
254 foundCycle = true; // n2 already in new level: we found a cycle
255 else
256 newLevel.ind2.push_back( n2 ); // add n2 to new level
257 }
258 if( foundCycle )
259 break;
260 }
261 if( foundCycle )
262 break;
263 }
264 }
265 levels.push_back( newLevel );
266 nr_1 += newLevel.ind1.size();
267 nr_2 += newLevel.ind2.size();
268 } while( ((newLevel.ind1.size() != 0) || (newLevel.ind2.size() != 0)) && !foundCycle );
269 if( nr_1 == nr1() && nr_2 == nr2() && !foundCycle )
270 return true;
271 else
272 return false;
273 }
274 }
275
276
277 void BipartiteGraph::printDot( std::ostream& os ) const {
278 using namespace std;
279 os << "graph G {" << endl;
280 os << "node[shape=circle,width=0.4,fixedsize=true];" << endl;
281 for( size_t n1 = 0; n1 < nr1(); n1++ )
282 os << "\tx" << n1 << ";" << endl;
283 os << "node[shape=box,width=0.3,height=0.3,fixedsize=true];" << endl;
284 for( size_t n2 = 0; n2 < nr2(); n2++ )
285 os << "\ty" << n2 << ";" << endl;
286 for( size_t n1 = 0; n1 < nr1(); n1++ )
287 foreach( const Neighbor &n2, nb1(n1) )
288 os << "\tx" << n1 << " -- y" << n2 << ";" << endl;
289 os << "}" << endl;
290 }
291
292
293 void BipartiteGraph::checkConsistency() const {
294 size_t N1 = nr1();
295 size_t N2 = nr2();
296 for( size_t n1 = 0; n1 < N1; n1++ ) {
297 size_t iter = 0;
298 foreach( const Neighbor &n2, nb1(n1) ) {
299 DAI_ASSERT( n2.iter == iter );
300 DAI_ASSERT( n2.node < N2 );
301 DAI_ASSERT( n2.dual < nb2(n2).size() );
302 DAI_ASSERT( nb2(n2, n2.dual) == n1 );
303 iter++;
304 }
305 }
306 for( size_t n2 = 0; n2 < N2; n2++ ) {
307 size_t iter = 0;
308 foreach( const Neighbor &n1, nb2(n2) ) {
309 DAI_ASSERT( n1.iter == iter );
310 DAI_ASSERT( n1.node < N1 );
311 DAI_ASSERT( n1.dual < nb1(n1).size() );
312 DAI_ASSERT( nb1(n1, n1.dual) == n2 );
313 iter++;
314 }
315 }
316 }
317
318
319 } // end of namespace dai