scaled_closeness_centrality.cpp 3.7 KB

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