metric_tsp_approx.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <HTML>
  2. <!--
  3. Copyright (c) Matyas Egyhazy 2008
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <Head>
  9. <Title>Boost Graph Library: Metric Traveling Salesperson Approximation</Title>
  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><A NAME="sec:metric_tsp_approx"></A>
  16. <TT>metric_tsp_approx</TT>
  17. </H1>
  18. <P>
  19. <PRE>
  20. template &lt;typename VertexListGraph,
  21. typename WeightMap,
  22. typename VertexIndexMap,
  23. typename TSPVertexVisitor&gt;
  24. void metric_tsp_approx_from_vertex(
  25. const VertexListGraph&amp; g,
  26. typename graph_traits&lt;VertexListGraph&gt;::vertex_descriptor start,
  27. WeightMap weightmap,
  28. VertexIndexMap indexmap,
  29. TSPVertexVisitor vis)
  30. <i>// Overloads</i>
  31. template&lt;typename VertexListGraph, typename OutputIterator&gt;
  32. void metric_tsp_approx_tour(VertexListGraph&amp; g, OutputIterator o)
  33. template&lt;typename VertexListGraph, typename WeightMap, typename OutputIterator&gt;
  34. void metric_tsp_approx_tour(VertexListGraph&amp; g, WeightMap w,
  35. OutputIterator o)
  36. template&lt;typename VertexListGraph, typename OutputIterator&gt;
  37. void metric_tsp_approx_tour_from_vertex(VertexListGraph&amp; g,
  38. typename graph_traits&lt;VertexListGraph&gt;::vertex_descriptor start,
  39. OutputIterator o)
  40. template&lt;typename VertexListGraph, typename WeightMap,
  41. typename OutputIterator&gt;
  42. void metric_tsp_approx_tour_from_vertex(VertexListGraph&amp; g,
  43. typename graph_traits&lt;VertexListGraph&gt;::vertex_descriptor start,
  44. WeightMap w, OutputIterator o)
  45. <i>// visitor versions</i>
  46. template&lt;typename VertexListGraph, typename TSPVertexVisitor&gt;
  47. void metric_tsp_approx(VertexListGraph&amp; g, TSPVertexVisitor vis)
  48. template&lt;typename VertexListGraph, typename Weightmap,
  49. typename VertexIndexMap, typename TSPVertexVisitor&gt;
  50. void metric_tsp_approx(VertexListGraph&amp; g, Weightmap w,
  51. TSPVertexVisitor vis)
  52. template&lt;typename VertexListGraph, typename WeightMap,
  53. typename VertexIndexMap, typename TSPVertexVisitor&gt;
  54. void metric_tsp_approx(VertexListGraph&amp; g, WeightMap w, VertexIndexMap id,
  55. TSPVertexVisitor vis)
  56. template&lt;typename VertexListGraph, typename WeightMap,
  57. typename TSPVertexVisitor&gt;
  58. void metric_tsp_approx_from_vertex(VertexListGraph&amp; g,
  59. typename graph_traits&lt;VertexListGraph&gt;::vertex_descriptor start,
  60. WeightMap w, TSPVertexVisitor vis)
  61. </PRE>
  62. <P>
  63. This is a traveling salesperson heuristic for generating a tour of vertices
  64. for a fully connected undirected graph with weighted edges that obey the triangle inequality.
  65. The current implementation is based off of the algorithm presented in <i>Introduction to Algorithms</i>
  66. on page 1029. This implementation generates solutions that are twice as long as the optimal tour in the worst case.
  67. The pseudo-code is listed below.
  68. </p>
  69. <table>
  70. <tr>
  71. <td valign="top">
  72. <pre>
  73. TSP-APPROX(<i>G</i>, <i>w</i>)
  74. select a vertex <i>r</i> <i>in</i> <i>V[G]</i>
  75. compute a minimum spanning tree <i>T</i> for <i>G</i> from root <i>r</i>
  76. using PRIM-MST(<i>G</i>, <i>r</i>, <i>w</i>)
  77. let <i>L</i> be the list of vertices visited in a preorder traversal of <i>T</i>
  78. <b>return</b> (the Hamiltonian cycle <i>H</i> that visites the vertices in the order <i>L</i>)
  79. </pre>
  80. </td>
  81. </tr>
  82. </table>
  83. <H3>Where Defined</H3>
  84. <P>
  85. <a href="../../../boost/graph/metric_tsp_approx.hpp"><TT>boost/graph/metric_tsp_approx.hpp</TT></a>
  86. <P>
  87. <h3>Parameters</h3>
  88. IN: <tt>const Graph&amp; g</tt>
  89. <blockquote>
  90. An undirected graph. The type <tt>Graph</tt> must be a
  91. model of <a href="./VertexListGraph.html">Vertex List Graph</a>
  92. and <a href="./IncidenceGraph.html">Incidence Graph</a> <a href="#2">[2]</a>.<br>
  93. </blockquote>
  94. IN: <tt>vertex_descriptor start</tt>
  95. <blockquote>
  96. The vertex that will be the start (and end) of the tour.<br>
  97. <b>Default:</b> <tt>*vertices(g).first</tt>
  98. </blockquote>
  99. IN: <tt>WeightMap weightmap</tt>
  100. <blockquote>
  101. The weight of each edge in the graph.
  102. The type <tt>WeightMap</tt> must be a model of
  103. <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The edge descriptor type of
  104. the graph needs to be usable as the key type for the weight
  105. map.<br>
  106. <b>Default:</b> <tt>get(edge_weight, g)</tt><br>
  107. </blockquote>
  108. IN: <tt>VertexIndexMap indexmap</tt>
  109. <blockquote>
  110. This maps each vertex to an integer in the range <tt>[0,
  111. num_vertices(g))</tt>. This is necessary for efficient updates of the
  112. heap data structure when an edge is relaxed. The type
  113. <tt>VertexIndexMap</tt> must be a model of
  114. <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The value type of the map must be an
  115. integer type. The vertex descriptor type of the graph needs to be
  116. usable as the key type of the map.<br>
  117. <b>Default:</b> <tt>get(vertex_index, g)</tt>
  118. Note: if you use this default, make sure your graph has
  119. an internal <tt>vertex_index</tt> property. For example,
  120. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  121. not have an internal <tt>vertex_index</tt> property.
  122. <br>
  123. </blockquote>
  124. OUT: <tt>OutputIterator o</tt>
  125. <blockquote>
  126. The OutputIterator holds the vertices of the tour.
  127. The type <tt>OutputIterator</tt> must be a model of the
  128. OutputIterator concept.
  129. </blockquote>
  130. OUT: <tt>TSPTourVisitor vis</tt>
  131. <blockquote>
  132. Use this to specify actions that you would like to happen
  133. when the algorithm visits a vertex of the tour.
  134. The type <tt>TSPTourVisitor</tt> must be a model of the <a href="./TSPTourVisitor.html">TSP Tour Visitor</a>
  135. concept.
  136. The visitor object is passed by value <a
  137. href="#1">[1]</a>.<br>
  138. </blockquote>
  139. <H3>Complexity</H3>
  140. <P>
  141. The time complexity is <i>O(E log V)</i>.
  142. <P>
  143. <H3>Example</H3>
  144. <P>
  145. The file <a
  146. href="../test/metric_tsp_approx.cpp"><TT>test/metric_tsp_approx.cpp</TT></a>
  147. contains an example of using this TSP approximation algorithm.
  148. <h3>Notes</h3>
  149. <p><a name="1">[1]</a>
  150. Since the visitor parameter is passed by value, if your visitor
  151. contains state then any changes to the state during the algorithm
  152. will be made to a copy of the visitor object, not the visitor object
  153. passed in. Therefore you may want the visitor to hold this state by
  154. pointer or reference.
  155. </p>
  156. <p><a name="2">[2]</a>
  157. Passing an <tt>adjacency_list</tt> with a vertex <em>not</em> set selected by
  158. <tt>vecS</tt> will result in <i>O(n<sup>2</sup>)</i> performance.
  159. </p>
  160. <HR>
  161. <TABLE>
  162. <TR valign=top>
  163. <TD nowrap>Copyright &copy; 2008</TD><TD>
  164. Matyas Egyhazy
  165. </TD></TR></TABLE>
  166. </BODY>
  167. </HTML>