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