planar_canonical_ordering.hpp 7.1 KB

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