closeness_centrality.cpp 2.9 KB

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