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 //[inclusive_mean_geodesic_example
8 #include <iostream>
9 #include <iomanip>
10
11 #include <boost/graph/directed_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 // This template structure defines the function that we will apply
21 // to compute both the per-vertex mean geodesic distances and the
22 // graph's mean geodesic distance.
23 template < typename Graph, typename DistanceType, typename ResultType,
24 typename Divides = divides< ResultType > >
25 struct inclusive_average
26 {
27 typedef DistanceType distance_type;
28 typedef ResultType result_type;
29
operator ()inclusive_average30 result_type operator()(distance_type d, const Graph& g)
31 {
32 if (d == numeric_values< distance_type >::infinity())
33 {
34 return numeric_values< result_type >::infinity();
35 }
36 else
37 {
38 return div(result_type(d), result_type(num_vertices(g)));
39 }
40 }
41 Divides div;
42 };
43
44 // The Page type stores the name of each vertex in the graph and
45 // represents web pages that can be navigated to.
46 struct WebPage
47 {
48 string name;
49 };
50
51 // The Link type stores an associated probability of traveling
52 // from one page to another.
53 struct Link
54 {
55 float probability;
56 };
57
58 // Declare the graph type and its vertex and edge types.
59 typedef directed_graph< WebPage, Link > Graph;
60 typedef graph_traits< Graph >::vertex_descriptor Vertex;
61 typedef graph_traits< Graph >::edge_descriptor Edge;
62
63 // The name map provides an abstract accessor for the names of
64 // each vertex. This is used during graph creation.
65 typedef property_map< Graph, string WebPage::* >::type NameMap;
66
67 // Declare a matrix type and its corresponding property map that
68 // will contain the distances between each pair of vertices.
69 typedef exterior_vertex_property< Graph, float > DistanceProperty;
70 typedef DistanceProperty::matrix_type DistanceMatrix;
71 typedef DistanceProperty::matrix_map_type DistanceMatrixMap;
72
73 // Declare the weight map as an accessor into the bundled
74 // edge property.
75 typedef property_map< Graph, float Link::* >::type WeightMap;
76
77 // Declare a container and its corresponding property map that
78 // will contain the resulting mean geodesic distances of each
79 // vertex in the graph.
80 typedef exterior_vertex_property< Graph, float > GeodesicProperty;
81 typedef GeodesicProperty::container_type GeodesicContainer;
82 typedef GeodesicProperty::map_type GeodesicMap;
83
84 static float exclusive_geodesics(const Graph&, DistanceMatrixMap, GeodesicMap);
85 static float inclusive_geodesics(const Graph&, DistanceMatrixMap, GeodesicMap);
86
main(int argc,char * argv[])87 int main(int argc, char* argv[])
88 {
89 // Create the graph, a name map that providse abstract access
90 // to the web page names, and the weight map as an accessor to
91 // the edge weights (or probabilities).
92 Graph g;
93 NameMap nm(get(&WebPage::name, g));
94 WeightMap wm(get(&Link::probability, g));
95
96 // Read the weighted graph from standard input.
97 read_weighted_graph(g, nm, wm, cin);
98
99 // Compute the distances between all pairs of vertices using
100 // the Floyd-Warshall algorithm. The weight map was created
101 // above so it could be populated when the graph was read in.
102 DistanceMatrix distances(num_vertices(g));
103 DistanceMatrixMap dm(distances, g);
104 floyd_warshall_all_pairs_shortest_paths(g, dm, weight_map(wm));
105
106 // Create the containers and the respective property maps that
107 // will contain the mean geodesics averaged both including
108 // self-loop distances and excluding them.
109 GeodesicContainer exclude(num_vertices(g));
110 GeodesicContainer include(num_vertices(g));
111 GeodesicMap exmap(exclude, g);
112 GeodesicMap inmap(include, g);
113
114 float ex = exclusive_geodesics(g, dm, exmap);
115 float in = inclusive_geodesics(g, dm, inmap);
116
117 // Print the mean geodesic distance of each vertex and finally,
118 // the graph itself.
119 cout << setw(12) << setiosflags(ios::left) << "vertex";
120 cout << setw(12) << setiosflags(ios::left) << "excluding";
121 cout << setw(12) << setiosflags(ios::left) << "including" << endl;
122 graph_traits< Graph >::vertex_iterator i, end;
123 for (boost::tie(i, end) = vertices(g); i != end; ++i)
124 {
125 cout << setw(12) << setiosflags(ios::left) << g[*i].name << setw(12)
126 << get(exmap, *i) << setw(12) << get(inmap, *i) << endl;
127 }
128 cout << "small world (excluding self-loops): " << ex << endl;
129 cout << "small world (including self-loops): " << in << endl;
130
131 return 0;
132 }
133
exclusive_geodesics(const Graph & g,DistanceMatrixMap dm,GeodesicMap gm)134 float exclusive_geodesics(const Graph& g, DistanceMatrixMap dm, GeodesicMap gm)
135 {
136 // Compute the mean geodesic distances, which excludes distances
137 // of self-loops by default. Return the measure for the entire graph.
138 return all_mean_geodesics(g, dm, gm);
139 }
140
inclusive_geodesics(const Graph & g,DistanceMatrixMap dm,GeodesicMap gm)141 float inclusive_geodesics(const Graph& g, DistanceMatrixMap dm, GeodesicMap gm)
142 {
143 // Create a new measure object for computing the mean geodesic
144 // distance of all vertices. This measure will actually be used
145 // for both averages.
146 inclusive_average< Graph, float, float > m;
147
148 // Compute the mean geodesic distance using the inclusive average
149 // to account for self-loop distances. Return the measure for the
150 // entire graph.
151 return all_mean_geodesics(g, dm, gm, m);
152 }
153 //]
154