stanford_graph.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. //=======================================================================
  2. // Copyright 1997-2001 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  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. //=======================================================================
  9. #ifndef BOOST_GRAPH_SGB_GRAPH_HPP
  10. #define BOOST_GRAPH_SGB_GRAPH_HPP
  11. #include <boost/config.hpp>
  12. #include <boost/operators.hpp>
  13. #include <boost/property_map/property_map.hpp>
  14. #include <boost/graph/graph_traits.hpp>
  15. #include <boost/graph/properties.hpp>
  16. // Thanks to Andreas Scherer for numerous suggestions and fixes!
  17. // This file adapts a Stanford GraphBase (SGB) Graph pointer into a
  18. // VertexListGraph. Note that a graph adaptor class is not needed,
  19. // SGB's Graph* is used as is. The VertexListGraph concept is fulfilled by
  20. // defining the appropriate non-member functions for Graph*.
  21. //
  22. // The PROTOTYPES change file extensions to SGB must be applied so
  23. // that the SGB functions have real prototypes which are necessary for
  24. // the C++ compiler. To apply the PROTOTYPES extensions, before you do
  25. // "make tests install" for SGB do "ln -s PROTOTYPES/* ." to the SGB
  26. // root directory (or just copy all the files from the PROTOTYPES
  27. // directory to the SGB root directory).
  28. //
  29. extern "C" {
  30. // We include all global definitions for the general stuff
  31. // of The Stanford GraphBase and its various graph generator
  32. // functions by reading all SGB headerfiles as in section 2 of
  33. // the "test_sample" program.
  34. #include <gb_graph.h> /* SGB data structures */
  35. #include <gb_io.h> /* SGB input/output routines */
  36. #include <gb_flip.h> /* random number generator */
  37. #include <gb_dijk.h> /* routines for shortest paths */
  38. #include <gb_basic.h> /* the basic graph operations */
  39. #undef empty /* avoid name clash with C++ standard library */
  40. inline Graph* empty( long n ) /* and provide workaround */
  41. { return board(n,0L,0L,0L,2L,0L,0L); }
  42. #include <gb_books.h> /* graphs based on literature */
  43. #include <gb_econ.h> /* graphs based on economic data */
  44. #include <gb_games.h> /* graphs based on football scores */
  45. #undef ap /* avoid name clash with BGL parameter */
  46. // ap ==> Vertex::u.I
  47. #include <gb_gates.h> /* graphs based on logic circuits */
  48. #undef val /* avoid name clash with g++ headerfile stl_tempbuf.h */
  49. // val ==> Vertex::x.I
  50. #include <gb_lisa.h> /* graphs based on Mona Lisa */
  51. #include <gb_miles.h> /* graphs based on mileage data */
  52. #include <gb_plane.h> /* planar graphs */
  53. #include <gb_raman.h> /* Ramanujan graphs */
  54. #include <gb_rand.h> /* random graphs */
  55. #include <gb_roget.h> /* graphs based on Roget's Thesaurus */
  56. #include <gb_save.h> /* we save results in ASCII format */
  57. #include <gb_words.h> /* five-letter-word graphs */
  58. #undef weight /* avoid name clash with BGL parameter */
  59. // weight ==> Vertex::u.I
  60. }
  61. namespace boost {
  62. class sgb_edge;
  63. }
  64. class sgb_out_edge_iterator;
  65. class sgb_adj_iterator;
  66. class sgb_vertex_iterator;
  67. namespace boost {
  68. typedef Graph* sgb_graph_ptr;
  69. typedef const Graph* sgb_const_graph_ptr;
  70. struct sgb_traversal_tag :
  71. public virtual vertex_list_graph_tag,
  72. public virtual incidence_graph_tag,
  73. public virtual adjacency_graph_tag { };
  74. template <> struct graph_traits<sgb_graph_ptr> {
  75. typedef Vertex* vertex_descriptor;
  76. typedef boost::sgb_edge edge_descriptor;
  77. typedef sgb_out_edge_iterator out_edge_iterator;
  78. typedef void in_edge_iterator;
  79. typedef sgb_adj_iterator adjacency_iterator;
  80. typedef sgb_vertex_iterator vertex_iterator;
  81. typedef void edge_iterator;
  82. typedef long vertices_size_type;
  83. typedef long edge_size_type;
  84. typedef long degree_size_type;
  85. typedef directed_tag directed_category;
  86. typedef sgb_traversal_tag traversal_category;
  87. typedef allow_parallel_edge_tag edge_parallel_category;
  88. /** Return a null descriptor */
  89. static vertex_descriptor null_vertex()
  90. { return NULL; }
  91. };
  92. template <> struct graph_traits<sgb_const_graph_ptr> {
  93. typedef Vertex* vertex_descriptor;
  94. typedef boost::sgb_edge edge_descriptor;
  95. typedef sgb_out_edge_iterator out_edge_iterator;
  96. typedef void in_edge_iterator;
  97. typedef sgb_adj_iterator adjacency_iterator;
  98. typedef sgb_vertex_iterator vertex_iterator;
  99. typedef void edge_iterator;
  100. typedef long vertices_size_type;
  101. typedef long edge_size_type;
  102. typedef long degree_size_type;
  103. typedef directed_tag directed_category;
  104. typedef sgb_traversal_tag traversal_category;
  105. typedef allow_parallel_edge_tag edge_parallel_category;
  106. /** Return a null descriptor */
  107. static vertex_descriptor null_vertex()
  108. { return NULL; }
  109. };
  110. }
  111. namespace boost {
  112. struct edge_length_t {
  113. typedef edge_property_tag kind;
  114. };
  115. // We could just use Arc* as the edge descriptor type, but
  116. // we want to add the source(e,g) function which requires
  117. // that we carry along a pointer to the source vertex.
  118. class sgb_edge {
  119. typedef sgb_edge self;
  120. public:
  121. sgb_edge() : _arc(0), _src(0) { }
  122. sgb_edge(Arc* a, Vertex* s) : _arc(a), _src(s) { }
  123. friend Vertex* source(self e, sgb_const_graph_ptr) { return e._src; }
  124. friend Vertex* target(self e, sgb_const_graph_ptr) { return e._arc->tip; }
  125. friend bool operator==(const self& a, const self& b) {
  126. return a._arc == b._arc; }
  127. friend bool operator!=(const self& a, const self& b) {
  128. return a._arc != b._arc; }
  129. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  130. template <class Ref> friend class sgb_edge_length_map;
  131. template <class Tag, class Ref> friend class sgb_edge_util_map;
  132. friend long get(edge_length_t, const sgb_graph_ptr&, const sgb_edge& key);
  133. friend long get(edge_length_t, const sgb_const_graph_ptr&, const sgb_edge& key);
  134. friend void put(edge_length_t, sgb_graph_ptr&, const sgb_edge& key, long value);
  135. protected:
  136. #endif
  137. Arc* _arc;
  138. Vertex* _src;
  139. };
  140. } // namespace boost
  141. class sgb_out_edge_iterator
  142. : public boost::forward_iterator_helper<
  143. sgb_out_edge_iterator, boost::sgb_edge,
  144. std::ptrdiff_t, boost::sgb_edge*, boost::sgb_edge>
  145. {
  146. typedef sgb_out_edge_iterator self;
  147. public:
  148. sgb_out_edge_iterator() : _src(0), _arc(0) {}
  149. sgb_out_edge_iterator(Vertex* s, Arc* d) : _src(s), _arc(d) {}
  150. boost::sgb_edge operator*() { return boost::sgb_edge(_arc, _src); }
  151. self& operator++() { _arc = _arc->next; return *this; }
  152. friend bool operator==(const self& x, const self& y) {
  153. return x._arc == y._arc; }
  154. protected:
  155. Vertex* _src;
  156. Arc* _arc;
  157. };
  158. class sgb_adj_iterator
  159. : public boost::forward_iterator_helper<
  160. sgb_adj_iterator, Vertex*, std::ptrdiff_t, Vertex**,Vertex*>
  161. {
  162. typedef sgb_adj_iterator self;
  163. public:
  164. sgb_adj_iterator() : _arc(0) {}
  165. sgb_adj_iterator(Arc* d) : _arc(d) {}
  166. Vertex* operator*() { return _arc->tip; }
  167. self& operator++() { _arc = _arc->next; return *this; }
  168. friend bool operator==(const self& x, const self& y) {
  169. return x._arc == y._arc; }
  170. protected:
  171. Arc* _arc;
  172. };
  173. // The reason we have this instead of just using Vertex* is that we
  174. // want to use Vertex* as the vertex_descriptor instead of just
  175. // Vertex, which avoids problems with boost passing vertex descriptors
  176. // by value and how that interacts with the sgb_vertex_id_map.
  177. class sgb_vertex_iterator
  178. : public boost::forward_iterator_helper<
  179. sgb_vertex_iterator, Vertex*, std::ptrdiff_t, Vertex**, Vertex*>
  180. {
  181. typedef sgb_vertex_iterator self;
  182. public:
  183. sgb_vertex_iterator() : _v(0) { }
  184. sgb_vertex_iterator(Vertex* v) : _v(v) { }
  185. Vertex* operator*() { return _v; }
  186. self& operator++() { ++_v; return *this; }
  187. friend bool operator==(const self& x, const self& y) {
  188. return x._v == y._v; }
  189. protected:
  190. Vertex* _v;
  191. };
  192. namespace boost {
  193. inline std::pair<sgb_vertex_iterator,sgb_vertex_iterator>
  194. vertices(sgb_const_graph_ptr g)
  195. {
  196. return std::make_pair(sgb_vertex_iterator(g->vertices),
  197. sgb_vertex_iterator(g->vertices + g->n));
  198. }
  199. inline std::pair<sgb_out_edge_iterator,sgb_out_edge_iterator>
  200. out_edges(Vertex* u, sgb_const_graph_ptr)
  201. {
  202. return std::make_pair( sgb_out_edge_iterator(u, u->arcs),
  203. sgb_out_edge_iterator(u, 0) );
  204. }
  205. inline boost::graph_traits<sgb_graph_ptr>::degree_size_type
  206. out_degree(Vertex* u, sgb_const_graph_ptr g)
  207. {
  208. boost::graph_traits<sgb_graph_ptr>::out_edge_iterator i, i_end;
  209. boost::tie(i, i_end) = out_edges(u, g);
  210. return std::distance(i, i_end);
  211. }
  212. // in_edges?
  213. inline std::pair<sgb_adj_iterator,sgb_adj_iterator>
  214. adjacent_vertices(Vertex* u, sgb_const_graph_ptr)
  215. {
  216. return std::make_pair( sgb_adj_iterator(u->arcs),
  217. sgb_adj_iterator(0) );
  218. }
  219. inline long num_vertices(sgb_const_graph_ptr g) { return g->n; }
  220. inline long num_edges(sgb_const_graph_ptr g) { return g->m; }
  221. inline Vertex* vertex(long v, sgb_const_graph_ptr g)
  222. { return g->vertices + v; }
  223. // Various Property Maps
  224. // Vertex ID
  225. class sgb_vertex_id_map
  226. : public boost::put_get_helper<long, sgb_vertex_id_map>
  227. {
  228. public:
  229. typedef boost::readable_property_map_tag category;
  230. typedef long value_type;
  231. typedef long reference;
  232. typedef Vertex* key_type;
  233. sgb_vertex_id_map() : _g(0) { }
  234. sgb_vertex_id_map(sgb_graph_ptr g) : _g(g) { }
  235. long operator[](Vertex* v) const { return v - _g->vertices; }
  236. protected:
  237. sgb_graph_ptr _g;
  238. };
  239. inline sgb_vertex_id_map get(vertex_index_t, sgb_graph_ptr g) {
  240. return sgb_vertex_id_map(g);
  241. }
  242. // Vertex Name
  243. class sgb_vertex_name_map
  244. : public boost::put_get_helper<char*, sgb_vertex_name_map>
  245. {
  246. public:
  247. typedef boost::readable_property_map_tag category;
  248. typedef char* value_type;
  249. typedef char* reference;
  250. typedef Vertex* key_type;
  251. char* operator[](Vertex* v) const { return v->name; }
  252. };
  253. inline sgb_vertex_name_map get(vertex_name_t, sgb_graph_ptr) {
  254. return sgb_vertex_name_map();
  255. }
  256. // Vertex Property Tags
  257. #define SGB_PROPERTY_TAG(KIND,TAG) \
  258. template <class T> struct TAG##_property { \
  259. typedef KIND##_property_tag kind; \
  260. typedef T type; \
  261. };
  262. SGB_PROPERTY_TAG(vertex, u)
  263. SGB_PROPERTY_TAG(vertex, v)
  264. SGB_PROPERTY_TAG(vertex, w)
  265. SGB_PROPERTY_TAG(vertex, x)
  266. SGB_PROPERTY_TAG(vertex, y)
  267. SGB_PROPERTY_TAG(vertex, z)
  268. // Edge Property Tags
  269. SGB_PROPERTY_TAG(edge, a)
  270. SGB_PROPERTY_TAG(edge, b)
  271. // Various Utility Maps
  272. // helpers
  273. inline Vertex*& get_util(util& u, Vertex*) { return u.V; }
  274. inline Arc*& get_util(util& u, Arc*) { return u.A; }
  275. inline sgb_graph_ptr& get_util(util& u, sgb_graph_ptr) { return u.G; }
  276. inline char*& get_util(util& u, char*) { return u.S; }
  277. inline long& get_util(util& u, long) { return u.I; }
  278. #define SGB_GET_UTIL_FIELD(KIND,X) \
  279. template <class T> \
  280. inline T& get_util_field(KIND* k, X##_property<T>) { \
  281. return get_util(k->X, T()); }
  282. SGB_GET_UTIL_FIELD(Vertex, u)
  283. SGB_GET_UTIL_FIELD(Vertex, v)
  284. SGB_GET_UTIL_FIELD(Vertex, w)
  285. SGB_GET_UTIL_FIELD(Vertex, x)
  286. SGB_GET_UTIL_FIELD(Vertex, y)
  287. SGB_GET_UTIL_FIELD(Vertex, z)
  288. SGB_GET_UTIL_FIELD(Arc, a)
  289. SGB_GET_UTIL_FIELD(Arc, b)
  290. // Vertex Utility Map
  291. template <class Tag, class Ref>
  292. class sgb_vertex_util_map
  293. : public boost::put_get_helper<Ref, sgb_vertex_util_map<Tag, Ref> >
  294. {
  295. Tag tag;
  296. public:
  297. explicit sgb_vertex_util_map(Tag tag = Tag()): tag(tag) {}
  298. typedef boost::lvalue_property_map_tag category;
  299. typedef typename Tag::type value_type;
  300. typedef Vertex* key_type;
  301. typedef Ref reference;
  302. reference operator[](Vertex* v) const {
  303. return get_util_field(v, tag);
  304. }
  305. };
  306. // Edge Utility Map
  307. template <class Tag, class Ref>
  308. class sgb_edge_util_map
  309. : public boost::put_get_helper<Ref, sgb_edge_util_map<Tag, Ref> >
  310. {
  311. Tag tag;
  312. public:
  313. explicit sgb_edge_util_map(Tag tag = Tag()): tag(tag) {}
  314. typedef boost::lvalue_property_map_tag category;
  315. typedef typename Tag::type value_type;
  316. typedef Vertex* key_type;
  317. typedef Ref reference;
  318. reference operator[](const sgb_edge& e) const {
  319. return get_util_field(e._arc, tag);
  320. }
  321. };
  322. template <class Tag>
  323. inline sgb_vertex_util_map<Tag, const typename Tag::type&>
  324. get_property_map(Tag, const sgb_graph_ptr& g, vertex_property_tag) {
  325. return sgb_vertex_util_map<Tag, const typename Tag::type&>();
  326. }
  327. template <class Tag>
  328. inline sgb_vertex_util_map<Tag, typename Tag::type&>
  329. get_property_map(Tag, sgb_graph_ptr& g, vertex_property_tag) {
  330. return sgb_vertex_util_map<Tag, typename Tag::type&>();
  331. }
  332. template <class Tag>
  333. inline sgb_edge_util_map<Tag, const typename Tag::type&>
  334. get_property_map(Tag, const sgb_graph_ptr& g, edge_property_tag) {
  335. return sgb_edge_util_map<Tag, const typename Tag::type&>();
  336. }
  337. template <class Tag>
  338. inline sgb_edge_util_map<Tag, typename Tag::type&>
  339. get_property_map(Tag, sgb_graph_ptr& g, edge_property_tag) {
  340. return sgb_edge_util_map<Tag, typename Tag::type&>();
  341. }
  342. // Edge Length Access
  343. template <class Ref>
  344. class sgb_edge_length_map
  345. : public boost::put_get_helper<Ref, sgb_edge_length_map<Ref> >
  346. {
  347. public:
  348. typedef boost::lvalue_property_map_tag category;
  349. typedef long value_type;
  350. typedef sgb_edge key_type;
  351. typedef Ref reference;
  352. reference operator[](const sgb_edge& e) const {
  353. return e._arc->len;
  354. }
  355. };
  356. inline sgb_edge_length_map<const long&>
  357. get(edge_length_t, const sgb_graph_ptr&) {
  358. return sgb_edge_length_map<const long&>();
  359. }
  360. inline sgb_edge_length_map<const long&>
  361. get(edge_length_t, const sgb_const_graph_ptr&) {
  362. return sgb_edge_length_map<const long&>();
  363. }
  364. inline sgb_edge_length_map<long&>
  365. get(edge_length_t, sgb_graph_ptr&) {
  366. return sgb_edge_length_map<long&>();
  367. }
  368. inline long
  369. get(edge_length_t, const sgb_graph_ptr&, const sgb_edge& key) {
  370. return key._arc->len;
  371. }
  372. inline long
  373. get(edge_length_t, const sgb_const_graph_ptr&, const sgb_edge& key) {
  374. return key._arc->len;
  375. }
  376. inline void
  377. put(edge_length_t, sgb_graph_ptr&, const sgb_edge& key, long value)
  378. {
  379. key._arc->len = value;
  380. }
  381. // Property Map Traits Classes
  382. template <>
  383. struct property_map<sgb_graph_ptr, edge_length_t> {
  384. typedef sgb_edge_length_map<long&> type;
  385. typedef sgb_edge_length_map<const long&> const_type;
  386. };
  387. template <>
  388. struct property_map<sgb_graph_ptr, vertex_index_t> {
  389. typedef sgb_vertex_id_map type;
  390. typedef sgb_vertex_id_map const_type;
  391. };
  392. template <>
  393. struct property_map<sgb_graph_ptr, vertex_name_t> {
  394. typedef sgb_vertex_name_map type;
  395. typedef sgb_vertex_name_map const_type;
  396. };
  397. template <>
  398. struct property_map<sgb_const_graph_ptr, edge_length_t> {
  399. typedef sgb_edge_length_map<const long&> const_type;
  400. };
  401. template <>
  402. struct property_map<sgb_const_graph_ptr, vertex_index_t> {
  403. typedef sgb_vertex_id_map const_type;
  404. };
  405. template <>
  406. struct property_map<sgb_const_graph_ptr, vertex_name_t> {
  407. typedef sgb_vertex_name_map const_type;
  408. };
  409. namespace detail {
  410. template <class Kind, class PropertyTag>
  411. struct sgb_choose_property_map { };
  412. template <class PropertyTag>
  413. struct sgb_choose_property_map<vertex_property_tag, PropertyTag> {
  414. typedef typename PropertyTag::type value_type;
  415. typedef sgb_vertex_util_map<PropertyTag, value_type&> type;
  416. typedef sgb_vertex_util_map<PropertyTag, const value_type&> const_type;
  417. };
  418. template <class PropertyTag>
  419. struct sgb_choose_property_map<edge_property_tag, PropertyTag> {
  420. typedef typename PropertyTag::type value_type;
  421. typedef sgb_edge_util_map<PropertyTag, value_type&> type;
  422. typedef sgb_edge_util_map<PropertyTag, const value_type&> const_type;
  423. };
  424. } // namespace detail
  425. template <class PropertyTag>
  426. struct property_map<sgb_graph_ptr, PropertyTag> {
  427. typedef typename property_kind<PropertyTag>::type Kind;
  428. typedef detail::sgb_choose_property_map<Kind, PropertyTag> Choice;
  429. typedef typename Choice::type type;
  430. typedef typename Choice::const_type const_type;
  431. };
  432. template <class PropertyTag>
  433. struct property_map<sgb_const_graph_ptr, PropertyTag> {
  434. typedef typename property_kind<PropertyTag>::type Kind;
  435. typedef detail::sgb_choose_property_map<Kind, PropertyTag> Choice;
  436. typedef typename Choice::const_type const_type;
  437. };
  438. #define SGB_UTIL_ACCESSOR(KIND,X) \
  439. template <class T> \
  440. inline sgb_##KIND##_util_map< X##_property<T>, T&> \
  441. get(X##_property<T>, sgb_graph_ptr&) { \
  442. return sgb_##KIND##_util_map< X##_property<T>, T&>(); \
  443. } \
  444. template <class T> \
  445. inline sgb_##KIND##_util_map< X##_property<T>, const T&> \
  446. get(X##_property<T>, const sgb_graph_ptr&) { \
  447. return sgb_##KIND##_util_map< X##_property<T>, const T&>(); \
  448. } \
  449. template <class T> \
  450. inline sgb_##KIND##_util_map< X##_property<T>, const T&> \
  451. get(X##_property<T>, const sgb_const_graph_ptr&) { \
  452. return sgb_##KIND##_util_map< X##_property<T>, const T&>(); \
  453. } \
  454. template <class T, class Key> \
  455. inline typename \
  456. sgb_##KIND##_util_map< X##_property<T>, const T&>::value_type \
  457. get(X##_property<T>, const sgb_graph_ptr&, const Key& key) { \
  458. return sgb_##KIND##_util_map< X##_property<T>, const T&>()[key]; \
  459. } \
  460. template <class T, class Key> \
  461. inline typename \
  462. sgb_##KIND##_util_map< X##_property<T>, const T&>::value_type \
  463. get(X##_property<T>, const sgb_const_graph_ptr&, const Key& key) { \
  464. return sgb_##KIND##_util_map< X##_property<T>, const T&>()[key]; \
  465. } \
  466. template <class T, class Key, class Value> \
  467. inline void \
  468. put(X##_property<T>, sgb_graph_ptr&, const Key& key, const Value& value) { \
  469. sgb_##KIND##_util_map< X##_property<T>, T&>()[key] = value; \
  470. }
  471. SGB_UTIL_ACCESSOR(vertex, u)
  472. SGB_UTIL_ACCESSOR(vertex, v)
  473. SGB_UTIL_ACCESSOR(vertex, w)
  474. SGB_UTIL_ACCESSOR(vertex, x)
  475. SGB_UTIL_ACCESSOR(vertex, y)
  476. SGB_UTIL_ACCESSOR(vertex, z)
  477. SGB_UTIL_ACCESSOR(edge, a)
  478. SGB_UTIL_ACCESSOR(edge, b)
  479. } // namespace boost
  480. #endif // BOOST_GRAPH_SGB_GRAPH_HPP