• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //[eccentricity_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/floyd_warshall_shortest.hpp>
14 #include <boost/graph/eccentricity.hpp>
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 matrix type and its corresponding property map that
36 // will contain the distances between each pair of vertices.
37 typedef exterior_vertex_property< Graph, int > DistanceProperty;
38 typedef DistanceProperty::matrix_type DistanceMatrix;
39 typedef DistanceProperty::matrix_map_type DistanceMatrixMap;
40 
41 // Declare the weight map so that each edge returns the same value.
42 typedef constant_property_map< Edge, int > WeightMap;
43 
44 // Declare a container and its corresponding property map that
45 // will contain the resulting eccentricities of each vertex in
46 // the graph.
47 typedef boost::exterior_vertex_property< Graph, int > EccentricityProperty;
48 typedef EccentricityProperty::container_type EccentricityContainer;
49 typedef EccentricityProperty::map_type EccentricityMap;
50 
main(int argc,char * argv[])51 int main(int argc, char* argv[])
52 {
53     // Create the graph and a name map that provides access to
54     // then actor names.
55     Graph g;
56     NameMap nm(get(&Actor::name, g));
57 
58     // Read the graph from standard input.
59     read_graph(g, nm, cin);
60 
61     // Compute the distances between all pairs of vertices using
62     // the Floyd-Warshall algorithm. Note that the weight map is
63     // created so that every edge has a weight of 1.
64     DistanceMatrix distances(num_vertices(g));
65     DistanceMatrixMap dm(distances, g);
66     WeightMap wm(1);
67     floyd_warshall_all_pairs_shortest_paths(g, dm, weight_map(wm));
68 
69     // Compute the eccentricities for graph - this computation returns
70     // both the radius and diameter as well.
71     int r, d;
72     EccentricityContainer eccs(num_vertices(g));
73     EccentricityMap em(eccs, g);
74     boost::tie(r, d) = all_eccentricities(g, dm, em);
75 
76     // Print the closeness centrality of each vertex.
77     graph_traits< Graph >::vertex_iterator i, end;
78     for (boost::tie(i, end) = vertices(g); i != end; ++i)
79     {
80         cout << setw(12) << setiosflags(ios::left) << g[*i].name << get(em, *i)
81              << endl;
82     }
83     cout << "radius: " << r << endl;
84     cout << "diamter: " << d << endl;
85 
86     return 0;
87 }
88 //]
89