Added utils/uai2fg, ExactInf::findMaximum(), and fixed two bugs
[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 SmallSet<size_t> BipartiteGraph::delta1( size_t n1, bool include ) const {
128 // get all second-order neighbors
129 SmallSet<size_t> result;
130 foreach( const Neighbor &n2, nb1(n1) )
131 foreach( const Neighbor &m1, nb2(n2) )
132 if( include || (m1 != n1) )
133 result |= m1;
134 return result;
135 }
136
137
138 SmallSet<size_t> BipartiteGraph::delta2( size_t n2, bool include ) const {
139 // store all second-order neighbors
140 SmallSet<size_t> result;
141 foreach( const Neighbor &n1, nb2(n2) )
142 foreach( const Neighbor &m2, nb1(n1) )
143 if( include || (m2 != n2) )
144 result |= m2;
145 return result;
146 }
147
148
149 bool BipartiteGraph::isConnected() const {
150 if( nrNodes1() == 0 ) {
151 return true;
152 } else {
153 /*
154 // The BGL implementation is significantly slower...
155 using namespace boost;
156 typedef adjacency_list< vecS, vecS, undirectedS, property<vertex_distance_t, int> > boostGraph;
157 typedef pair<size_t, size_t> E;
158
159 // Copy graph structure into boostGraph object
160 size_t N = nrNodes1();
161 vector<E> edges;
162 edges.reserve( nrEdges() );
163 for( size_t n1 = 0; n1 < nrNodes1(); n1++ )
164 foreach( const Neighbor &n2, nb1(n1) )
165 edges.push_back( E( n1, n2.node + N ) );
166 boostGraph g( edges.begin(), edges.end(), nrNodes1() + nrNodes2() );
167
168 // Construct connected components using Boost Graph Library
169 std::vector<int> component( num_vertices( g ) );
170 int num_comp = connected_components( g, make_iterator_property_map(component.begin(), get(vertex_index, g)) );
171
172 return (num_comp == 1);
173 */
174
175 std::vector<bool> incomponent1( nrNodes1(), false );
176 std::vector<bool> incomponent2( nrNodes2(), false );
177
178 incomponent1[0] = true;
179 bool found_new_nodes;
180 do {
181 found_new_nodes = false;
182
183 // For all nodes of type 2, check if they are connected with the (growing) component
184 for( size_t n2 = 0; n2 < nrNodes2(); n2++ )
185 if( !incomponent2[n2] ) {
186 foreach( const Neighbor &n1, nb2(n2) ) {
187 if( incomponent1[n1] ) {
188 found_new_nodes = true;
189 incomponent2[n2] = true;
190 break;
191 }
192 }
193 }
194
195 // For all nodes of type 1, check if they are connected with the (growing) component
196 for( size_t n1 = 0; n1 < nrNodes1(); n1++ )
197 if( !incomponent1[n1] ) {
198 foreach( const Neighbor &n2, nb1(n1) ) {
199 if( incomponent2[n2] ) {
200 found_new_nodes = true;
201 incomponent1[n1] = true;
202 break;
203 }
204 }
205 }
206 } while( found_new_nodes );
207
208 // Check if there are remaining nodes (not in the component)
209 bool all_connected = true;
210 for( size_t n1 = 0; (n1 < nrNodes1()) && all_connected; n1++ )
211 if( !incomponent1[n1] )
212 all_connected = false;
213 for( size_t n2 = 0; (n2 < nrNodes2()) && all_connected; n2++ )
214 if( !incomponent2[n2] )
215 all_connected = false;
216
217 return all_connected;
218 }
219 }
220
221
222 bool BipartiteGraph::isTree() const {
223 vector<levelType> levels;
224
225 bool foundCycle = false;
226 size_t nr_1 = 0;
227 size_t nr_2 = 0;
228
229 if( nrNodes1() == 0 )
230 return (nrNodes2() < 2 );
231 else if( nrNodes2() == 0 )
232 return (nrNodes1() < 2 );
233 else {
234 levelType newLevel;
235 do {
236 newLevel.ind1.clear();
237 newLevel.ind2.clear();
238 if( levels.size() == 0 ) {
239 size_t n1 = 0;
240 // add n1 to ind1
241 newLevel.ind1 = vector<size_t>( 1, n1 );
242 // add all neighbors of n1 to ind2
243 newLevel.ind2.reserve( nb1(n1).size() );
244 foreach( const Neighbor &n2, nb1(n1) )
245 newLevel.ind2.push_back( n2 );
246 } else {
247 const levelType &prevLevel = levels.back();
248 // build newLevel.ind1
249 foreach( size_t n2, prevLevel.ind2 ) { // for all n2 in the previous level
250 foreach( const Neighbor &n1, nb2(n2) ) { // for all neighbors n1 of n2
251 if( find( prevLevel.ind1.begin(), prevLevel.ind1.end(), n1 ) == prevLevel.ind1.end() ) { // n1 not in previous level
252 if( find( newLevel.ind1.begin(), newLevel.ind1.end(), n1 ) != newLevel.ind1.end() )
253 foundCycle = true; // n1 already in new level: we found a cycle
254 else
255 newLevel.ind1.push_back( n1 ); // add n1 to new level
256 }
257 if( foundCycle )
258 break;
259 }
260 if( foundCycle )
261 break;
262 }
263 // build newLevel.ind2
264 foreach( size_t n1, newLevel.ind1 ) { // for all n1 in this level
265 foreach( const Neighbor &n2, nb1(n1) ) { // for all neighbors n2 of n1
266 if( find( prevLevel.ind2.begin(), prevLevel.ind2.end(), n2 ) == prevLevel.ind2.end() ) { // n2 not in previous level
267 if( find( newLevel.ind2.begin(), newLevel.ind2.end(), n2 ) != newLevel.ind2.end() )
268 foundCycle = true; // n2 already in new level: we found a cycle
269 else
270 newLevel.ind2.push_back( n2 ); // add n2 to new level
271 }
272 if( foundCycle )
273 break;
274 }
275 if( foundCycle )
276 break;
277 }
278 }
279 levels.push_back( newLevel );
280 nr_1 += newLevel.ind1.size();
281 nr_2 += newLevel.ind2.size();
282 } while( ((newLevel.ind1.size() != 0) || (newLevel.ind2.size() != 0)) && !foundCycle );
283 if( nr_1 == nrNodes1() && nr_2 == nrNodes2() && !foundCycle )
284 return true;
285 else
286 return false;
287 }
288 }
289
290
291 void BipartiteGraph::printDot( std::ostream& os ) const {
292 os << "graph BipartiteGraph {" << endl;
293 os << "node[shape=circle,width=0.4,fixedsize=true];" << endl;
294 for( size_t n1 = 0; n1 < nrNodes1(); n1++ )
295 os << "\tx" << n1 << ";" << endl;
296 os << "node[shape=box,width=0.3,height=0.3,fixedsize=true];" << endl;
297 for( size_t n2 = 0; n2 < nrNodes2(); n2++ )
298 os << "\ty" << n2 << ";" << endl;
299 for( size_t n1 = 0; n1 < nrNodes1(); n1++ )
300 foreach( const Neighbor &n2, nb1(n1) )
301 os << "\tx" << n1 << " -- y" << n2 << ";" << endl;
302 os << "}" << endl;
303 }
304
305
306 void BipartiteGraph::checkConsistency() const {
307 size_t N1 = nrNodes1();
308 size_t N2 = nrNodes2();
309 for( size_t n1 = 0; n1 < N1; n1++ ) {
310 size_t iter = 0;
311 foreach( const Neighbor &n2, nb1(n1) ) {
312 DAI_ASSERT( n2.iter == iter );
313 DAI_ASSERT( n2.node < N2 );
314 DAI_ASSERT( n2.dual < nb2(n2).size() );
315 DAI_ASSERT( nb2(n2, n2.dual) == n1 );
316 iter++;
317 }
318 }
319 for( size_t n2 = 0; n2 < N2; n2++ ) {
320 size_t iter = 0;
321 foreach( const Neighbor &n1, nb2(n2) ) {
322 DAI_ASSERT( n1.iter == iter );
323 DAI_ASSERT( n1.node < N1 );
324 DAI_ASSERT( n1.dual < nb1(n1).size() );
325 DAI_ASSERT( nb1(n1, n1.dual) == n2 );
326 iter++;
327 }
328 }
329 }
330
331
332 } // end of namespace dai