MutableGraph.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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>MutableGraph</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. <H2><A NAME="sec:MutableGraph"></A>
  16. MutableGraph
  17. </H2>
  18. A MutableGraph can be changed via the addition or removal of
  19. edges and vertices.
  20. <H3>Refinement of</H3>
  21. <a href="./Graph.html">Graph</a>
  22. <h3>Notation</h3>
  23. <Table>
  24. <TR>
  25. <TD><tt>G</tt></TD>
  26. <TD>A type that is a model of Graph.</TD>
  27. </TR>
  28. <TR>
  29. <TD><tt>g</tt></TD>
  30. <TD>An object of type <tt>G</tt>.</TD>
  31. </TR>
  32. <TR>
  33. <TD><tt>e</tt></TD>
  34. <TD>An object of type <tt>boost::graph_traits&lt;G&gt;::edge_descriptor</tt>.</TD>
  35. </TR>
  36. <TR>
  37. <TD><tt>u,v</tt></TD>
  38. <TD>are objects of type <tt>boost::graph_traits&lt;G&gt;::vertex_descriptor</tt>.</TD>
  39. </TR>
  40. <TR>
  41. <TD><tt>iter</tt></TD>
  42. <TD>is an object of type <tt>boost::graph_traits&lt;G&gt;::out_edge_iterator</tt>.</TD>
  43. </TR>
  44. <TR>
  45. <TD><tt>p</tt></TD>
  46. <TD>is an object of a type that models <a
  47. href="http://www.boost.org/sgi/stl/Predicate.html">Predicate</a>
  48. and whose argument type matches the <tt>edge_descriptor</tt> type.
  49. </TR>
  50. </table>
  51. <H3>Valid Expressions</H3>
  52. <table border>
  53. <tr>
  54. <TD><a name="sec:add-edge"><TT>add_edge(u,&nbsp;v,&nbsp;g)</TT></a></TD>
  55. <TD>
  56. Inserts the edge <i>(u,v)</i> into the graph, and returns an edge
  57. descriptor pointing to the new edge. If the graph disallows parallel
  58. edges, and the edge <i>(u,v)</i> is already in the graph, then the
  59. <tt>bool</tt> flag returned is <tt>false</tt> and the returned edge
  60. descriptor points to the already existing edge. Note that for
  61. undirected graphs, <i>(u,v)</i> is the same edge as <i>(v,u)</i>, so
  62. after a call to the function <tt>add_edge()</tt>, this implies that
  63. edge <i>(u,v)</i> will appear in the out-edges of <i>u</i> and
  64. <i>(u,v)</i> (or equivalently <i>(v,u)</i>) will appear in the
  65. out-edges of <i>v</i>. Put another way, <i>v</i> will be adjacent to
  66. <i>u</i> and <i>u</i> will be adjacent to <i>v</i>.
  67. <br>
  68. Return type: <TT>std::pair&lt;edge_descriptor, bool&gt;</TT>
  69. </TD>
  70. </tr>
  71. <tr>
  72. <TD><a name="sec:remove_edge"><TT>remove_edge(u,&nbsp;v,&nbsp;g)</TT></a></TD>
  73. <TD>
  74. Remove the edge <i>(u,v)</i> from the graph. If the
  75. graph allows parallel edges this remove all occurrences of
  76. <i>(u,v)</i>.<br>
  77. Return type: <TT>void</TT><br>
  78. Precondition: <i>u</i> and <i>v</i> are vertices in the graph.<br>
  79. Postcondition: <i>(u,v)</i> is no longer in the edge set for
  80. <TT>g</TT>.<br>
  81. </TD>
  82. </TR>
  83. <tr>
  84. <TD><TT>remove_edge(e,&nbsp;g)</TT></TD>
  85. <TD>Remove the edge <i>e</i> from the graph.<br>
  86. Return type: <TT>void</TT><br>
  87. Precondition: <i>e</i> is an edge in the graph.<br>
  88. Postcondition: <i>e</i> is no longer in the edge set for <TT>g</TT>.
  89. </TD>
  90. </TR>
  91. <tr>
  92. <TD><TT>remove_edge(iter,&nbsp;g)</TT></TD>
  93. <TD>Remove the edge pointed to be <tt>iter</tt> from the graph. This
  94. expression is only required when the graph also models <a
  95. href="./IncidenceGraph.html">IncidenceGraph</a>.<br>
  96. Return type: <TT>void</TT><br>
  97. Precondition: <tt>*iter</tt> is an edge in the graph.<br>
  98. Postcondition: <tt>*iter</tt> is no longer in the edge set for <TT>g</TT>.
  99. </TD>
  100. </TR>
  101. <tr>
  102. <TD><TT>remove_edge_if(p,&nbsp;g)</TT></TD>
  103. <TD>Remove all the edges from graph <tt>g</tt> for which
  104. the predicate <tt>p</tt> returns true.<br>
  105. Return type: <TT>void</TT>
  106. </TD>
  107. </TR>
  108. <tr>
  109. <TD><TT>remove_out_edge_if(u,&nbsp;p,&nbsp;g)</TT></TD>
  110. <TD>Remove all the out-edges of vertex <tt>u</tt> for which the
  111. predicate <tt>p</tt> returns true. This expression is only required
  112. when the graph also models <a
  113. href="./IncidenceGraph.html">IncidenceGraph</a>.<br>
  114. Return type: <TT>void</TT>
  115. </TD>
  116. </TR>
  117. <tr>
  118. <TD><TT>remove_in_edge_if(u,&nbsp;p,&nbsp;g)</TT></TD>
  119. <TD>Remove all the in-edges of vertex <tt>u</tt> for which the
  120. predicate <tt>p</tt> returns true. This expression is only required when the
  121. graph also models <a
  122. href="./BidirectionalGraph.html">BidirectionalGraph</a>.<br>
  123. Return type: <TT>void</TT>
  124. </TD>
  125. </TR>
  126. <tr>
  127. <TD><a name="sec:add-vertex"><TT>add_vertex(g)</TT></a></TD>
  128. <TD>
  129. Add a new vertex to the graph. The <TT>vertex_descriptor</TT> for the
  130. new vertex is returned.<br>
  131. Return type: <TT>vertex_descriptor</TT>
  132. </TD>
  133. </TR>
  134. <tr>
  135. <TD><TT>clear_vertex(u,&nbsp;g)</TT></TD>
  136. <TD>
  137. Remove all edges to and from vertex <tt>u</tt> from the graph.<br>
  138. Return type: <TT>void</TT><br>
  139. Precondition: <tt>u</tt> is a valid vertex descriptor of <TT>g</TT>.<br>
  140. Postcondition: <tt>u</tt> does not appear as a source or target of
  141. any edge in <TT>g</TT>.
  142. </TD>
  143. </TR>
  144. <tr>
  145. <TD><a name="sec:remove-vertex"><TT>remove_vertex(u,&nbsp;g)</TT></a></TD>
  146. <TD>
  147. Remove <i>u</i> from the vertex set of the graph. Note that undefined
  148. behavior may result if there are edges remaining in the graph who's
  149. target is <i>u</i>. Typically the <TT>clear_vertex()</TT> function
  150. should be called first.<br>
  151. Return type: <TT>void</TT><br>
  152. Precondition: <TT>u</TT> is a valid vertex descriptor of <TT>g</TT>.<br>
  153. Postcondition: <TT>num_vertices(g)</TT> is one less, <TT>u</TT>
  154. no longer appears in the vertex set of the graph and it
  155. is no longer a valid vertex descriptor.
  156. </TD>
  157. </TR>
  158. </TABLE>
  159. <P>
  160. </LI>
  161. </UL>
  162. <P>
  163. <H3>Complexity Guarantees</H3>
  164. <P>
  165. <UL>
  166. <LI>Edge insertion must be either amortized constant time or it
  167. can be <i>O(log(E/V))</i> if the insertion also checks to
  168. prevent the addition of parallel edges (which is a ``feature'' of
  169. some graph types).
  170. </LI>
  171. <LI>Edge removal is guaranteed to be <i>O(E)</i>.</LI>
  172. <LI>Vertex insertion is guaranteed to be amortized constant time.</LI>
  173. <LI>Clearing a vertex is <i>O(E + V)</i>.</LI>
  174. <LI>Vertex removal is <i>O(E + V)</i>.</LI>
  175. </UL>
  176. <H3>Models</H3>
  177. <UL>
  178. <LI><TT>adjacency_list</TT>
  179. </LI>
  180. </UL>
  181. <H3>Concept Checking Class</H3>
  182. <PRE>
  183. template &lt;class G&gt;
  184. struct MutableGraphConcept
  185. {
  186. typedef typename boost::graph_traits&lt;G&gt;::edge_descriptor edge_descriptor;
  187. void constraints() {
  188. v = add_vertex(g);
  189. clear_vertex(v, g);
  190. remove_vertex(v, g);
  191. e_b = add_edge(u, v, g);
  192. remove_edge(u, v, g);
  193. remove_edge(e, g);
  194. }
  195. G g;
  196. edge_descriptor e;
  197. std::pair&lt;edge_descriptor, bool&gt; e_b;
  198. typename boost::graph_traits&lt;G&gt;::vertex_descriptor u, v;
  199. typename boost::graph_traits&lt;G&gt;::out_edge_iterator iter;
  200. };
  201. template &lt;class edge_descriptor&gt;
  202. struct dummy_edge_predicate {
  203. bool operator()(const edge_descriptor& e) const {
  204. return false;
  205. }
  206. };
  207. template &lt;class G&gt;
  208. struct MutableIncidenceGraphConcept
  209. {
  210. void constraints() {
  211. BOOST_CONCEPT_ASSERT(( MutableGraph&lt;G&gt; ));
  212. remove_edge(iter, g);
  213. remove_out_edge_if(u, p, g);
  214. }
  215. G g;
  216. typedef typename boost::graph_traits&lt;G&gt;::edge_descriptor edge_descriptor;
  217. dummy_edge_predicate&lt;edge_descriptor&gt; p;
  218. typename boost::graph_traits&lt;G&gt;::vertex_descriptor u;
  219. typename boost::graph_traits&lt;G&gt;::out_edge_iterator iter;
  220. };
  221. template &lt;class G&gt;
  222. struct MutableBidirectionalGraphConcept
  223. {
  224. void constraints() {
  225. BOOST_CONCEPT_ASSERT(( MutableIncidenceGraph&lt;G&gt; ));
  226. remove_in_edge_if(u, p, g);
  227. }
  228. G g;
  229. typedef typename boost::graph_traits&lt;G&gt;::edge_descriptor edge_descriptor;
  230. dummy_edge_predicate&lt;edge_descriptor&gt; p;
  231. typename boost::graph_traits&lt;G&gt;::vertex_descriptor u;
  232. };
  233. template &lt;class G&gt;
  234. struct MutableEdgeListGraphConcept
  235. {
  236. void constraints() {
  237. BOOST_CONCEPT_ASSERT(( MutableGraph&lt;G&gt; ));
  238. remove_edge_if(p, g);
  239. }
  240. G g;
  241. typedef typename boost::graph_traits&lt;G&gt;::edge_descriptor edge_descriptor;
  242. dummy_edge_predicate&lt;edge_descriptor&gt; p;
  243. };
  244. </PRE>
  245. <br>
  246. <HR>
  247. <TABLE>
  248. <TR valign=top>
  249. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  250. <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>)
  251. </TD></TR></TABLE>
  252. </BODY>
  253. </HTML>