parallel_edges_loops_test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //=======================================================================
  2. // Copyright 2007 Aaron Windsor
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. /*
  9. This test is almost identical to all_planar_input_files_test.cpp
  10. except that parallel edges and loops are added to the graphs as
  11. they are read in.
  12. This test needs to be linked against Boost.Filesystem.
  13. */
  14. #define BOOST_FILESYSTEM_VERSION 3
  15. #include <iostream>
  16. #include <fstream>
  17. #include <vector>
  18. #include <string>
  19. #include <utility>
  20. #include <boost/property_map/property_map.hpp>
  21. #include <boost/lexical_cast.hpp>
  22. #include <boost/tuple/tuple.hpp>
  23. #include <boost/filesystem.hpp>
  24. #include <boost/algorithm/string.hpp>
  25. #include <boost/test/minimal.hpp>
  26. #include <boost/graph/adjacency_list.hpp>
  27. #include <boost/graph/depth_first_search.hpp>
  28. #include <boost/graph/properties.hpp>
  29. #include <boost/graph/graph_traits.hpp>
  30. #include <boost/graph/planar_canonical_ordering.hpp>
  31. #include <boost/graph/make_connected.hpp>
  32. #include <boost/graph/make_biconnected_planar.hpp>
  33. #include <boost/graph/make_maximal_planar.hpp>
  34. #include <boost/graph/is_straight_line_drawing.hpp>
  35. #include <boost/graph/is_kuratowski_subgraph.hpp>
  36. #include <boost/graph/chrobak_payne_drawing.hpp>
  37. #include <boost/graph/boyer_myrvold_planar_test.hpp>
  38. #include <boost/graph/planar_detail/add_edge_visitors.hpp>
  39. using namespace boost;
  40. struct coord_t
  41. {
  42. std::size_t x;
  43. std::size_t y;
  44. };
  45. template <typename Graph>
  46. void read_dimacs(Graph& g, const std::string& filename)
  47. {
  48. // every <vertex_stride>th vertex has a self-loop
  49. int vertex_stride = 5;
  50. // on vertices with self loops, there are between 1 and
  51. // <max_loop_multiplicity> loops
  52. int max_loop_multiplicity = 6;
  53. // every <edge_stride>th edge is a parallel edge
  54. int edge_stride = 7;
  55. // parallel edges come in groups of 2 to <max_edge_multiplicity> + 1
  56. int max_edge_multiplicity = 5;
  57. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator_t;
  58. typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
  59. std::vector<vertex_t> vertices_by_index;
  60. std::ifstream in(filename.c_str());
  61. long num_edges_added = 0;
  62. long num_parallel_edges = 0;
  63. while (!in.eof())
  64. {
  65. char buffer[256];
  66. in.getline(buffer, 256);
  67. std::string s(buffer);
  68. if (s.size() == 0)
  69. continue;
  70. std::vector<std::string> v;
  71. split(v, buffer, is_any_of(" \t\n"));
  72. if (v[0] == "p")
  73. {
  74. //v[1] == "edge"
  75. long num_vertices = boost::lexical_cast<long>(v[2].c_str());
  76. g = Graph(num_vertices);
  77. vertex_iterator_t vi, vi_end;
  78. long count = 0;
  79. long mult_count = 0;
  80. for(boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  81. {
  82. if (count % vertex_stride == 0)
  83. {
  84. for(int i = 0;
  85. i < (mult_count % max_loop_multiplicity) + 1;
  86. ++i
  87. )
  88. {
  89. add_edge(*vi, *vi, g);
  90. }
  91. ++mult_count;
  92. }
  93. ++count;
  94. }
  95. std::copy(vertices(g).first,
  96. vertices(g).second,
  97. std::back_inserter(vertices_by_index)
  98. );
  99. }
  100. else if (v[0] == "e")
  101. {
  102. add_edge(vertices_by_index[boost::lexical_cast<long>(v[1].c_str())],
  103. vertices_by_index[boost::lexical_cast<long>(v[2].c_str())],
  104. g);
  105. if (num_edges_added % edge_stride == 0)
  106. {
  107. for(int i = 0;
  108. i < (num_parallel_edges % max_edge_multiplicity) + 1;
  109. ++i
  110. )
  111. {
  112. add_edge(vertices_by_index
  113. [boost::lexical_cast<long>(v[1].c_str())],
  114. vertices_by_index
  115. [boost::lexical_cast<long>(v[2].c_str())],
  116. g);
  117. }
  118. ++num_parallel_edges;
  119. }
  120. ++num_edges_added;
  121. }
  122. }
  123. }
  124. struct face_counter : planar_face_traversal_visitor
  125. {
  126. face_counter() : m_num_faces(0) {}
  127. void begin_face() { ++m_num_faces; }
  128. long num_faces() { return m_num_faces; }
  129. private:
  130. long m_num_faces;
  131. };
  132. int test_graph(const std::string& dimacs_filename)
  133. {
  134. typedef adjacency_list<listS,
  135. vecS,
  136. undirectedS,
  137. property<vertex_index_t, int>,
  138. property<edge_index_t, int> > graph;
  139. typedef graph_traits<graph>::edge_descriptor edge_t;
  140. typedef graph_traits<graph>::edge_iterator edge_iterator_t;
  141. typedef graph_traits<graph>::vertex_iterator vertex_iterator_t;
  142. typedef graph_traits<graph>::edges_size_type e_size_t;
  143. typedef graph_traits<graph>::vertex_descriptor vertex_t;
  144. typedef edge_index_update_visitor<property_map<graph, edge_index_t>::type>
  145. edge_visitor_t;
  146. vertex_iterator_t vi, vi_end;
  147. edge_iterator_t ei, ei_end;
  148. graph g;
  149. read_dimacs(g, dimacs_filename);
  150. // Initialize the interior edge index
  151. property_map<graph, edge_index_t>::type e_index = get(edge_index, g);
  152. e_size_t edge_count = 0;
  153. for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  154. put(e_index, *ei, edge_count++);
  155. // Initialize the interior vertex index - not needed if the vertices
  156. // are stored with a vecS
  157. /*
  158. property_map<graph, vertex_index_t>::type v_index = get(vertex_index, g);
  159. v_size_t vertex_count = 0;
  160. for(boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  161. put(v_index, *vi, vertex_count++);
  162. */
  163. // This edge_updater will automatically update the interior edge
  164. // index of the graph as edges are created.
  165. edge_visitor_t edge_updater(get(edge_index, g), num_edges(g));
  166. // The input graph may not be maximal planar, but the Chrobak-Payne straight
  167. // line drawing needs a maximal planar graph as input. So, we make a copy of
  168. // the original graph here, then add edges to the graph to make it maximal
  169. // planar. When we're done creating a drawing of the maximal planar graph,
  170. // we can use the same mapping of vertices to points on the grid to embed the
  171. // original, non-maximal graph.
  172. graph g_copy(g);
  173. // Add edges to make g connected, if it isn't already
  174. make_connected(g, get(vertex_index, g), edge_updater);
  175. std::vector<graph_traits<graph>::edge_descriptor> kuratowski_edges;
  176. typedef std::vector< std::vector<edge_t> > edge_permutation_storage_t;
  177. typedef boost::iterator_property_map
  178. < edge_permutation_storage_t::iterator,
  179. property_map<graph, vertex_index_t>::type
  180. >
  181. edge_permutation_t;
  182. edge_permutation_storage_t edge_permutation_storage(num_vertices(g));
  183. edge_permutation_t perm(edge_permutation_storage.begin(),
  184. get(vertex_index,g)
  185. );
  186. // Test for planarity, computing the planar embedding or the kuratowski
  187. // subgraph.
  188. if (!boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  189. boyer_myrvold_params::embedding = perm,
  190. boyer_myrvold_params::kuratowski_subgraph
  191. = std::back_inserter(kuratowski_edges)
  192. )
  193. )
  194. {
  195. std::cerr << "Not planar. ";
  196. BOOST_REQUIRE(is_kuratowski_subgraph
  197. (g, kuratowski_edges.begin(), kuratowski_edges.end())
  198. );
  199. return 0;
  200. }
  201. // If we get this far, we have a connected planar graph.
  202. make_biconnected_planar(g, perm, get(edge_index, g), edge_updater);
  203. // Compute the planar embedding of the (now) biconnected planar graph
  204. BOOST_CHECK (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  205. boyer_myrvold_params::embedding
  206. = perm
  207. )
  208. );
  209. // If we get this far, we have a biconnected planar graph
  210. make_maximal_planar(g, perm, get(vertex_index,g), get(edge_index,g),
  211. edge_updater);
  212. // Now the graph is triangulated - we can compute the final planar embedding
  213. BOOST_CHECK (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  214. boyer_myrvold_params::embedding
  215. = perm
  216. )
  217. );
  218. // Make sure Euler's formula holds
  219. face_counter vis;
  220. planar_face_traversal(g, perm, vis, get(edge_index, g));
  221. BOOST_CHECK(num_vertices(g) - num_edges(g) + vis.num_faces() == 2);
  222. // Compute a planar canonical ordering of the vertices
  223. std::vector<vertex_t> ordering;
  224. planar_canonical_ordering(g, perm, std::back_inserter(ordering));
  225. BOOST_CHECK(ordering.size() == num_vertices(g));
  226. typedef std::vector< coord_t > drawing_storage_t;
  227. typedef boost::iterator_property_map
  228. < drawing_storage_t::iterator, property_map<graph, vertex_index_t>::type >
  229. drawing_map_t;
  230. drawing_storage_t drawing_vector(num_vertices(g));
  231. drawing_map_t drawing(drawing_vector.begin(), get(vertex_index,g));
  232. // Compute a straight line drawing
  233. chrobak_payne_straight_line_drawing(g,
  234. perm,
  235. ordering.begin(),
  236. ordering.end(),
  237. drawing
  238. );
  239. std::cerr << "Planar. ";
  240. BOOST_REQUIRE (is_straight_line_drawing(g, drawing));
  241. return 0;
  242. }
  243. int test_main(int argc, char* argv[])
  244. {
  245. std::string input_directory_str = "planar_input_graphs";
  246. if (argc > 1)
  247. {
  248. input_directory_str = std::string(argv[1]);
  249. }
  250. std::cout << "Reading planar input files from " << input_directory_str
  251. << std::endl;
  252. filesystem::path input_directory =
  253. filesystem::system_complete(filesystem::path(input_directory_str));
  254. const std::string dimacs_extension = ".dimacs";
  255. filesystem::directory_iterator dir_end;
  256. for( filesystem::directory_iterator dir_itr(input_directory);
  257. dir_itr != dir_end; ++dir_itr)
  258. {
  259. if (dir_itr->path().extension() != dimacs_extension)
  260. continue;
  261. std::cerr << "Testing " << dir_itr->path().leaf() << "... ";
  262. BOOST_REQUIRE (test_graph(dir_itr->path().string()) == 0);
  263. std::cerr << std::endl;
  264. }
  265. return 0;
  266. }