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 <iostream>
8 #include <vector>
9
10 #include <boost/graph/undirected_graph.hpp>
11 #include <boost/graph/directed_graph.hpp>
12 #include <boost/graph/floyd_warshall_shortest.hpp>
13 #include <boost/graph/closeness_centrality.hpp>
14 #include <boost/graph/exterior_property.hpp>
15 #include <boost/graph/property_maps/constant_property_map.hpp>
16
17 using namespace std;
18 using namespace boost;
19
20 // number of vertices in the graph
21 static const unsigned N = 5;
22
23 template <typename Graph>
24 struct vertex_vector
25 {
26 typedef graph_traits<Graph> traits;
27 typedef vector<typename traits::vertex_descriptor> type;
28 };
29
30 template <typename Graph>
build_graph(Graph & g,typename vertex_vector<Graph>::type & v)31 void build_graph(Graph& g,
32 typename vertex_vector<Graph>::type& v)
33 {
34 // add vertices
35 for(size_t i = 0; i < N; ++i) {
36 v[i] = add_vertex(g);
37 }
38
39 // add edges
40 add_edge(v[0], v[1], g);
41 add_edge(v[1], v[2], g);
42 add_edge(v[2], v[0], g);
43 add_edge(v[3], v[4], g);
44 add_edge(v[4], v[0], g);
45 }
46
47
48 template <typename Graph>
test_undirected()49 void test_undirected()
50 {
51 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
52 typedef typename graph_traits<Graph>::edge_descriptor Edge;
53
54 typedef exterior_vertex_property<Graph, double> CentralityProperty;
55 typedef typename CentralityProperty::container_type CentralityContainer;
56 typedef typename CentralityProperty::map_type CentralityMap;
57
58 typedef exterior_vertex_property<Graph, int> DistanceProperty;
59 typedef typename DistanceProperty::matrix_type DistanceMatrix;
60 typedef typename DistanceProperty::matrix_map_type DistanceMatrixMap;
61
62 typedef constant_property_map<Edge, int> WeightMap;
63
64 Graph g;
65 vector<Vertex> v(N);
66 build_graph(g, v);
67
68 CentralityContainer centralities(num_vertices(g));
69 DistanceMatrix distances(num_vertices(g));
70
71 CentralityMap cm(centralities, g);
72 DistanceMatrixMap dm(distances, g);
73
74 WeightMap wm(1);
75
76 floyd_warshall_all_pairs_shortest_paths(g, dm, weight_map(wm));
77 all_closeness_centralities(g, dm, cm);
78
79 BOOST_ASSERT(cm[v[0]] == double(1)/5);
80 BOOST_ASSERT(cm[v[1]] == double(1)/7);
81 BOOST_ASSERT(cm[v[2]] == double(1)/7);
82 BOOST_ASSERT(cm[v[3]] == double(1)/9);
83 BOOST_ASSERT(cm[v[4]] == double(1)/6);
84 }
85
86 template <typename Graph>
test_directed()87 void test_directed()
88 {
89 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
90 typedef typename graph_traits<Graph>::edge_descriptor Edge;
91
92 typedef exterior_vertex_property<Graph, double> CentralityProperty;
93 typedef typename CentralityProperty::container_type CentralityContainer;
94 typedef typename CentralityProperty::map_type CentralityMap;
95
96 typedef exterior_vertex_property<Graph, int> DistanceProperty;
97 typedef typename DistanceProperty::matrix_type DistanceMatrix;
98 typedef typename DistanceProperty::matrix_map_type DistanceMatrixMap;
99
100 typedef constant_property_map<Edge, int> WeightMap;
101
102 Graph g;
103 vector<Vertex> v(N);
104 build_graph(g, v);
105
106 CentralityContainer centralities(num_vertices(g));
107 DistanceMatrix distances(num_vertices(g));
108
109 CentralityMap cm(centralities, g);
110 DistanceMatrixMap dm(distances, g);
111
112 WeightMap wm(1);
113
114 floyd_warshall_all_pairs_shortest_paths(g, dm, weight_map(wm));
115 all_closeness_centralities(g, dm, cm);
116
117 BOOST_ASSERT(cm[v[0]] == double(0));
118 BOOST_ASSERT(cm[v[1]] == double(0));
119 BOOST_ASSERT(cm[v[2]] == double(0));
120 BOOST_ASSERT(cm[v[3]] == double(1)/10);
121 BOOST_ASSERT(cm[v[4]] == double(0));
122 }
123
124 int
main(int,char * [])125 main(int, char *[])
126 {
127 typedef undirected_graph<> Graph;
128 typedef directed_graph<> Digraph;
129
130 test_undirected<Graph>();
131 test_directed<Digraph>();
132 }
133