1// (C) Copyright 2007-2009 Andrew Sutton
2//
3// Use, modification and distribution are subject to the
4// Boost Software License, Version 1.0 (See accompanying file
5// LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
6
7#include <vector>
8
9#include <boost/graph/undirected_graph.hpp>
10#include <boost/graph/directed_graph.hpp>
11#include <boost/graph/degree_centrality.hpp>
12#include <boost/graph/exterior_property.hpp>
13
14using namespace std;
15using namespace boost;
16
17// useful types
18// number of vertices in the graph
19static const unsigned N = 5;
20
21template < typename Graph >
22void build_graph(
23 Graph& g, vector< typename graph_traits< Graph >::vertex_descriptor >& v)
24{
25 // add vertices
26 for (size_t i = 0; i < N; ++i)
27 {
28 v[i] = add_vertex(g);
29 }
30
31 // add edges
32 add_edge(v[0], v[1], g);
33 add_edge(v[1], v[2], g);
34 add_edge(v[2], v[0], g);
35 add_edge(v[3], v[4], g);
36 add_edge(v[4], v[0], g);
37}
38
39template < typename Graph > void test_undirected()
40{
41 typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
42
43 typedef exterior_vertex_property< Graph, unsigned > CentralityProperty;
44 typedef typename CentralityProperty::container_type CentralityContainer;
45 typedef typename CentralityProperty::map_type CentralityMap;
46
47 Graph g;
48 vector< Vertex > v(N);
49 build_graph(g, v);
50
51 CentralityContainer cents(num_vertices(g));
52 CentralityMap cm(cents, g);
53 all_degree_centralities(g, cm);
54
55 BOOST_ASSERT(cm[v[0]] == 3);
56 BOOST_ASSERT(cm[v[1]] == 2);
57 BOOST_ASSERT(cm[v[2]] == 2);
58 BOOST_ASSERT(cm[v[3]] == 1);
59 BOOST_ASSERT(cm[v[4]] == 2);
60}
61
62template < typename Graph > void test_influence()
63{
64 typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
65
66 typedef exterior_vertex_property< Graph, unsigned > CentralityProperty;
67 typedef typename CentralityProperty::container_type CentralityContainer;
68 typedef typename CentralityProperty::map_type CentralityMap;
69
70 Graph g;
71
72 vector< Vertex > v(N);
73 build_graph(g, v);
74
75 CentralityContainer cents(num_vertices(g));
76 CentralityMap cm(cents, g);
77 all_influence_values(g, cm);
78
79 BOOST_ASSERT(cm[v[0]] == 1);
80 BOOST_ASSERT(cm[v[1]] == 1);
81 BOOST_ASSERT(cm[v[2]] == 1);
82 BOOST_ASSERT(cm[v[3]] == 1);
83 BOOST_ASSERT(cm[v[4]] == 1);
84}
85
86template < typename Graph > void test_prestige()
87{
88 typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
89
90 typedef exterior_vertex_property< Graph, unsigned > CentralityProperty;
91 typedef typename CentralityProperty::container_type CentralityContainer;
92 typedef typename CentralityProperty::map_type CentralityMap;
93
94 Graph g;
95
96 vector< Vertex > v(N);
97 build_graph(g, v);
98
99 CentralityContainer cents(num_vertices(g));
100 CentralityMap cm(cents, g);
101 all_prestige_values(g, cm);
102
103 BOOST_ASSERT(cm[v[0]] == 2);
104 BOOST_ASSERT(cm[v[1]] == 1);
105 BOOST_ASSERT(cm[v[2]] == 1);
106 BOOST_ASSERT(cm[v[3]] == 0);
107 BOOST_ASSERT(cm[v[4]] == 1);
108}
109
110int main(int, char*[])
111{
112 typedef undirected_graph<> Graph;
113 typedef directed_graph<> Digraph;
114
115 test_undirected< Graph >();
116 test_influence< Digraph >();
117 test_prestige< Digraph >();
118}
119

source code of boost/libs/graph/test/degree_centrality.cpp