Improved documentation...
[libdai.git] / examples / example_bipgraph.cpp
1 #include <dai/bipgraph.h>
2
3 using namespace std;
4 using namespace dai;
5
6 int main() {
7 // Create a list of edges
8 vector<BipartiteGraph::Edge> edges;
9 edges.reserve( 5 );
10 edges.push_back( BipartiteGraph::Edge(0, 0) );
11 edges.push_back( BipartiteGraph::Edge(1, 0) );
12 edges.push_back( BipartiteGraph::Edge(2, 0) );
13 edges.push_back( BipartiteGraph::Edge(1, 1) );
14 edges.push_back( BipartiteGraph::Edge(2, 1) );
15
16 // Create a bipartite graph with 3 nodes of type 1,
17 // 2 nodes of type 2 and edge list edges.
18 BipartiteGraph G( 3, 2, edges.begin(), edges.end() );
19
20 // Display some information about G
21 cout << "G has " << G.nr1() << " nodes of type 1, " << G.nr2() << " nodes of type 2 and " << G.nrEdges() << " edges." << endl << endl;
22
23 // Iterate over all nodes n1 of type 1
24 for( size_t n1 = 0; n1 < G.nr1(); n1++ ) {
25 cout << "Node " << n1 << " of type 1 has " << G.nb1(n1).size() << " neighbors:" << endl;
26 // Iterate over all neighbors n2 of n1
27 foreach( const BipartiteGraph::Neighbor &n2, G.nb1(n1) ) {
28 // The n2.iter'th neighbor of n1 is n2:
29 assert( G.nb1(n1)[n2.iter] == n2 );
30
31 // The n2.dual'th neighbor of n2 is n1:
32 assert( G.nb2(n2)[n2.dual] == n1 );
33
34 // n2 can be used as an abbreviation of n2.node:
35 assert( static_cast<size_t>(n2) == n2.node );
36
37 cout << " the " << n2.iter << "'th neighbor is node " << n2 << " of type 2" << endl;
38 }
39 cout << endl;
40 }
41 }