clustering_coefficient.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. //[code_clustering_coefficient
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <boost/graph/undirected_graph.hpp>
  10. #include <boost/graph/exterior_property.hpp>
  11. #include <boost/graph/clustering_coefficient.hpp>
  12. #include "helper.hpp"
  13. using namespace std;
  14. using namespace boost;
  15. // The Actor type stores the name of each vertex in the graph.
  16. struct Actor
  17. {
  18. string name;
  19. };
  20. // Declare the graph type and its vertex and edge types.
  21. typedef undirected_graph<Actor> Graph;
  22. typedef graph_traits<Graph>::vertex_descriptor Vertex;
  23. typedef graph_traits<Graph>::edge_descriptor Edge;
  24. // The name map provides an abstract accessor for the names of
  25. // each vertex. This is used during graph creation.
  26. typedef property_map<Graph, string Actor::*>::type NameMap;
  27. // The clustering property, container, and map define the containment
  28. // and abstract accessor for the clustering coefficients of vertices.
  29. typedef exterior_vertex_property<Graph, float> ClusteringProperty;
  30. typedef ClusteringProperty::container_type ClusteringContainer;
  31. typedef ClusteringProperty::map_type ClusteringMap;
  32. int
  33. main(int argc, char *argv[])
  34. {
  35. // Create the graph and a name map that provides access to
  36. // then actor names.
  37. Graph g;
  38. NameMap nm(get(&Actor::name, g));
  39. // Read the graph from standard input.
  40. read_graph(g, nm, cin);
  41. // Compute the clustering coefficients of each vertex in the graph
  42. // and the mean clustering coefficient which is returned from the
  43. // computation.
  44. ClusteringContainer coefs(num_vertices(g));
  45. ClusteringMap cm(coefs, g);
  46. float cc = all_clustering_coefficients(g, cm);
  47. // Print the clustering coefficient of each vertex.
  48. graph_traits<Graph>::vertex_iterator i, end;
  49. for(boost::tie(i, end) = vertices(g); i != end; ++i) {
  50. cout << setw(12) << setiosflags(ios::left)
  51. << g[*i].name << get(cm, *i) << endl;
  52. }
  53. cout << "mean clustering coefficient: " << cc << endl;
  54. return 0;
  55. }
  56. //]