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