[Benjamin Piwowarski] Renamed "foreach" macro into "bforeach" to avoid conflicts...
[libdai.git] / examples / example_bipgraph.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <dai/bipgraph.h>
10
11 using namespace std;
12 using namespace dai;
13
14 int main() {
15 // Create a list of edges
16 vector<Edge> edges;
17 edges.reserve( 5 );
18 edges.push_back( Edge(0, 0) );
19 edges.push_back( Edge(1, 0) );
20 edges.push_back( Edge(2, 0) );
21 edges.push_back( Edge(1, 1) );
22 edges.push_back( Edge(2, 1) );
23
24 // Create a bipartite graph with 3 nodes of type 1,
25 // 2 nodes of type 2 and edge list edges.
26 BipartiteGraph G( 3, 2, edges.begin(), edges.end() );
27
28 // Display some information about G
29 cout << "G has " << G.nrNodes1() << " nodes of type 1, " << G.nrNodes2() << " nodes of type 2 and " << G.nrEdges() << " edges." << endl << endl;
30
31 // Iterate over all nodes n1 of type 1
32 for( size_t n1 = 0; n1 < G.nrNodes1(); n1++ ) {
33 cout << "Node " << n1 << " of type 1 has " << G.nb1(n1).size() << " neighbors:" << endl;
34 // Iterate over all neighbors n2 of n1
35 bforeach( const Neighbor &n2, G.nb1(n1) ) {
36 // The n2.iter'th neighbor of n1 is n2:
37 DAI_ASSERT( G.nb1(n1)[n2.iter] == n2 );
38
39 // The n2.dual'th neighbor of n2 is n1:
40 DAI_ASSERT( G.nb2(n2)[n2.dual] == n1 );
41
42 // n2 can be used as an abbreviation of n2.node:
43 DAI_ASSERT( static_cast<size_t>(n2) == n2.node );
44
45 cout << " the " << n2.iter << "'th neighbor is node " << n2 << " of type 2" << endl;
46 }
47 cout << endl;
48 }
49 }