mean_geodesic.cpp 3.0 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. //[mean_geodesic_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/geodesic_distance.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 mean geodesic distances of each
  37. // vertex in the graph.
  38. typedef exterior_vertex_property<Graph, float> GeodesicProperty;
  39. typedef GeodesicProperty::container_type GeodesicContainer;
  40. typedef GeodesicProperty::map_type GeodesicMap;
  41. int
  42. main(int argc, char *argv[])
  43. {
  44. // Create the graph and a property map that provides access
  45. // to the actor names.
  46. Graph g;
  47. NameMap nm(get(&Actor::name, g));
  48. // Read the graph from standad 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 mean geodesic distances for each vertex in
  58. // the graph and get the average mean geodesic distace (the
  59. // so-called small-world distance) as a result.
  60. GeodesicContainer geodesics(num_vertices(g));
  61. GeodesicMap gm(geodesics, g);
  62. float sw = all_mean_geodesics(g, dm, gm);
  63. // Print the mean geodesic distance of each vertex and finally,
  64. // the graph itself.
  65. graph_traits<Graph>::vertex_iterator i, end;
  66. for(boost::tie(i, end) = vertices(g); i != end; ++i) {
  67. cout << setw(12) << setiosflags(ios::left)
  68. << g[*i].name << get(gm, *i) << endl;
  69. }
  70. cout << "small world distance: " << sw << endl;
  71. return 0;
  72. }
  73. //]