topological_sort.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <HTML>
  2. <!--
  3. Copyright (c) Jeremy Siek 2000
  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: Topological Sort</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:topological-sort">
  16. <img src="figs/python.gif" alt="(Python)"/>
  17. <TT>topological_sort</TT>
  18. </H1>
  19. <PRE>
  20. template &lt;typename VertexListGraph, typename OutputIterator,
  21. typename P, typename T, typename R&gt;
  22. void topological_sort(VertexListGraph&amp; g, OutputIterator result,
  23. const bgl_named_params&lt;P, T, R&gt;&amp; params = <i>all defaults</i>)
  24. </PRE>
  25. <P>
  26. The topological sort algorithm creates a linear ordering of the
  27. vertices such that if edge <i>(u,v)</i> appears in the graph, then
  28. <i>v</i> comes before <i>u</i> in the ordering. The graph must be a
  29. directed acyclic graph (DAG). The implementation consists mainly of a
  30. call to <a
  31. href="./depth_first_search.html"><tt>depth_first_search()</tt></a>.
  32. </p>
  33. <h3>Where Defined:</h3>
  34. <a href="../../../boost/graph/topological_sort.hpp"><TT>boost/graph/topological_sort.hpp</TT></a>
  35. <h3>Parameters</h3>
  36. IN: <tt>VertexListGraph&amp; g</tt>
  37. <blockquote>
  38. A directed acylic graph (DAG). The graph type must
  39. be a model of <a href="./VertexListGraph.html">Vertex List Graph</a>
  40. and <a href="./IncidenceGraph.html">Incidence Graph</a>.
  41. If the graph is not a DAG then a <a href="./exception.html#not_a_dag"><tt>not_a_dag</tt></a>
  42. exception will be thrown and the
  43. user should discard the contents of <tt>result</tt> range.<br>
  44. <b>Python</b>: The parameter is named <tt>graph</tt>.
  45. </blockquote>
  46. OUT: <tt>OutputIterator result</tt>
  47. <blockquote>
  48. The vertex descriptors of the graph will be output to the
  49. <TT>result</TT> output iterator in <b>reverse</b> topological order. The
  50. iterator type must model <a
  51. href="http://www.boost.org/sgi/stl/OutputIterator.html">Output
  52. Iterator</a>.<br>
  53. <b>Python</b>: This parameter is not used in Python. Instead, a
  54. Python <tt>list</tt> containing the vertices in topological order is
  55. returned.
  56. </blockquote>
  57. <h3>Named Parameters</h3>
  58. UTIL/OUT: <tt>color_map(ColorMap color)</tt>
  59. <blockquote>
  60. This is used by the algorithm to keep track of its progress through
  61. the graph. The type <tt>ColorMap</tt> must be a model of <a
  62. href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write
  63. Property Map</a> and its key type must be the graph's vertex
  64. descriptor type and the value type of the color map must model
  65. <a href="./ColorValue.html">ColorValue</a>.<br>
  66. <b>Default:</b> an <a
  67. href="../../property_map/doc/iterator_property_map.html">
  68. </tt>iterator_property_map</tt></a> created from a
  69. <tt>std::vector</tt> of <tt>default_color_type</tt> of size
  70. <tt>num_vertices(g)</tt> and using the <tt>i_map</tt> for the index
  71. map.<br>
  72. <b>Python</b>: The color map must be a <tt>vertex_color_map</tt> for
  73. the graph.
  74. </blockquote>
  75. IN: <tt>vertex_index_map(VertexIndexMap i_map)</tt>
  76. <blockquote>
  77. This maps each vertex to an integer in the range <tt>[0,
  78. num_vertices(g))</tt>. This parameter is only necessary when the
  79. default color property map is used. The type <tt>VertexIndexMap</tt>
  80. must be a model of <a
  81. href="../../property_map/doc/ReadablePropertyMap.html">Readable Property
  82. Map</a>. The value type of the map must be an integer type. The
  83. vertex descriptor type of the graph needs to be usable as the key
  84. type of the map.<br>
  85. <b>Default:</b> <tt>get(vertex_index, g)</tt>
  86. Note: if you use this default, make sure your graph has
  87. an internal <tt>vertex_index</tt> property. For example,
  88. <tt>adjacency_list</tt> with <tt>VertexList=listS</tt> does
  89. not have an internal <tt>vertex_index</tt> property.
  90. <br>
  91. <b>Python</b>: Unsupported parameter.
  92. </blockquote>
  93. <H3>Complexity</H3>
  94. The time complexity is <i>O(V + E)</i>.
  95. <H3>Example</H3>
  96. <P>
  97. Calculate a topological ordering of the vertices.
  98. <P>
  99. <PRE>
  100. typedef adjacency_list&lt; vecS, vecS, directedS, color_property&lt;&gt; &gt; Graph;
  101. typedef boost::graph_traits&lt;Graph&gt;::vertex_descriptor Vertex;
  102. Pair edges[6] = { Pair(0,1), Pair(2,4),
  103. Pair(2,5),
  104. Pair(0,3), Pair(1,4),
  105. Pair(4,3) };
  106. Graph G(6, edges, edges + 6);
  107. typedef std::vector&lt; Vertex &gt; container;
  108. container c;
  109. topological_sort(G, std::back_inserter(c));
  110. cout &lt;&lt; "A topological ordering: ";
  111. for ( container::reverse_iterator ii=c.rbegin(); ii!=c.rend(); ++ii)
  112. cout &lt;&lt; index(*ii) &lt;&lt; " ";
  113. cout &lt;&lt; endl;
  114. </PRE>
  115. The output is:
  116. <PRE>
  117. A topological ordering: 2 5 0 1 4 3
  118. </PRE>
  119. <br>
  120. <HR>
  121. <TABLE>
  122. <TR valign=top>
  123. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  124. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>, Indiana University (<A HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)
  125. </TD></TR></TABLE>
  126. </BODY>
  127. </HTML>