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
7 //[degree_centrality_example
8 #include <iostream>
9 #include <iomanip>
10
11 #include <boost/graph/undirected_graph.hpp>
12 #include <boost/graph/exterior_property.hpp>
13 #include <boost/graph/degree_centrality.hpp>
14
15 #include "helper.hpp"
16
17 using namespace std;
18 using namespace boost;
19
20 // The Actor type stores the name of each vertex in the graph.
21 struct Actor
22 {
23 string name;
24 };
25
26 // Declare the graph type and its vertex and edge types.
27 typedef undirected_graph< Actor > Graph;
28 typedef graph_traits< Graph >::vertex_descriptor Vertex;
29 typedef graph_traits< Graph >::edge_descriptor Edge;
30
31 // The name map provides an abstract accessor for the names of
32 // each vertex. This is used during graph creation.
33 typedef property_map< Graph, string Actor::* >::type NameMap;
34
35 // Declare a container type for degree centralities and its
36 // corresponding property map.
37 typedef exterior_vertex_property< Graph, unsigned > CentralityProperty;
38 typedef CentralityProperty::container_type CentralityContainer;
39 typedef CentralityProperty::map_type CentralityMap;
40
main(int argc,char * argv[])41 int main(int argc, char* argv[])
42 {
43 // Create the graph and a property map that provides access
44 // to the actor names.
45 Graph g;
46 NameMap nm(get(&Actor::name, g));
47
48 // Read the graph from standard input.
49 read_graph(g, nm, cin);
50
51 // Compute the degree centrality for graph.
52 CentralityContainer cents(num_vertices(g));
53 CentralityMap cm(cents, g);
54 all_degree_centralities(g, cm);
55
56 // Print the degree centrality of each vertex.
57 graph_traits< Graph >::vertex_iterator i, end;
58 for (boost::tie(i, end) = vertices(g); i != end; ++i)
59 {
60 cout << setiosflags(ios::left) << setw(12) << g[*i].name << cm[*i]
61 << endl;
62 }
63
64 return 0;
65 }
66 //]
67