Renamed some functions of BipartiteGraph
[libdai.git] / examples / example_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) 2008-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 */
9
10
11 #include <dai/bipgraph.h>
12
13 using namespace std;
14 using namespace dai;
15
16 int main() {
17 // Create a list of edges
18 vector<BipartiteGraph::Edge> edges;
19 edges.reserve( 5 );
20 edges.push_back( BipartiteGraph::Edge(0, 0) );
21 edges.push_back( BipartiteGraph::Edge(1, 0) );
22 edges.push_back( BipartiteGraph::Edge(2, 0) );
23 edges.push_back( BipartiteGraph::Edge(1, 1) );
24 edges.push_back( BipartiteGraph::Edge(2, 1) );
25
26 // Create a bipartite graph with 3 nodes of type 1,
27 // 2 nodes of type 2 and edge list edges.
28 BipartiteGraph G( 3, 2, edges.begin(), edges.end() );
29
30 // Display some information about G
31 cout << "G has " << G.nrNodes1() << " nodes of type 1, " << G.nrNodes2() << " nodes of type 2 and " << G.nrEdges() << " edges." << endl << endl;
32
33 // Iterate over all nodes n1 of type 1
34 for( size_t n1 = 0; n1 < G.nrNodes1(); n1++ ) {
35 cout << "Node " << n1 << " of type 1 has " << G.nb1(n1).size() << " neighbors:" << endl;
36 // Iterate over all neighbors n2 of n1
37 foreach( const BipartiteGraph::Neighbor &n2, G.nb1(n1) ) {
38 // The n2.iter'th neighbor of n1 is n2:
39 DAI_ASSERT( G.nb1(n1)[n2.iter] == n2 );
40
41 // The n2.dual'th neighbor of n2 is n1:
42 DAI_ASSERT( G.nb2(n2)[n2.dual] == n1 );
43
44 // n2 can be used as an abbreviation of n2.node:
45 DAI_ASSERT( static_cast<size_t>(n2) == n2.node );
46
47 cout << " the " << n2.iter << "'th neighbor is node " << n2 << " of type 2" << endl;
48 }
49 cout << endl;
50 }
51 }