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//[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 <boost/graph/property_maps/constant_property_map.hpp>
16#include "helper.hpp"
17
18using namespace std;
19using namespace boost;
20
21// The Actor type stores the name of each vertex in the graph.
22struct Actor
23{
24 string name;
25};
26
27// Declare the graph type and its vertex and edge types.
28typedef undirected_graph< Actor > Graph;
29typedef graph_traits< Graph >::vertex_descriptor Vertex;
30typedef graph_traits< Graph >::edge_descriptor Edge;
31
32// The name map provides an abstract accessor for the names of
33// each vertex. This is used during graph creation.
34typedef property_map< Graph, string Actor::* >::type NameMap;
35
36// Declare a matrix type and its corresponding property map that
37// will contain the distances between each pair of vertices.
38typedef exterior_vertex_property< Graph, int > DistanceProperty;
39typedef DistanceProperty::matrix_type DistanceMatrix;
40typedef DistanceProperty::matrix_map_type DistanceMatrixMap;
41
42// Declare the weight map so that each edge returns the same value.
43typedef constant_property_map< Edge, int > WeightMap;
44
45// Declare a container and its corresponding property map that
46// will contain the resulting closeness centralities of each
47// vertex in the graph.
48typedef boost::exterior_vertex_property< Graph, float > ClosenessProperty;
49typedef ClosenessProperty::container_type ClosenessContainer;
50typedef ClosenessProperty::map_type ClosenessMap;
51
52int main(int argc, char* argv[])
53{
54 // Create the graph and a property map that provides access to[
55 // tha actor names.
56 Graph g;
57 NameMap nm(get(p: &Actor::name, g));
58
59 // Read the graph from standard input.
60 read_graph(g, nm, is&: cin);
61
62 // Compute the distances between all pairs of vertices using
63 // the Floyd-Warshall algorithm. Note that the weight map is
64 // created so that every edge has a weight of 1.
65 DistanceMatrix distances(num_vertices(g));
66 DistanceMatrixMap dm(distances, g);
67 WeightMap wm(1);
68 floyd_warshall_all_pairs_shortest_paths(g, d&: dm, params: weight_map(p: wm));
69
70 // Compute the closeness centrality for graph.
71 ClosenessContainer cents(num_vertices(g));
72 ClosenessMap cm(cents, g);
73 all_closeness_centralities(g, dist: dm, cent: cm);
74
75 // Print the closeness centrality of each vertex.
76 graph_traits< Graph >::vertex_iterator i, end;
77 for (boost::tie(t0&: i, t1&: end) = vertices(g); i != end; ++i)
78 {
79 cout << setw(12) << setiosflags(ios::left) << g[*i].name << get(pa: cm, k: *i)
80 << endl;
81 }
82
83 return 0;
84}
85//]
86

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