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 
51 int
main(int argc,char * argv[])52 main(int argc, char *argv[])
53 {
54     // Create the graph and a property map that provides access
55     // to the actor names.
56     Graph g;
57     NameMap nm(get(&Actor::name, g));
58 
59     // Read the graph from standad input.
60     read_graph(g, nm, cin);
61 
62     // Compute the distances between all pairs of vertices using
63     // the Floyd-Warshall algorithm. Note that the weight map is
64     // created so that every edge has a weight of 1.
65     DistanceMatrix distances(num_vertices(g));
66     DistanceMatrixMap dm(distances, g);
67     WeightMap wm(1);
68     floyd_warshall_all_pairs_shortest_paths(g, dm, weight_map(wm));
69 
70     // Compute the mean geodesic distances for each vertex in
71     // the graph and get the average mean geodesic distace (the
72     // so-called small-world distance) as a result.
73     GeodesicContainer geodesics(num_vertices(g));
74     GeodesicMap gm(geodesics, g);
75     float sw = all_mean_geodesics(g, dm, gm);
76 
77     // Print the mean geodesic distance of each vertex and finally,
78     // the graph itself.
79     graph_traits<Graph>::vertex_iterator i, end;
80     for(boost::tie(i, end) = vertices(g); i != end; ++i) {
81         cout << setw(12) << setiosflags(ios::left)
82              << g[*i].name << get(gm, *i) << endl;
83     }
84     cout << "small world distance: " << sw << endl;
85 
86 
87     return 0;
88 }
89 //]
90