degree_centrality.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. //[degree_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/degree_centrality.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. // Declare a container type for degree centralities and its
  28. // corresponding property map.
  29. typedef exterior_vertex_property<Graph, unsigned> CentralityProperty;
  30. typedef CentralityProperty::container_type CentralityContainer;
  31. typedef CentralityProperty::map_type CentralityMap;
  32. int
  33. main(int argc, char *argv[])
  34. {
  35. // Create the graph and a property map that provides access
  36. // to the 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 degree centrality for graph.
  42. CentralityContainer cents(num_vertices(g));
  43. CentralityMap cm(cents, g);
  44. all_degree_centralities(g, cm);
  45. // Print the degree centrality of each vertex.
  46. graph_traits<Graph>::vertex_iterator i, end;
  47. for(boost::tie(i, end) = vertices(g); i != end; ++i) {
  48. cout << setiosflags(ios::left) << setw(12)
  49. << g[*i].name << cm[*i] << endl;
  50. }
  51. return 0;
  52. }
  53. //]