| 1 | <HTML> |
|---|
| 2 | <!-- |
|---|
| 3 | -- Copyright (c) Jeremy Siek 2000 |
|---|
| 4 | -- |
|---|
| 5 | -- Permission to use, copy, modify, distribute and sell this software |
|---|
| 6 | -- and its documentation for any purpose is hereby granted without fee, |
|---|
| 7 | -- provided that the above copyright notice appears in all copies and |
|---|
| 8 | -- that both that copyright notice and this permission notice appear |
|---|
| 9 | -- in supporting documentation. Silicon Graphics makes no |
|---|
| 10 | -- representations about the suitability of this software for any |
|---|
| 11 | -- purpose. It is provided "as is" without express or implied warranty. |
|---|
| 12 | --> |
|---|
| 13 | <Head> |
|---|
| 14 | <Title>MutableGraph</Title> |
|---|
| 15 | <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" |
|---|
| 16 | ALINK="#ff0000"> |
|---|
| 17 | <IMG SRC="../../../boost.png" |
|---|
| 18 | ALT="C++ Boost" width="277" height="86"> |
|---|
| 19 | |
|---|
| 20 | <BR Clear> |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | <H2><A NAME="sec:MutableGraph"></A> |
|---|
| 24 | MutableGraph |
|---|
| 25 | </H2> |
|---|
| 26 | |
|---|
| 27 | A MutableGraph can be changed via the addition or removal of |
|---|
| 28 | edges and vertices. |
|---|
| 29 | |
|---|
| 30 | <H3>Refinement of</H3> |
|---|
| 31 | |
|---|
| 32 | <a href="./Graph.html">Graph</a> |
|---|
| 33 | |
|---|
| 34 | <h3>Notation</h3> |
|---|
| 35 | |
|---|
| 36 | <Table> |
|---|
| 37 | <TR> |
|---|
| 38 | <TD><tt>G</tt></TD> |
|---|
| 39 | <TD>A type that is a model of Graph.</TD> |
|---|
| 40 | </TR> |
|---|
| 41 | |
|---|
| 42 | <TR> |
|---|
| 43 | <TD><tt>g</tt></TD> |
|---|
| 44 | <TD>An object of type <tt>G</tt>.</TD> |
|---|
| 45 | </TR> |
|---|
| 46 | |
|---|
| 47 | <TR> |
|---|
| 48 | <TD><tt>e</tt></TD> |
|---|
| 49 | <TD>An object of type <tt>boost::graph_traits<G>::edge_descriptor</tt>.</TD> |
|---|
| 50 | </TR> |
|---|
| 51 | |
|---|
| 52 | <TR> |
|---|
| 53 | <TD><tt>u,v</tt></TD> |
|---|
| 54 | <TD>are objects of type <tt>boost::graph_traits<G>::vertex_descriptor</tt>.</TD> |
|---|
| 55 | </TR> |
|---|
| 56 | |
|---|
| 57 | <TR> |
|---|
| 58 | <TD><tt>iter</tt></TD> |
|---|
| 59 | <TD>is an object of type <tt>boost::graph_traits<G>::out_edge_iterator</tt>.</TD> |
|---|
| 60 | </TR> |
|---|
| 61 | |
|---|
| 62 | <TR> |
|---|
| 63 | <TD><tt>p</tt></TD> |
|---|
| 64 | <TD>is an object of a type that models <a |
|---|
| 65 | href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a> |
|---|
| 66 | and whose argument type matches the <tt>edge_descriptor</tt> type. |
|---|
| 67 | </TR> |
|---|
| 68 | |
|---|
| 69 | </table> |
|---|
| 70 | |
|---|
| 71 | <H3>Valid Expressions</H3> |
|---|
| 72 | |
|---|
| 73 | <table border> |
|---|
| 74 | |
|---|
| 75 | <tr> |
|---|
| 76 | <TD><a name="sec:add-edge"><TT>add_edge(u, v, g)</TT></a></TD> |
|---|
| 77 | <TD> |
|---|
| 78 | Inserts the edge <i>(u,v)</i> into the graph, and returns an edge |
|---|
| 79 | descriptor pointing to the new edge. If the graph disallows parallel |
|---|
| 80 | edges, and the edge <i>(u,v)</i> is already in the graph, then the |
|---|
| 81 | <tt>bool</tt> flag returned is <tt>false</tt> and the returned edge |
|---|
| 82 | descriptor points to the already existing edge. Note that for |
|---|
| 83 | undirected graphs, <i>(u,v)</i> is the same edge as <i>(v,u)</i>, so |
|---|
| 84 | after a call to the function <tt>add_edge()</tt>, this implies that |
|---|
| 85 | edge <i>(u,v)</i> will appear in the out-edges of <i>u</i> and |
|---|
| 86 | <i>(u,v)</i> (or equivalently <i>(v,u)</i>) will appear in the |
|---|
| 87 | out-edges of <i>v</i>. Put another way, <i>v</i> will be adjacent to |
|---|
| 88 | <i>u</i> and <i>u</i> will be adjacent to <i>v</i>. |
|---|
| 89 | <br> |
|---|
| 90 | Return type: <TT>std::pair<edge_descriptor, bool></TT> |
|---|
| 91 | </TD> |
|---|
| 92 | </tr> |
|---|
| 93 | |
|---|
| 94 | <tr> |
|---|
| 95 | <TD><a name="sec:remove_edge"><TT>remove_edge(u, v, g)</TT></a></TD> |
|---|
| 96 | <TD> |
|---|
| 97 | Remove the edge <i>(u,v)</i> from the graph. If the |
|---|
| 98 | graph allows parallel edges this remove all occurrences of |
|---|
| 99 | <i>(u,v)</i>.<br> |
|---|
| 100 | Return type: <TT>void</TT><br> |
|---|
| 101 | Precondition: <i>u</i> and <i>v</i> are vertices in the graph.<br> |
|---|
| 102 | Postcondition: <i>(u,v)</i> is no longer in the edge set for |
|---|
| 103 | <TT>g</TT>.<br> |
|---|
| 104 | </TD> |
|---|
| 105 | </TR> |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | <tr> |
|---|
| 109 | <TD><TT>remove_edge(e, g)</TT></TD> |
|---|
| 110 | <TD>Remove the edge <i>e</i> from the graph.<br> |
|---|
| 111 | Return type: <TT>void</TT><br> |
|---|
| 112 | Precondition: <i>e</i> is an edge in the graph.<br> |
|---|
| 113 | Postcondition: <i>e</i> is no longer in the edge set for <TT>g</TT>. |
|---|
| 114 | </TD> |
|---|
| 115 | </TR> |
|---|
| 116 | |
|---|
| 117 | <tr> |
|---|
| 118 | <TD><TT>remove_edge(iter, g)</TT></TD> |
|---|
| 119 | <TD>Remove the edge pointed to be <tt>iter</tt> from the graph. This |
|---|
| 120 | expression is only required when the graph also models <a |
|---|
| 121 | href="./IncidenceGraph.html">IncidenceGraph</a>.<br> |
|---|
| 122 | Return type: <TT>void</TT><br> |
|---|
| 123 | Precondition: <tt>*iter</tt> is an edge in the graph.<br> |
|---|
| 124 | Postcondition: <tt>*iter</tt> is no longer in the edge set for <TT>g</TT>. |
|---|
| 125 | </TD> |
|---|
| 126 | </TR> |
|---|
| 127 | |
|---|
| 128 | <tr> |
|---|
| 129 | <TD><TT>remove_edge_if(p, g)</TT></TD> |
|---|
| 130 | <TD>Remove all the edges from graph <tt>g</tt> for which |
|---|
| 131 | the predicate <tt>p</tt> returns true.<br> |
|---|
| 132 | Return type: <TT>void</TT> |
|---|
| 133 | </TD> |
|---|
| 134 | </TR> |
|---|
| 135 | |
|---|
| 136 | <tr> |
|---|
| 137 | <TD><TT>remove_out_edge_if(u, p, g)</TT></TD> |
|---|
| 138 | <TD>Remove all the out-edges of vertex <tt>u</tt> for which the |
|---|
| 139 | predicate <tt>p</tt> returns true. This expression is only required |
|---|
| 140 | when the graph also models <a |
|---|
| 141 | href="./IncidenceGraph.html">IncidenceGraph</a>.<br> |
|---|
| 142 | Return type: <TT>void</TT> |
|---|
| 143 | </TD> |
|---|
| 144 | </TR> |
|---|
| 145 | |
|---|
| 146 | <tr> |
|---|
| 147 | <TD><TT>remove_in_edge_if(u, p, g)</TT></TD> |
|---|
| 148 | <TD>Remove all the in-edges of vertex <tt>u</tt> for which the |
|---|
| 149 | predicate <tt>p</tt> returns true. This expression is only required when the |
|---|
| 150 | graph also models <a |
|---|
| 151 | href="./BidirectionalGraph.html">BidirectionalGraph</a>.<br> |
|---|
| 152 | Return type: <TT>void</TT> |
|---|
| 153 | </TD> |
|---|
| 154 | </TR> |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | <tr> |
|---|
| 158 | <TD><a name="sec:add-vertex"><TT>add_vertex(g)</TT></a></TD> |
|---|
| 159 | <TD> |
|---|
| 160 | Add a new vertex to the graph. The <TT>vertex_descriptor</TT> for the |
|---|
| 161 | new vertex is returned.<br> |
|---|
| 162 | Return type: <TT>vertex_descriptor</TT> |
|---|
| 163 | </TD> |
|---|
| 164 | </TR> |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | <tr> |
|---|
| 168 | <TD><TT>clear_vertex(u, g)</TT></TD> |
|---|
| 169 | <TD> |
|---|
| 170 | Remove all edges to and from vertex <tt>u</tt> from the graph.<br> |
|---|
| 171 | Return type: <TT>void</TT><br> |
|---|
| 172 | Precondition: <tt>u</tt> is a valid vertex descriptor of <TT>g</TT>.<br> |
|---|
| 173 | Postcondition: <tt>u</tt> does not appear as a source or target of |
|---|
| 174 | any edge in <TT>g</TT>. |
|---|
| 175 | </TD> |
|---|
| 176 | </TR> |
|---|
| 177 | |
|---|
| 178 | <tr> |
|---|
| 179 | <TD><a name="sec:remove-vertex"><TT>remove_vertex(u, g)</TT></a></TD> |
|---|
| 180 | <TD> |
|---|
| 181 | Remove <i>u</i> from the vertex set of the graph. Note that undefined |
|---|
| 182 | behavior may result if there are edges remaining in the graph who's |
|---|
| 183 | target is <i>u</i>. Typically the <TT>clear_vertex()</TT> function |
|---|
| 184 | should be called first.<br> |
|---|
| 185 | Return type: <TT>void</TT><br> |
|---|
| 186 | Precondition: <TT>u</TT> is a valid vertex descriptor of <TT>g</TT>.<br> |
|---|
| 187 | Postcondition: <TT>num_vertices(g)</TT> is one less, <TT>u</TT> |
|---|
| 188 | no longer appears in the vertex set of the graph and it |
|---|
| 189 | is no longer a valid vertex descriptor. |
|---|
| 190 | </TD> |
|---|
| 191 | </TR> |
|---|
| 192 | </TABLE> |
|---|
| 193 | |
|---|
| 194 | <P> |
|---|
| 195 | </LI> |
|---|
| 196 | </UL> |
|---|
| 197 | |
|---|
| 198 | <P> |
|---|
| 199 | |
|---|
| 200 | <H3>Complexity Guarantees</H3> |
|---|
| 201 | |
|---|
| 202 | <P> |
|---|
| 203 | |
|---|
| 204 | <UL> |
|---|
| 205 | <LI>Edge insertion must be either amortized constant time or it |
|---|
| 206 | can be <i>O(log(E/V))</i> if the insertion also checks to |
|---|
| 207 | prevent the addition of parallel edges (which is a ``feature'' of |
|---|
| 208 | some graph types). |
|---|
| 209 | </LI> |
|---|
| 210 | <LI>Edge removal is guaranteed to be <i>O(E)</i>.</LI> |
|---|
| 211 | <LI>Vertex insertion is guaranteed to be amortized constant time.</LI> |
|---|
| 212 | <LI>Clearing a vertex is <i>O(E + V)</i>.</LI> |
|---|
| 213 | <LI>Vertex removal is <i>O(E + V)</i>.</LI> |
|---|
| 214 | </UL> |
|---|
| 215 | |
|---|
| 216 | <H3>Models</H3> |
|---|
| 217 | |
|---|
| 218 | <UL> |
|---|
| 219 | <LI><TT>adjacency_list</TT> |
|---|
| 220 | </LI> |
|---|
| 221 | </UL> |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | <H3>Concept Checking Class</H3> |
|---|
| 225 | |
|---|
| 226 | <PRE> |
|---|
| 227 | template <class G> |
|---|
| 228 | struct MutableGraphConcept |
|---|
| 229 | { |
|---|
| 230 | typedef typename boost::graph_traits<G>::edge_descriptor edge_descriptor; |
|---|
| 231 | void constraints() { |
|---|
| 232 | v = add_vertex(g); |
|---|
| 233 | clear_vertex(v, g); |
|---|
| 234 | remove_vertex(v, g); |
|---|
| 235 | e_b = add_edge(u, v, g); |
|---|
| 236 | remove_edge(u, v, g); |
|---|
| 237 | remove_edge(e, g); |
|---|
| 238 | } |
|---|
| 239 | G g; |
|---|
| 240 | edge_descriptor e; |
|---|
| 241 | std::pair<edge_descriptor, bool> e_b; |
|---|
| 242 | typename boost::graph_traits<G>::vertex_descriptor u, v; |
|---|
| 243 | typename boost::graph_traits<G>::out_edge_iterator iter; |
|---|
| 244 | }; |
|---|
| 245 | |
|---|
| 246 | template <class edge_descriptor> |
|---|
| 247 | struct dummy_edge_predicate { |
|---|
| 248 | bool operator()(const edge_descriptor& e) const { |
|---|
| 249 | return false; |
|---|
| 250 | } |
|---|
| 251 | }; |
|---|
| 252 | |
|---|
| 253 | template <class G> |
|---|
| 254 | struct MutableIncidenceGraphConcept |
|---|
| 255 | { |
|---|
| 256 | void constraints() { |
|---|
| 257 | function_requires< MutableGraph<G> >(); |
|---|
| 258 | remove_edge(iter, g); |
|---|
| 259 | remove_out_edge_if(u, p, g); |
|---|
| 260 | } |
|---|
| 261 | G g; |
|---|
| 262 | typedef typename boost::graph_traits<G>::edge_descriptor edge_descriptor; |
|---|
| 263 | dummy_edge_predicate<edge_descriptor> p; |
|---|
| 264 | typename boost::graph_traits<G>::vertex_descriptor u; |
|---|
| 265 | typename boost::graph_traits<G>::out_edge_iterator iter; |
|---|
| 266 | }; |
|---|
| 267 | |
|---|
| 268 | template <class G> |
|---|
| 269 | struct MutableBidirectionalGraphConcept |
|---|
| 270 | { |
|---|
| 271 | void constraints() { |
|---|
| 272 | function_requires< MutableIncidenceGraph<G> >(); |
|---|
| 273 | remove_in_edge_if(u, p, g); |
|---|
| 274 | } |
|---|
| 275 | G g; |
|---|
| 276 | typedef typename boost::graph_traits<G>::edge_descriptor edge_descriptor; |
|---|
| 277 | dummy_edge_predicate<edge_descriptor> p; |
|---|
| 278 | typename boost::graph_traits<G>::vertex_descriptor u; |
|---|
| 279 | }; |
|---|
| 280 | |
|---|
| 281 | template <class G> |
|---|
| 282 | struct MutableEdgeListGraphConcept |
|---|
| 283 | { |
|---|
| 284 | void constraints() { |
|---|
| 285 | function_requires< MutableGraph<G> >(); |
|---|
| 286 | remove_edge_if(p, g); |
|---|
| 287 | } |
|---|
| 288 | G g; |
|---|
| 289 | typedef typename boost::graph_traits<G>::edge_descriptor edge_descriptor; |
|---|
| 290 | dummy_edge_predicate<edge_descriptor> p; |
|---|
| 291 | }; |
|---|
| 292 | </PRE> |
|---|
| 293 | |
|---|
| 294 | <br> |
|---|
| 295 | <HR> |
|---|
| 296 | <TABLE> |
|---|
| 297 | <TR valign=top> |
|---|
| 298 | <TD nowrap>Copyright © 2000-2001</TD><TD> |
|---|
| 299 | <A HREF="../../../people/jeremy_siek.htm">Jeremy Siek</A>, Indiana University (<A HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>) |
|---|
| 300 | </TD></TR></TABLE> |
|---|
| 301 | |
|---|
| 302 | </BODY> |
|---|
| 303 | </HTML> |
|---|