planar_graphs.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <HTML>
  2. <!-- Copyright 2007 Aaron Windsor
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt)
  6. -->
  7. <HEAD>
  8. <TITLE>Boost Graph Library: Planar Graphs</TITLE>
  9. </HEAD>
  10. <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
  11. ALINK="#ff0000">
  12. <IMG SRC="../../../boost.png"
  13. ALT="C++ Boost" width="277" height="86">
  14. <BR Clear>
  15. <H1>Planar Graphs</H1>
  16. <p>
  17. A graph is <a name="planar"><i>planar</i></a> if it can be drawn in
  18. two-dimensional space with no two of its edges crossing. Such a drawing of a
  19. planar graph is called a <a name="plane_drawing"><i>plane drawing</i></a>.
  20. Every planar graph also admits a <i>straight-line drawing</i>, which is a
  21. plane drawing where each edge is represented by a line segment.
  22. <br>
  23. <br>
  24. <table class="image" align="center">
  25. <caption align="bottom">
  26. <h5>A planar graph (left), a plane drawing (center), and a straight line
  27. drawing (right), all of the same graph</h5>
  28. </caption>
  29. <tr>
  30. <td>
  31. <img src="./figs/planar_plane_straight_line.png">
  32. </td>
  33. </tr>
  34. <tr></tr>
  35. <table>
  36. <br>
  37. Two examples of non-planar graphs are K<sub>5</sub>, the complete graph on
  38. five vertices, and K<sub>3,3</sub>, the complete bipartite graph on six
  39. vertices with three vertices in each bipartition. No matter how the vertices
  40. of either graph are arranged in the plane, at least two edges are forced to
  41. cross.
  42. <a name = "kuratowskisubgraphs">
  43. <br>
  44. <br>
  45. <table class="image" align="center">
  46. <caption align="bottom"><h5>K<sub>5</sub> (left) and K<sub>3,3</sub> (right) -
  47. the two Kuratowski subgraphs</h5>
  48. </caption>
  49. <tr>
  50. <td>
  51. <img src="./figs/k_5_and_k_3_3.png">
  52. </td>
  53. </tr>
  54. </table>
  55. <br>
  56. The above graphs are both minimal examples of non-planarity within
  57. their class of graphs; delete any edge or vertex from either one and the
  58. resulting graph is planar. A theorem of Kuratowski singles these two graphs
  59. out as fundamental obstructions to planarity within any graph:
  60. <blockquote>
  61. <i>
  62. A graph is planar if and only if it does not contain a subgraph that is an
  63. expansion<a href="#1">[1]</a> of either K<sub>5</sub> or K<sub>3,3</sub>
  64. </i>
  65. </blockquote>
  66. <p>
  67. A subgraph that is an expansion of K<sub>5</sub> or K<sub>3,3</sub> is called
  68. a <a name = "kuratowski_subgraph"><i>Kuratowski subgraph</i></a>. Because of
  69. the above theorem, given any graph, one can produce either a plane drawing of
  70. a graph, which will certify that the graph is planar, or a minimal set of edges
  71. that forms a Kuratowski subgraph, which will certify that the graph is
  72. non-planar - in both cases, the certificate of planarity or non-planarity is
  73. easy to check.
  74. <p>
  75. Any plane drawing separates the plane into distinct regions bordered by graph
  76. edges called <i>faces</i>. As a simple example, any embedding of a triangle
  77. into the plane separates it into two faces: the region inside the triangle and
  78. the (unbounded) region outside the triangle. The unbounded region outside the
  79. graph's embedding is called the <i>outer face</i>. Every embedding yields
  80. one outer face and zero or more inner faces. A famous result called
  81. Euler's formula states that for any
  82. planar graph with <i>n</i> vertices, <i>e</i> edges, <i>f</i> faces, and
  83. <i>c</i> connected components,
  84. <a name="EulersFormula">
  85. <blockquote>
  86. <i>n + f = e + c + 1</i>
  87. </blockquote>
  88. </a>
  89. This formula implies that any planar graph with no self-loops or parallel edges
  90. has at most <i>3n - 6</i> edges and <i>2n- 4</i> faces. Because of these
  91. bounds, algorithms on planar graphs can run in time <i>O(n)</i> or space
  92. <i>O(n)</i> on an <i>n</i> vertex graph even if they have to traverse all
  93. edges or faces of the graph.
  94. <p>
  95. A convenient way to separate the actual planarity test from algorithms that
  96. accept a planar graph as input is through an intermediate structure called a
  97. <i>planar embedding</i>. Instead of specifying the absolute positions of the
  98. vertices and edges in the plane as a plane drawing would, a planar embedding
  99. specifies their positions relative to one another. A planar embedding consists
  100. of a sequence, for each vertex in the graph, of all of the edges incident on
  101. that vertex in the order in which they are to be drawn around that vertex.
  102. The orderings defined by this sequence
  103. can either represent a clockwise or counter-clockwise iteration through the
  104. neighbors of each vertex, but the orientation must be
  105. consistent across the entire embedding.
  106. <p>
  107. In the Boost Graph Library, a planar embedding is a model of the
  108. <a href="./PlanarEmbedding.html">PlanarEmbedding</a> concept. A type that
  109. models PlanarEmbedding can be passed into the planarity test and populated if
  110. the input graph is planar. All other "back end" planar graph algorithms accept
  111. this populated PlanarEmbedding as an input. Conceptually, a type that models
  112. PlanarEmbedding is a <a href="../../property_map/doc/property_map.html">property
  113. map</a> that maps each vertex to a sequence of edges,
  114. where the sequence of edges has a similar interface to a standard C++
  115. container. The sequence of edges each vertex maps to represents the ordering
  116. of edges adjacent to that vertex. This interface is flexible enough to allow
  117. storage of the planar embedding independent from the graph in, say, a
  118. <tt>std::vector</tt> of <tt>std::vector</tt>s, or to allow for graph
  119. implementations that actually store lists of adjacent edges/vertices to
  120. internally re-arrange their storage to represent the planar embedding.
  121. Currently, only the former approach is supported when using the native graph
  122. types (<tt>adjacency_list</tt>, <tt>adjacency_matrix</tt>, etc.)
  123. of the Boost Graph Library.
  124. <H3>Tools for working with planar graphs in the Boost Graph Library</h3>
  125. The Boost Graph Library planar graph algorithms all work on undirected graphs.
  126. Some algorithms require certain degrees of connectivity of the input graph,
  127. but all algorithms work on graphs with self-loops and parallel edges.
  128. <p>
  129. The function <tt><a href = "boyer_myrvold.html">boyer_myrvold_planarity_test
  130. </a></tt> can be used to test whether or not a graph is planar, but it can also
  131. produce two important side-effects: in the case the graph is not planar, it can
  132. isolate a Kuratowski subgraph, and in the case the graph is planar, it can
  133. compute a planar embedding. The Boyer-Myrvold algorithm works on any undirected
  134. graph.
  135. <p>
  136. An undirected graph is <i>connected</i> if, for any two vertices <i>u</i> and
  137. <i>v</i>, there's a path from <i>u</i> to <i>v</i>. An undirected graph is
  138. <i>biconnected</i> if it is connected and it remains connected even if any
  139. single vertex is removed. Finally, a planar graph is
  140. <i>maximal planar</i> (also called
  141. <i>triangulated</i>) if no additional edge (with the exception of self-loops
  142. and parallel edges) can be added to it without creating
  143. a non-planar graph. Any maximal planar simple graph on <i>n > 2</i> vertices
  144. has exactly <i>3n - 6</i> edges and <i>2n - 4</i> faces, a consequence of
  145. Euler's formula. If a planar graph isn't connected, isn't biconnected, or isn't
  146. maximal planar, there is some set of edges that can be added to the graph to
  147. make it satisfy any of those three properties while preserving planarity. Many
  148. planar graph drawing algorithms make at least one of these three assumptions
  149. about the input graph, so there are functions in the Boost Graph Library that
  150. can help:
  151. <ul>
  152. <li><tt><a href="make_connected.html">make_connected</a></tt> adds a minimal
  153. set of edges to an undirected graph to make it connected.
  154. <li><tt><a href="make_biconnected_planar.html">make_biconnected_planar</a></tt>
  155. adds a set of edges to a connected, undirected planar graph to make it
  156. biconnected while preserving planarity.
  157. <li><tt><a href="make_maximal_planar.html">make_maximal_planar</a></tt> adds a
  158. set of edges to a biconnected, undirected planar graph to make it maximal
  159. planar.
  160. </ul>
  161. <p>
  162. Some algorithms involve a traversal of the faces of the graph, and the Boost
  163. Graph Library has the generic traversal function
  164. <tt><a href="planar_face_traversal.html">planar_face_traversal</a></tt> for
  165. this purpose. This traversal, like other traversals in the Boost Graph Library,
  166. can be customized by overriding event points in an appropriately defined
  167. <a href="PlanarFaceVisitor.html">visitor class</a>.
  168. <p>
  169. An intermediate step in some drawing algorithms for planar graphs is the
  170. creation of
  171. a <i>canonical ordering</i> of the vertices. A canonical ordering is a
  172. permutation of the vertices of a maximal planar graph. It orders the vertices
  173. in a way that makes it straightforward to draw the <i>i</i>th vertex once the
  174. first <i>(i-1)</i> vertices have been drawn - the only edges connecting the
  175. <i>i</i>th vertex to vertices already drawn will be adjacent to a consecutive
  176. sequence of vertices along the outer face of the partially embedded graph. The
  177. function
  178. <tt><a href="planar_canonical_ordering.html">planar_canonical_ordering</a></tt>
  179. will create such an ordering, given a maximal planar graph and a planar
  180. embedding of that graph.
  181. <p>
  182. A straight line drawing can be created using the function
  183. <tt>
  184. <a href="straight_line_drawing.html">chrobak_payne_straight_line_drawing</a>,
  185. </tt> which takes a maximal planar graph, a planar embedding of that
  186. graph, and a canonical ordering as input. The resulting drawing maps all of the
  187. vertices from a graph with <i>n</i> vertices to integer coordinates on a
  188. <i>(2n-4) x (n-2)</i> grid such that when the edges of the graph are drawn
  189. as line segments connecting vertices, no two edges cross. Self-loops and
  190. parallel edges are ignored by this algorithm.
  191. <p>
  192. Finally, there are two functions that can be used to verify the results of the
  193. <tt>boyer_myrvold_planarity_test</tt> and
  194. <tt>chrobak_payne_straight_line_drawing</tt> functions:
  195. <ul>
  196. <li><tt><a href="is_kuratowski_subgraph.html">is_kuratowski_subgraph</a></tt>
  197. takes the output of <tt>boyer_myrvold_planarity_test</tt> on a nonplanar graph
  198. and verifies that it can be contracted into a graph isomorphic to a Kuratowski
  199. subgraph.
  200. <li><tt><a href="is_straight_line_drawing.html">is_straight_line_drawing</a>
  201. </tt> takes the output of <tt>chrobak_payne_straight_line_drawing</tt> and uses
  202. a planar sweep algorithm to verify that none of the embedded edges intersect.
  203. </ul>
  204. <h3>Complexity</h3>
  205. Most of the algorithms in the Boost Graph Library that deal with planar graphs
  206. run in time <i>O(n)</i> on an input graph with <i>n</i> vertices. This achieves
  207. a theoretically optimal bound (you must at least iterate over all <i>n</i>
  208. vertices in order to embed a graph in the plane.) However, some of the work
  209. that goes into achieving these theoretically optimal time bounds may come at
  210. the expense of practical performance. For example, since any comparison-based
  211. sorting algorithm uses at least on the order of <i>n log n</i> comparisons in
  212. the worst case, any time an algorithm dealing with planar graphs needs to sort,
  213. a bucket sort is used to sort in <i>O(n)</i> time. Also, computing a planar
  214. embedding of a graph involves maintaining an ordered list of edges around a
  215. vertex, and this list of edges needs to support an arbitrary sequence of
  216. concatenations and reversals. A <tt>std::list</tt> can only guarantee
  217. <i>O(n<sup>2</sup>)</i> for a mixed sequence of <i>n</i> concatenations and
  218. reversals (since <tt>reverse</tt> is an <i>O(n)</i> operation.) However, our
  219. implementation achieves <i>O(n)</i> for these operations by using a list data
  220. structure that implements mixed sequences of concatenations and reversals
  221. lazily.
  222. <p>
  223. In both of the above cases, it may be preferable to sacrifice the nice
  224. theoretical upper bound for performance by using the C++ STL. The bucket sort
  225. allocates and populates a vector of vectors; because of the overhead in
  226. doing so, <tt>std::stable_sort</tt> may actually be faster in some cases.
  227. The custom list also uses more space than <tt>std::list</tt>, and it's not
  228. clear that anything other than carefully constructed pathological examples
  229. could force a <tt>std::list</tt> to use <i>n<sup>2</sup></i> operations within
  230. the planar embedding algorithm. For these reasons, the macro
  231. <tt>BOOST_GRAPH_PREFER_STD_LIB</tt> exists, which, when defined, will force
  232. the planar graph algorithms to use <tt>std::stable_sort</tt> and
  233. <tt>std::list</tt> in the examples above.
  234. <p>
  235. See the documentation on individual algorithms for more information about
  236. complexity guarantees.
  237. <h3>Examples</h3>
  238. <ol>
  239. <li><a href="../example/simple_planarity_test.cpp">Testing whether or not a
  240. graph is planar.</a>
  241. <li><a href="../example/straight_line_drawing.cpp">Creating a straight line
  242. drawing of a graph in the plane.</a>
  243. </ol>
  244. <h3>Notes</h3>
  245. <p><a name="1">[1]</a> A graph <i>G'</i> is an expansion of a graph <i>G</i> if
  246. <i>G'</i> can be created from <i>G</i> by a series of zero or more <i>edge
  247. subdivisions</i>: take any edge <i>{x,y}</i> in the graph, remove it, add a new
  248. vertex <i>z</i>, and add the two edges <i>{x,z}</i> and <i>{z,y}</i> to the
  249. graph. For example, a path of any length is an expansion of a single edge and
  250. a cycle of any length is an expansion of a triangle.
  251. <br>
  252. <HR>
  253. Copyright &copy; 2007 Aaron Windsor (<a href="mailto:aaron.windsor@gmail.com">
  254. aaron.windsor@gmail.com</a>)
  255. </BODY>
  256. </HTML>