1 //=======================================================================
2 // Copyright 2002 Indiana University.
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
11 Adapted from the GIRTH program of the Stanford GraphBase.
15 This program explores the girth and diameter of Ramanujan graphs.
16 The bipartite graphs have q^3-q vertices, and the non-bipartite
17 graphs have half that number. Each vertex has degree p+1.
18 Both p and q should be odd prime numbers;
19 or you can try p = 2 with q = 17 or 43.
21 Choose a branching factor, p: 2
22 Ok, now choose the cube root of graph size, q: 17
23 Starting at any given vertex, there are
24 3 vertices at distance 1,
25 6 vertices at distance 2,
26 12 vertices at distance 3,
27 24 vertices at distance 4,
28 46 vertices at distance 5,
29 90 vertices at distance 6,
30 169 vertices at distance 7,
31 290 vertices at distance 8,
32 497 vertices at distance 9,
33 634 vertices at distance 10,
34 521 vertices at distance 11,
35 138 vertices at distance 12,
36 13 vertices at distance 13,
37 3 vertices at distance 14,
38 1 vertices at distance 15.
39 So the diameter is 15, and the girth is 9.
43 #include <boost/config.hpp>
47 #include <boost/limits.hpp>
48 #include <boost/graph/stanford_graph.hpp>
49 #include <boost/graph/breadth_first_search.hpp>
50 #include <boost/graph/graph_utility.hpp>
52 typedef boost::graph_traits
<Graph
*> Traits
;
53 typedef Traits::vertex_descriptor vertex_descriptor
;
54 typedef Traits::edge_descriptor edge_descriptor
;
55 typedef Traits::vertex_iterator vertex_iterator
;
57 std::vector
<std::size_t> distance_list
;
59 typedef boost::v_property
<long> dist_t
;
60 boost::property_map
<Graph
*, dist_t
>::type d_map
;
62 typedef boost::u_property
<vertex_descriptor
> pred_t
;
63 boost::property_map
<Graph
*, pred_t
>::type p_map
;
65 typedef boost::w_property
<long> color_t
;
66 boost::property_map
<Graph
*, color_t
>::type c_map
;
68 class diameter_and_girth_visitor
: public boost::bfs_visitor
<>
71 diameter_and_girth_visitor(std::size_t& k_
, std::size_t& girth_
)
72 : k(k_
), girth(girth_
) { }
74 void tree_edge(edge_descriptor e
, Graph
* g
) {
75 vertex_descriptor u
= source(e
, g
), v
= target(e
, g
);
81 void non_tree_edge(edge_descriptor e
, Graph
* g
) {
82 vertex_descriptor u
= source(e
, g
), v
= target(e
, g
);
84 if (d_map
[v
] + k
< girth
&& v
!= p_map
[u
])
97 "This program explores the girth and diameter of Ramanujan graphs."
100 "The bipartite graphs have q^3-q vertices, and the non-bipartite"
103 "graphs have half that number. Each vertex has degree p+1."
105 std::cout
<< "Both p and q should be odd prime numbers;" << std::endl
;
106 std::cout
<< " or you can try p = 2 with q = 17 or 43." << std::endl
;
110 std::cout
<< std::endl
111 << "Choose a branching factor, p: ";
116 std::cout
<< "Ok, now choose the cube root of graph size, q: ";
122 g
= raman(p
, q
, 0L, 0L);
124 std::cerr
<< " Sorry, I couldn't make that graph (error code "
125 << panic_code
<< ")" << std::endl
;
128 distance_list
.clear();
129 distance_list
.resize(boost::num_vertices(g
), 0);
131 // obtain property maps
132 d_map
= get(dist_t(), g
);
133 p_map
= get(pred_t(), g
);
134 c_map
= get(color_t(), g
);
136 vertex_iterator i
, end
;
137 for (boost::tie(i
, end
) = boost::vertices(g
); i
!= end
; ++i
)
141 std::size_t girth
= (std::numeric_limits
<std::size_t>::max
)();
142 diameter_and_girth_visitor
vis(k
, girth
);
144 vertex_descriptor s
= *boost::vertices(g
).first
;
146 boost::breadth_first_search(g
, s
, visitor(vis
).color_map(c_map
));
148 std::cout
<< "Starting at any given vertex, there are" << std::endl
;
150 for (long d
= 1; distance_list
[d
] != 0; ++d
)
151 std::cout
<< distance_list
[d
] << " vertices at distance " << d
152 << (distance_list
[d
+1] != 0 ? "," : ".") << std::endl
;
154 std::cout
<< "So the diameter is " << k
- 1
155 << ", and the girth is " << girth