lengauer_tarjan_dominator.htm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <HTML>
  2. <!--
  3. Copyright (c) JongSoo Park 2005
  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. <Head>
  9. <Title>Boost Graph Library: Lengauer-Tarjan Dominator Tree Algorithm</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:lengauer-tarjan"></A>
  16. <TT>lengauer_tarjan_dominator_tree</TT>
  17. </H1>
  18. <P>
  19. <PRE>
  20. <i>// The simplest version:
  21. // Data structures for depth first search is created internally,
  22. // and depth first search runs internally.</i>
  23. template &lt;class Graph, class DomTreePredMap&gt;
  24. void
  25. lengauer_tarjan_dominator_tree
  26. (const Graph&amp; g,
  27. const typename graph_traits&lt;Graph&gt;::vertex_descriptor&amp; entry,
  28. DomTreePredMap domTreePredMap)
  29. <i>// The version providing data structures for depth first search:
  30. // After calling this function,
  31. // user can reuse the depth first search related information
  32. // filled in property maps.</i>
  33. template &lt;class Graph, class IndexMap, class TimeMap, class PredMap,
  34. class VertexVector, class DomTreePredMap&gt;
  35. void
  36. lengauer_tarjan_dominator_tree
  37. (const Graph&amp; g,
  38. const typename graph_traits&lt;Graph&gt;::vertex_descriptor&amp; entry,
  39. const IndexMap&amp; indexMap,
  40. TimeMap dfnumMap, PredMap parentMap, VertexVector&amp; verticesByDFNum,
  41. DomTreePredMap domTreePredMap)
  42. <i>// The version without depth first search:
  43. // The caller should provide depth first search related information
  44. // evaluated before.</i>
  45. template &lt;class Graph, class IndexMap, class TimeMap, class PredMap,
  46. class VertexVector, class DomTreePredMap&gt;
  47. void
  48. lengauer_tarjan_dominator_tree_without_dfs(
  49. (const Graph&amp; g,
  50. const typename graph_traits&lt;Graph&gt;::vertex_descriptor&amp; entry,
  51. const IndexMap&amp; indexMap,
  52. TimeMap dfnumMap, PredMap parentMap, VertexVector&amp; verticesByDFNum,
  53. DomTreePredMap domTreePredMap)
  54. </PRE>
  55. <P> This algorithm&nbsp;[<A
  56. HREF="bibliography.html#lengauer-tarjan79">65</A>,<A
  57. HREF="bibliography.html#muchnick97">66</A>,<A
  58. HREF="bibliography.html#appel98">67</A>] builds the dominator tree for
  59. directed graph. There are three options for dealing the depth first
  60. search related values. The simplest version creates data structures
  61. and run depth first search internally. However, chances are that a
  62. user wants to reuse the depth first search data, so we have two
  63. versions. </P>
  64. <P> A vertex <i>u</i> dominates a vertex <i>v</i>, if every path of
  65. directed graph from the entry to <i>v</i> must go through <i>u</i>. In
  66. the left graph of <a href="#fig:dominator-tree-example">Figure 1</a>,
  67. vertex 1 dominates vertex 2, 3, 4, 5, 6 and 7, because we have to pass
  68. vertex 1 to reach those vertex. Note that vertex 4 dominates vertex 6,
  69. even though vertex 4 is a successor of vertex 6. Dominator
  70. relationship is useful in many applications especially for compiler
  71. optimization. We can define the immediate dominator for each vertex
  72. such that <i>idom(n) dominates n</i> but does not dominate any other
  73. dominator of <i>n</i>. For example, vertex 1, 3 and 4 are dominators
  74. of vertex 6, but vertex 4 is the immediate dominator, because vertex 1
  75. and 3 dominates vertex 4. If we make every idom of each vertex as its
  76. parent, we can build the dominator tree like the right part of <a
  77. href="#fig:dominator-tree-example">Figure 1</a> </P>
  78. <P></P>
  79. <DIV ALIGN="CENTER"><A NAME="fig:dominator-tree-example">
  80. <TABLE>
  81. <CAPTION ALIGN="BOTTOM"><STRONG>Figure 1:</STRONG>
  82. An example graph and its dominator tree.</CAPTION>
  83. <TR>
  84. <TD><IMG SRC="./figs/dominator-tree1.gif"></TD>
  85. <TD>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
  86. <TD><IMG SRC="./figs/dominator-tree2.gif"></TD>
  87. </TR>
  88. </TABLE>
  89. </DIV><P></P>
  90. <P> An easy way to build dominator tree is to use iterative bit vector
  91. algorithm, but it may be slow in the worst case. We implemented
  92. Lengauer-Tarjan algorithm whose time complexity is
  93. <i>O((V+E)log(V+E))</i>. </P>
  94. <P> Lengauer-Tarjan algorithm utilizes two techniques. The first one
  95. is, as an intermediate step, finding semidominator which is relatively
  96. easier to evaluate than immediate dominator, and the second one is the
  97. path compression. For the detail of the algorithm, see [<A
  98. HREF="bibliography.html#lengauer-tarjan79">65</A>]. </P>
  99. <h3>Where Defined</h3>
  100. <a href="../../../boost/graph/dominator_tree.hpp"><tt>boost/graph/dominator_tree.hpp</tt></a>
  101. <h3>Parameters</h3>
  102. IN: <tt>const Graph&amp; g</tt>
  103. <blockquote>
  104. The graph object on which the algorithm will be applied.
  105. The type <tt>Graph</tt> must be a model of
  106. <a href="./VertexListGraph.html">Vertex List Graph</a>
  107. and <a href="./BidirectionalGraph.html">Bidirectional Graph</a>.<br>
  108. </blockquote>
  109. IN: <tt>vertex_descriptor entry</tt>
  110. <blockquote>
  111. The entry vertex. The dominator tree will be rooted at this vertex.
  112. </blockquote>
  113. IN: <tt>IndexMap indexMap</tt>
  114. <blockquote>
  115. This maps each vertex to an integer in the range <tt>[0, num_vertices(g))</tt>.
  116. The type
  117. <tt>VertexIndexMap</tt> must be a model of
  118. <a href="../../property_map/doc/ReadablePropertyMap.html">Readable Property Map</a>. The value type of the map must be an
  119. integer type. The vertex descriptor type of the graph needs to be
  120. usable as the key type of the map.
  121. </blockquote>
  122. IN/OUT: <tt>TimeMap dfnumMap</tt>
  123. <blockquote>
  124. The sequence number of depth first search. The type <tt>TimeMap</tt> must be a model of <a href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write Property Map</a>. The vertex descriptor type of the graph needs to be usable as the key type of the <tt>TimeMap</tt>.
  125. </blockquote>
  126. IN/OUT: <tt>PredMap parentMap</tt>
  127. <blockquote>
  128. The predecessor map records the parent of the depth first search tree. The <tt>PredMap</tt> type must be a <a href="../../property_map/doc/ReadWritePropertyMap.html">Read/Write Property Map</a> whose key and value types are the same as the vertex descriptor type of the graph.
  129. </blockquote>
  130. IN/OUT: <tt>VertexVector verticesByDFNum</tt>
  131. <blockquote>
  132. The vector containing vertices in depth first search order. If we access this vector sequentially, it's equivalent to access vertices by depth first search order.
  133. </blockquote>
  134. OUT: <tt>DomTreePredMap domTreePredMap</tt>
  135. <blockquote>
  136. The dominator tree where parents are each children's immediate dominator.
  137. </blockquote>
  138. <H3>Complexity</H3>
  139. <P>
  140. The time complexity is <i>O((V+E)log(V+E))</i>.
  141. <H3>Example</H3>
  142. <P>
  143. See <a href="../test/dominator_tree_test.cpp">
  144. <TT>test/dominator_tree_test.cpp</TT></a> for an example of using Dijkstra's
  145. algorithm.
  146. <br>
  147. <HR>
  148. <TABLE>
  149. <TR valign="top">
  150. <TD>Copyright &copy; 2005</TD><TD>
  151. JongSoo Park, Stanford University
  152. </TD></TR></TABLE>
  153. </BODY>
  154. </HTML>