1<HTML> 2<!-- 3 Copyright (c) Jeremy Siek 2000 4 5 Distributed under the Boost Software License, Version 1.0. 6 (See accompanying file LICENSE_1_0.txt or copy at 7 http://www.boost.org/LICENSE_1_0.txt) 8 --> 9<Head> 10<Title>Boost Graph Library: Filtered Graph</Title> 11<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" 12 ALINK="#ff0000"> 13<IMG SRC="../../../boost.png" 14 ALT="C++ Boost" width="277" height="86"> 15 16<BR Clear> 17 18 19 20<H1><A NAME="sec:filtered-graph-class"></A> 21<pre> 22filtered_graph<Graph, EdgePredicate, VertexPredicate> 23</pre> 24</H1> 25 26 27<P> 28The <tt>filtered_graph</tt> class template is an adaptor that creates 29a filtered view of a graph. The predicate function objects determine 30which edges and vertices of the original graph will show up in the 31filtered graph. If the edge predicate returns <tt>true</tt> for an 32edge then it shows up in the filtered graph, and if the predicate 33returns <tt>false</tt> then the edge does not appear in the filtered 34graph. Likewise for vertices. The <tt>filtered_graph</tt> class does 35not create a copy of the original graph, but uses a reference to the 36original graph. The lifetime of the original graph must extend past 37any use of the filtered graph. The filtered graph does not change the 38structure of the original graph, though vertex and edge properties of 39the original graph can be changed through property maps of the 40filtered graph. Vertex and edge descriptors of the filtered graph are 41the same as, and interchangeable with, the vertex and edge descriptors 42of the original graph. 43 44<P>The <a href="#num_vertices"><tt>num_vertices</tt></a> and <a 45href="#num_edges"><tt>num_edges</tt></a> functions do not filter 46before returning results, so they return the number of vertices or 47edges in the underlying graph, unfiltered <a href="#2">[2]</a>. 48 49<H3>Example</H3> 50 51<P> 52In this example we will filter a graph's edges based on edge 53weight. We will keep all edges with positive edge weight. 54First, we create a predicate function object. 55 56<PRE> 57template <typename EdgeWeightMap> 58struct positive_edge_weight { 59 positive_edge_weight() { } 60 positive_edge_weight(EdgeWeightMap weight) : m_weight(weight) { } 61 template <typename Edge> 62 bool operator()(const Edge& e) const { 63 return 0 < get(m_weight, e); 64 } 65 EdgeWeightMap m_weight; 66}; 67</PRE> 68 69Now we create a graph and print out the filtered graph. 70<pre> 71int main() 72{ 73 using namespace boost; 74 75 typedef adjacency_list<vecS, vecS, directedS, 76 no_property, property<edge_weight_t, int> > Graph; 77 typedef property_map<Graph, edge_weight_t>::type EdgeWeightMap; 78 79 enum { A, B, C, D, E, N }; 80 const char* name = "ABCDE"; 81 Graph g(N); 82 add_edge(A, B, 2, g); 83 add_edge(A, C, 0, g); 84 add_edge(C, D, 1, g); 85 add_edge(C, E, 0, g); 86 add_edge(D, B, 3, g); 87 add_edge(E, C, 0, g); 88 89 positive_edge_weight<EdgeWeightMap> filter(get(edge_weight, g)); 90 filtered_graph<Graph, positive_edge_weight<EdgeWeightMap> > 91 fg(g, filter); 92 93 std::cout << "filtered edge set: "; 94 print_edges(fg, name); 95 96 std::cout << "filtered out-edges:" << std::endl; 97 print_graph(fg, name); 98 99 return 0; 100} 101</pre> 102The output is: 103<PRE> 104filtered edge set: (A,B) (C,D) (D,B) 105filtered out-edges: 106A --> B 107B --> 108C --> D 109D --> B 110E --> 111</PRE> 112 113<P> 114 115<H3>Template Parameters</H3> 116 117<P> 118<TABLE border> 119<TR> 120<th>Parameter</th><th>Description</th><th>Default</th> 121</tr> 122 123<TR><TD><TT>Graph</TT></TD> 124<TD>The underlying graph type.</TD> 125<TD> </TD> 126</TR> 127 128<TR> 129<TD><TT>EdgePredicate</TT></TD> <TD>A function object that selects 130which edges from the original graph will appear in the filtered 131graph. The function object must model <a 132href="http://www.boost.org/sgi/stl/Predicate.html">Predicate</a>. The 133argument type for the function object must be the edge descriptor type 134of the graph. Also, the predicate must be <a 135href="http://www.boost.org/sgi/stl/DefaultConstructible.html">Default Constructible</a> <a href="#1">[1]</a>.</TD> 136<TD> </TD> 137</TR> 138 139<TR> 140<TD><TT>VertexPredicate</TT></TD> 141<TD>A function object that selects 142which vertices from the original graph will appear in the filtered 143graph. The function object must model <a 144href="http://www.boost.org/sgi/stl/Predicate.html">Predicate</a>. The 145argument type for the function object must be the vertex descriptor type 146of the graph. Also, the predicate must be <a 147href="http://www.boost.org/sgi/stl/DefaultConstructible.html">Default Constructible</a> <a href="#1">[1]</a>.</TD> 148<TD><TT>keep_all</TT></TD> 149</TR> 150 151</TABLE> 152<P> 153 154<H3>Model of</H3> 155 156<P> 157This depends on the underlying graph type. If the underlying 158<tt>Graph</tt> type models <a 159href="./VertexAndEdgeListGraph.html">VertexAndEdgeListGraph</a> and <a 160href="./PropertyGraph.html">PropertyGraph</a> then so does the 161filtered graph. If the underlying <tt>Graph</tt> type models fewer or 162smaller concepts than these, then so does the filtered graph. 163 164<P> 165 166<H3>Where Defined</H3> 167 168<P> 169<a href="../../../boost/graph/filtered_graph.hpp"><TT>boost/graph/filtered_graph.hpp</TT></a> 170 171<P> 172 173<H2>Associated Types</H2> 174 175<hr> 176 177<tt>graph_traits<filtered_graph>::vertex_descriptor</tt> 178<br><br> 179 180The type for the vertex descriptors associated with the 181<TT>filtered_graph</TT>, which is the same type as the 182<tt>vertex_descriptor</tt> for the original <tt>Graph</tt>. 183 184 185<hr> 186 187<tt>graph_traits<filtered_graph>::edge_descriptor</tt><br> 188<br><br> 189The type for the edge descriptors associated with the 190<TT>filtered_graph</TT>, which is the same type as the 191<tt>edge_descriptor</tt> for the original <tt>Graph</tt>. 192 193<hr> 194 195<tt>graph_traits<filtered_graph>::vertex_iterator</tt><br> 196<br><br> 197The type for the iterators returned by <TT>vertices()</TT>, 198which is: 199<pre> 200<a href="../../iterator/doc/filter_iterator.html">filter_iterator</a><VertexPredicate, graph_traits<Graph>::vertex_iterator> 201</pre> 202The iterator is a model of <a href="../../utility/MultiPassInputIterator.html">MultiPassInputIterator</a>. 203 204 205<hr> 206 207<tt>graph_traits<filtered_graph>::edge_iterator</tt> 208<br><br> 209The type for the iterators returned by <TT>edges()</TT>, which is: 210<pre> 211<a href="../../iterator/doc/filter_iterator.html">filter_iterator</a><EdgePredicate, graph_traits<Graph>::edge_iterator> 212</pre> 213The iterator is a model of <a href="../../utility/MultiPassInputIterator.html">MultiPassInputIterator</a>. 214 215<hr> 216 217 218<tt>graph_traits<filtered_graph>::out_edge_iterator</tt> 219<br><br> 220The type for the iterators returned by <TT>out_edges()</TT>, which is: 221<pre> 222<a href="../../iterator/doc/filter_iterator.html">filter_iterator</a><EdgePredicate, graph_traits<Graph>::out_edge_iterator> 223</pre> 224The iterator is a model of <a href="../../utility/MultiPassInputIterator.html">MultiPassInputIterator</a>. 225 226 227<hr> 228 229<tt>graph_traits<filtered_graph>::adjacency_iterator</tt> 230<br><br> 231The type for the iterators returned by <TT>adjacent_vertices()</TT>. 232 233The <tt>adjacency_iterator</tt> models the same iterator concept as 234<tt>out_edge_iterator</tt>. 235 236<hr> 237 238<tt>graph_traits<filtered_graph>::directed_category</tt><br> 239<br><br> 240Provides information about whether the graph is directed 241(<TT>directed_tag</TT>) or undirected (<TT>undirected_tag</TT>). 242 243<hr> 244 245<tt>graph_traits<filtered_graph>::edge_parallel_category</tt><br> 246<br><br> 247This describes whether the graph class allows the insertion of 248parallel edges (edges with the same source and target). The two tags 249are <TT>allow_parallel_edge_tag</TT> and 250<TT>disallow_parallel_edge_tag</TT>. 251 252<hr> 253 254<tt>graph_traits<filtered_graph>::vertices_size_type</tt> 255<br><br> 256The type used for dealing with the number of vertices in the graph. 257 258<hr> 259 260<tt>graph_traits<filtered_graph>::edges_size_type</tt> 261<br><br> 262The type used for dealing with the number of edges in the graph. 263 264<hr> 265 266<tt>graph_traits<filtered_graph>::degree_size_type</tt> 267<br><br> 268The type used for dealing with the number of edges incident to a vertex 269in the graph. 270 271<hr> 272 273<tt>property_map<filtered_graph, Property>::type</tt><br> 274and<br> 275<tt>property_map<filtered_graph, Property>::const_type</tt> 276<br><br> 277The property map type for vertex or edge properties in the graph. 278The same property maps from the adapted graph are available 279in the filtered graph. 280 281<hr> 282 283<H2>Member Functions</H2> 284 285<hr> 286 287<pre> 288filtered_graph(Graph& g, EdgePredicate ep, VertexPredicate vp) 289</pre> 290Create a filtered graph based on the graph <i>g</i> and the 291edge filter <i>ep</i> and vertex filter <i>vp</i>. 292 293<hr> 294 295<pre> 296filtered_graph(Graph& g, EdgePredicate ep) 297</pre> 298Create a filtered graph based on the graph <i>g</i> and the 299edge filter <i>ep</i>. All vertices from the original graph 300are retained. 301 302<hr> 303 304filtered_graph(const filtered_graph& x) 305</pre> 306This creates a filtered graph for the same underlying graph 307as <i>x</i>. Anotherwords, this is a shallow copy. 308 309<hr> 310 311<pre> 312filtered_graph& operator=(const filtered_graph& x) 313</pre> 314This creates a filtered graph for the same underlying graph 315as <i>x</i>. Anotherwords, this is a shallow copy. 316 317<hr> 318 319<P> 320 321<H2>Non-Member Functions</H2> 322 323<h4>Structure Access</h4> 324 325<hr> 326 327<pre> 328std::pair<vertex_iterator, vertex_iterator> 329vertices(const filtered_graph& g) 330</pre> 331Returns an iterator-range providing access to the vertex set of graph 332<tt>g</tt>. 333 334<hr> 335 336<pre> 337std::pair<edge_iterator, edge_iterator> 338edges(const filtered_graph& g) 339</pre> 340Returns an iterator-range providing access to the edge set of graph 341<tt>g</tt>. 342 343<hr> 344 345<pre> 346std::pair<adjacency_iterator, adjacency_iterator> 347adjacent_vertices(vertex_descriptor u, const filtered_graph& g) 348</pre> 349Returns an iterator-range providing access to the vertices adjacent to 350vertex <tt>u</tt> in graph <tt>g</tt>. 351 352<hr> 353 354 355<pre> 356std::pair<out_edge_iterator, out_edge_iterator> 357out_edges(vertex_descriptor u, const filtered_graph& g) 358</pre> 359Returns an iterator-range providing access to the out-edges of vertex 360<tt>u</tt> in graph <tt>g</tt>. If the graph is undirected, this 361iterator-range provides access to all edges incident on vertex 362<tt>u</tt>. For both directed and undirected graphs, for an out-edge 363<tt>e</tt>, <tt>source(e, g) == u</tt> and <tt>target(e, g) == v</tt> 364where <tt>v</tt> is a vertex adjacent to <tt>u</tt>. 365 366<hr> 367 368<pre> 369std::pair<in_edge_iterator, in_edge_iterator> 370in_edges(vertex_descriptor v, const filtered_graph& g) 371</pre> 372Returns an iterator-range providing access to the in-edges of vertex 373<tt>v</tt> in graph <tt>g</tt>. For an in-edge <tt>e</tt>, 374<tt>target(e, g) == v</tt> and <tt>source(e, g) == u</tt> for some 375vertex <tt>u</tt> that is adjacent to <tt>v</tt>, whether the graph is 376directed or undirected. 377 378<hr> 379 380<pre> 381vertex_descriptor 382source(edge_descriptor e, const filtered_graph& g) 383</pre> 384Returns the source vertex of edge <tt>e</tt>. 385 386<hr> 387 388<pre> 389vertex_descriptor 390target(edge_descriptor e, const filtered_graph& g) 391</pre> 392Returns the target vertex of edge <tt>e</tt>. 393 394<hr> 395 396<pre> 397degree_size_type 398out_degree(vertex_descriptor u, const filtered_graph& g) 399</pre> 400Returns the number of edges leaving vertex <tt>u</tt>. 401 402<hr> 403 404<pre> 405degree_size_type 406in_degree(vertex_descriptor u, const filtered_graph& g) 407</pre> 408Returns the number of edges entering vertex <tt>u</tt>. 409 410<hr> 411 412<pre><a name="num_vertices"></a> 413vertices_size_type 414num_vertices(const filtered_graph& g) 415</pre> 416Returns the number of vertices in the underlying graph <a href="#2">[2]</a>. 417 418<hr> 419 420<pre><a name="num_edges"></a> 421edges_size_type 422num_edges(const filtered_graph& g) 423</pre> 424Returns the number of edges in the underlying graph <a href="#2">[2]</a>. 425 426<hr> 427 428<pre> 429std::pair<edge_descriptor, bool> 430edge(vertex_descriptor u, vertex_descriptor v, 431 const filtered_graph& g) 432</pre> 433Returns the edge connecting vertex <tt>u</tt> to vertex <tt>v</tt> in 434graph <tt>g</tt>. 435 436<hr> 437 438<pre> 439template <typename G, typename EP, typename VP> 440std::pair<out_edge_iterator, out_edge_iterator> 441edge_range(vertex_descriptor u, vertex_descriptor v, 442 const filtered_graph& g) 443</pre> 444Returns a pair of out-edge iterators that give the range for all the 445parallel edges from <tt>u</tt> to <tt>v</tt>. This function only works 446when the underlying graph supports <tt>edge_range</tt>, which requires 447that it sorts its out edges according to target vertex and allows 448parallel edges. The <tt>adjacency_list</tt> class with 449<tt>OutEdgeList=multisetS</tt> is an example of such a graph. 450 451 452<hr> 453 454<h4>Property Map Access</h4> 455 456<hr> 457 458<pre> 459template <class <a href="./PropertyTag.html">PropertyTag</a>> 460property_map<filtered_graph, PropertyTag>::type 461get(PropertyTag, filtered_graph& g) 462 463template <class <a href="./PropertyTag.html">PropertyTag</a>> 464property_map<filtered_graph, Tag>::const_type 465get(PropertyTag, const filtered_graph& g) 466</pre> 467Returns the property map object for the vertex property specified by 468<TT>PropertyTag</TT>. The <TT>PropertyTag</TT> must match one of the 469properties specified in the graph's <TT>VertexProperty</TT> template 470argument. 471 472<hr> 473 474<pre> 475template <class <a href="./PropertyTag.html">PropertyTag</a>, class X> 476typename property_traits<property_map<filtered_graph, PropertyTag>::const_type>::value_type 477get(PropertyTag, const filtered_graph& g, X x) 478</pre> 479This returns the property value for <tt>x</tt>, where <tt>x</tt> is either 480a vertex or edge descriptor. 481<hr> 482 483<pre> 484template <class <a href="./PropertyTag.html">PropertyTag</a>, class X, class Value> 485void 486put(PropertyTag, const filtered_graph& g, X x, const Value& value) 487</pre> 488This sets the property value for <tt>x</tt> to 489<tt>value</tt>. <tt>x</tt> is either a vertex or edge descriptor. 490<tt>Value</tt> must be convertible to 491<tt>typename property_traits<property_map<filtered_graph, PropertyTag>::type>::value_type</tt> 492 493<hr> 494 495<h3>See Also</h3> 496 497<a href="./property_map.html"><tt>property_map</tt></a>, 498<a href="./graph_traits.html"><tt>graph_traits</tt></a> 499 500<h3>Notes</h3> 501 502<p> 503<a name="1">[1]</a> The reason for requiring <a 504href="http://www.boost.org/sgi/stl/DefaultConstructible.html">Default 505Constructible</a> in the <tt>EdgePredicate</tt> and 506<tt>VertexPredicate</tt> types is that these predicates are stored 507by-value (for performance reasons) in the filter iterator adaptor, and 508iterators are required to be Default Constructible by the C++ 509Standard. 510 511<p> <a name="2">[2]</a> It would be nicer to return the number of 512vertices (or edges) remaining after the filter has been applied, but 513this has two problems. The first is that it would take longer to 514calculate, and the second is that it would interact badly with the 515underlying vertex/edge index mappings. The index mapping would no 516longer fall in the range <tt>[0,num_vertices(g))</tt> (resp. <tt>[0, 517num_edges(g))</tt>) which is assumed in many of the algorithms. 518 519 520<br> 521<HR> 522<TABLE> 523<TR valign=top> 524<TD nowrap>Copyright © 2000-2001</TD><TD> 525<A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>, 526Indiana University (<A 527HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br> 528<A HREF="http://www.boost.org/people/liequan_lee.htm">Lie-Quan Lee</A>, Indiana University (<A HREF="mailto:llee@cs.indiana.edu">llee@cs.indiana.edu</A>)<br> 529<A HREF="https://homes.cs.washington.edu/~al75">Andrew Lumsdaine</A>, 530Indiana University (<A 531HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>) 532</TD></TR></TABLE> 533 534</BODY> 535</HTML> 536