planar_canonical_ordering.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //=======================================================================
  2. // Copyright (c) Aaron Windsor 2007
  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. #ifndef __PLANAR_CANONICAL_ORDERING_HPP__
  9. #define __PLANAR_CANONICAL_ORDERING_HPP__
  10. #include <vector>
  11. #include <list>
  12. #include <boost/config.hpp>
  13. #include <boost/next_prior.hpp>
  14. #include <boost/graph/graph_traits.hpp>
  15. #include <boost/property_map/property_map.hpp>
  16. namespace boost
  17. {
  18. namespace detail
  19. {
  20. enum planar_canonical_ordering_state
  21. {
  22. PCO_PROCESSED,
  23. PCO_UNPROCESSED,
  24. PCO_ONE_NEIGHBOR_PROCESSED,
  25. PCO_READY_TO_BE_PROCESSED
  26. };
  27. }
  28. template < typename Graph, typename PlanarEmbedding, typename OutputIterator,
  29. typename VertexIndexMap >
  30. void planar_canonical_ordering(const Graph& g, PlanarEmbedding embedding,
  31. OutputIterator ordering, VertexIndexMap vm)
  32. {
  33. typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
  34. typedef typename graph_traits< Graph >::edge_descriptor edge_t;
  35. typedef
  36. typename graph_traits< Graph >::adjacency_iterator adjacency_iterator_t;
  37. typedef typename property_traits< PlanarEmbedding >::value_type
  38. embedding_value_t;
  39. typedef typename embedding_value_t::const_iterator embedding_iterator_t;
  40. typedef iterator_property_map< typename std::vector< vertex_t >::iterator,
  41. VertexIndexMap >
  42. vertex_to_vertex_map_t;
  43. typedef iterator_property_map<
  44. typename std::vector< std::size_t >::iterator, VertexIndexMap >
  45. vertex_to_size_t_map_t;
  46. std::vector< vertex_t > processed_neighbor_vector(num_vertices(g));
  47. vertex_to_vertex_map_t processed_neighbor(
  48. processed_neighbor_vector.begin(), vm);
  49. std::vector< std::size_t > status_vector(
  50. num_vertices(g), detail::PCO_UNPROCESSED);
  51. vertex_to_size_t_map_t status(status_vector.begin(), vm);
  52. std::list< vertex_t > ready_to_be_processed;
  53. vertex_t first_vertex = *vertices(g).first;
  54. vertex_t second_vertex = first_vertex;
  55. adjacency_iterator_t ai, ai_end;
  56. for (boost::tie(ai, ai_end) = adjacent_vertices(first_vertex, g);
  57. ai != ai_end; ++ai)
  58. {
  59. if (*ai == first_vertex)
  60. continue;
  61. second_vertex = *ai;
  62. break;
  63. }
  64. ready_to_be_processed.push_back(first_vertex);
  65. status[first_vertex] = detail::PCO_READY_TO_BE_PROCESSED;
  66. ready_to_be_processed.push_back(second_vertex);
  67. status[second_vertex] = detail::PCO_READY_TO_BE_PROCESSED;
  68. while (!ready_to_be_processed.empty())
  69. {
  70. vertex_t u = ready_to_be_processed.front();
  71. ready_to_be_processed.pop_front();
  72. if (status[u] != detail::PCO_READY_TO_BE_PROCESSED
  73. && u != second_vertex)
  74. continue;
  75. embedding_iterator_t ei, ei_start, ei_end;
  76. embedding_iterator_t next_edge_itr, prior_edge_itr;
  77. ei_start = embedding[u].begin();
  78. ei_end = embedding[u].end();
  79. prior_edge_itr = prior(ei_end);
  80. while (source(*prior_edge_itr, g) == target(*prior_edge_itr, g))
  81. prior_edge_itr = prior(prior_edge_itr);
  82. for (ei = ei_start; ei != ei_end; ++ei)
  83. {
  84. edge_t e(*ei); // e = (u,v)
  85. next_edge_itr
  86. = boost::next(ei) == ei_end ? ei_start : boost::next(ei);
  87. vertex_t v = source(e, g) == u ? target(e, g) : source(e, g);
  88. vertex_t prior_vertex = source(*prior_edge_itr, g) == u
  89. ? target(*prior_edge_itr, g)
  90. : source(*prior_edge_itr, g);
  91. vertex_t next_vertex = source(*next_edge_itr, g) == u
  92. ? target(*next_edge_itr, g)
  93. : source(*next_edge_itr, g);
  94. // Need prior_vertex, u, v, and next_vertex to all be
  95. // distinct. This is possible, since the input graph is
  96. // triangulated. It'll be true all the time in a simple
  97. // graph, but loops and parallel edges cause some complications.
  98. if (prior_vertex == v || prior_vertex == u)
  99. {
  100. prior_edge_itr = ei;
  101. continue;
  102. }
  103. // Skip any self-loops
  104. if (u == v)
  105. continue;
  106. // Move next_edge_itr (and next_vertex) forwards
  107. // past any loops or parallel edges
  108. while (next_vertex == v || next_vertex == u)
  109. {
  110. next_edge_itr = boost::next(next_edge_itr) == ei_end
  111. ? ei_start
  112. : boost::next(next_edge_itr);
  113. next_vertex = source(*next_edge_itr, g) == u
  114. ? target(*next_edge_itr, g)
  115. : source(*next_edge_itr, g);
  116. }
  117. if (status[v] == detail::PCO_UNPROCESSED)
  118. {
  119. status[v] = detail::PCO_ONE_NEIGHBOR_PROCESSED;
  120. processed_neighbor[v] = u;
  121. }
  122. else if (status[v] == detail::PCO_ONE_NEIGHBOR_PROCESSED)
  123. {
  124. vertex_t x = processed_neighbor[v];
  125. // are edges (v,u) and (v,x) adjacent in the planar
  126. // embedding? if so, set status[v] = 1. otherwise, set
  127. // status[v] = 2.
  128. if ((next_vertex == x
  129. && !(first_vertex == u && second_vertex == x))
  130. || (prior_vertex == x
  131. && !(first_vertex == x && second_vertex == u)))
  132. {
  133. status[v] = detail::PCO_READY_TO_BE_PROCESSED;
  134. }
  135. else
  136. {
  137. status[v] = detail::PCO_READY_TO_BE_PROCESSED + 1;
  138. }
  139. }
  140. else if (status[v] > detail::PCO_ONE_NEIGHBOR_PROCESSED)
  141. {
  142. // check the two edges before and after (v,u) in the planar
  143. // embedding, and update status[v] accordingly
  144. bool processed_before = false;
  145. if (status[prior_vertex] == detail::PCO_PROCESSED)
  146. processed_before = true;
  147. bool processed_after = false;
  148. if (status[next_vertex] == detail::PCO_PROCESSED)
  149. processed_after = true;
  150. if (!processed_before && !processed_after)
  151. ++status[v];
  152. else if (processed_before && processed_after)
  153. --status[v];
  154. }
  155. if (status[v] == detail::PCO_READY_TO_BE_PROCESSED)
  156. ready_to_be_processed.push_back(v);
  157. prior_edge_itr = ei;
  158. }
  159. status[u] = detail::PCO_PROCESSED;
  160. *ordering = u;
  161. ++ordering;
  162. }
  163. }
  164. template < typename Graph, typename PlanarEmbedding, typename OutputIterator >
  165. void planar_canonical_ordering(
  166. const Graph& g, PlanarEmbedding embedding, OutputIterator ordering)
  167. {
  168. planar_canonical_ordering(g, embedding, ordering, get(vertex_index, g));
  169. }
  170. } // namespace boost
  171. #endif //__PLANAR_CANONICAL_ORDERING_HPP__