stanford_graph.hpp 18 KB

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