example_bipgraph.cpp
This example deals with the following bipartite graph:
It has three nodes of type 1 (drawn as circles) and two nodes of type 2 (drawn as rectangles). Node 0 of type 1 has only one neighbor (node 0 of type 2), but node 0 of type 2 has three neighbors (nodes 0,1,2 of type 1). The example code shows how to construct a BipartiteGraph object representing this bipartite graph and how to iterate over nodes and their neighbors.
G has 3 nodes of type 1, 2 nodes of type 2 and 5 edges.
Node 0 of type 1 has 1 neighbors:
the 0'th neighbor is node 0 of type 2
Node 1 of type 1 has 2 neighbors:
the 0'th neighbor is node 0 of type 2
the 1'th neighbor is node 1 of type 2
Node 2 of type 1 has 2 neighbors:
the 0'th neighbor is node 0 of type 2
the 1'th neighbor is node 1 of type 2
#include <dai/bipgraph.h>
using namespace std;
using namespace dai;
int main() {
vector<Edge> edges;
edges.reserve( 5 );
edges.push_back( Edge(0, 0) );
edges.push_back( Edge(1, 0) );
edges.push_back( Edge(2, 0) );
edges.push_back( Edge(1, 1) );
edges.push_back( Edge(2, 1) );
BipartiteGraph G( 3, 2, edges.begin(), edges.end() );
cout << "G has " << G.nrNodes1() << " nodes of type 1, " << G.nrNodes2() << " nodes of type 2 and " << G.nrEdges() << " edges." << endl << endl;
for( size_t n1 = 0; n1 < G.nrNodes1(); n1++ ) {
cout << "Node " << n1 << " of type 1 has " << G.nb1(n1).size() << " neighbors:" << endl;
foreach( const Neighbor &n2, G.nb1(n1) ) {
DAI_ASSERT( G.nb1(n1)[n2.iter] == n2 );
DAI_ASSERT( G.nb2(n2)[n2.dual] == n1 );
DAI_ASSERT( static_cast<size_t>(n2) == n2.node );
cout << " the " << n2.iter << "'th neighbor is node " << n2 << " of type 2" << endl;
}
cout << endl;
}
}