• 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 //[mean_geodesic_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/geodesic_distance.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 mean geodesic distances of each
46 // vertex in the graph.
47 typedef exterior_vertex_property< Graph, float > GeodesicProperty;
48 typedef GeodesicProperty::container_type GeodesicContainer;
49 typedef GeodesicProperty::map_type GeodesicMap;
50 
main(int argc,char * argv[])51 int main(int argc, char* argv[])
52 {
53     // Create the graph and a property map that provides access
54     // to the actor names.
55     Graph g;
56     NameMap nm(get(&Actor::name, g));
57 
58     // Read the graph from standad 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 mean geodesic distances for each vertex in
70     // the graph and get the average mean geodesic distace (the
71     // so-called small-world distance) as a result.
72     GeodesicContainer geodesics(num_vertices(g));
73     GeodesicMap gm(geodesics, g);
74     float sw = all_mean_geodesics(g, dm, gm);
75 
76     // Print the mean geodesic distance of each vertex and finally,
77     // the graph itself.
78     graph_traits< Graph >::vertex_iterator i, end;
79     for (boost::tie(i, end) = vertices(g); i != end; ++i)
80     {
81         cout << setw(12) << setiosflags(ios::left) << g[*i].name << get(gm, *i)
82              << endl;
83     }
84     cout << "small world distance: " << sw << endl;
85 
86     return 0;
87 }
88 //]
89