eccentricity.cpp 2.9 KB

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