1// (C) Copyright Andrew Sutton 2007
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//[scaled_closeness_centrality_example
8#include <iostream>
9#include <iomanip>
10
11#include <boost/graph/undirected_graph.hpp>
12#include <boost/graph/exterior_property.hpp>
13#include <boost/graph/floyd_warshall_shortest.hpp>
14#include <boost/graph/closeness_centrality.hpp>
15#include "helper.hpp"
16
17using namespace std;
18using namespace boost;
19
20// This template struct provides a generic version of a "scaling"
21// closeness measure. Specifically, this implementation divides
22// the number of vertices in the graph by the sum of geodesic
23// distances of each vertex. This measure allows customization
24// of the distance type, result type, and even the underlying
25// divide operation.
26template < typename Graph, typename Distance, typename Result,
27 typename Divide = divides< Result > >
28struct scaled_closeness_measure
29{
30 typedef Distance distance_type;
31 typedef Result result_type;
32
33 Result operator()(Distance d, const Graph& g)
34 {
35 if (d == numeric_values< Distance >::infinity())
36 {
37 return numeric_values< Result >::zero();
38 }
39 else
40 {
41 return div(Result(num_vertices(g)), Result(d));
42 }
43 }
44 Divide div;
45};
46
47// The Actor type stores the name of each vertex in the graph.
48struct Actor
49{
50 std::string name;
51};
52
53// Declare the graph type and its vertex and edge types.
54typedef undirected_graph< Actor > Graph;
55typedef graph_traits< Graph >::vertex_descriptor Vertex;
56typedef graph_traits< Graph >::edge_descriptor Edge;
57
58// The name map provides an abstract accessor for the names of
59// each vertex. This is used during graph creation.
60typedef property_map< Graph, string Actor::* >::type NameMap;
61
62// Declare a matrix type and its corresponding property map that
63// will contain the distances between each pair of vertices.
64typedef exterior_vertex_property< Graph, int > DistanceProperty;
65typedef DistanceProperty::matrix_type DistanceMatrix;
66typedef DistanceProperty::matrix_map_type DistanceMatrixMap;
67
68// Declare the weight map so that each edge returns the same value.
69typedef constant_property_map< Edge, int > WeightMap;
70
71// Declare a container and its corresponding property map that
72// will contain the resulting closeness centralities of each
73// vertex in the graph.
74typedef boost::exterior_vertex_property< Graph, float > ClosenessProperty;
75typedef ClosenessProperty::container_type ClosenessContainer;
76typedef ClosenessProperty::map_type ClosenessMap;
77
78int main(int argc, char* argv[])
79{
80 // Create the graph and a property map that provides access
81 // to the actor names.
82 Graph g;
83 NameMap nm(get(p: &Actor::name, g));
84
85 // Read the graph from standard input.
86 read_graph(g, nm, is&: cin);
87
88 // Compute the distances between all pairs of vertices using
89 // the Floyd-Warshall algorithm. Note that the weight map is
90 // created so that every edge has a weight of 1.
91 DistanceMatrix distances(num_vertices(g));
92 DistanceMatrixMap dm(distances, g);
93 WeightMap wm(1);
94 floyd_warshall_all_pairs_shortest_paths(g, d&: dm, params: weight_map(p: wm));
95
96 // Create the scaled closeness measure.
97 scaled_closeness_measure< Graph, int, float > m;
98
99 // Compute the degree centrality for graph
100 ClosenessContainer cents(num_vertices(g));
101 ClosenessMap cm(cents, g);
102 all_closeness_centralities(g, dist: dm, cent: cm, measure: m);
103
104 // Print the scaled closeness centrality of each vertex.
105 graph_traits< Graph >::vertex_iterator i, end;
106 for (boost::tie(t0&: i, t1&: end) = vertices(g); i != end; ++i)
107 {
108 cout << setw(12) << setiosflags(ios::left) << g[*i].name << get(pa: cm, k: *i)
109 << endl;
110 }
111
112 return 0;
113}
114//]
115

source code of boost/libs/graph/example/scaled_closeness_centrality.cpp