3 Copyright (c) Jeremy Siek 2000
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)
10 <Title>Boost Graph Library: Filtered Graph
</Title>
11 <BODY BGCOLOR=
"#ffffff" LINK=
"#0000ee" TEXT=
"#000000" VLINK=
"#551a8b"
13 <IMG SRC=
"../../../boost.png"
14 ALT=
"C++ Boost" width=
"277" height=
"86">
20 <H1><A NAME=
"sec:filtered-graph-class"></A>
22 filtered_graph
<Graph, EdgePredicate, VertexPredicate
>
28 The
<tt>filtered_graph
</tt> class template is an adaptor that creates
29 a filtered view of a graph. The predicate function objects determine
30 which edges and vertices of the original graph will show up in the
31 filtered graph. If the edge predicate returns
<tt>true
</tt> for an
32 edge then it shows up in the filtered graph, and if the predicate
33 returns
<tt>false
</tt> then the edge does not appear in the filtered
34 graph. Likewise for vertices. The
<tt>filtered_graph
</tt> class does
35 not create a copy of the original graph, but uses a reference to the
36 original graph. The lifetime of the original graph must extend past
37 any use of the filtered graph. The filtered graph does not change the
38 structure of the original graph, though vertex and edge properties of
39 the original graph can be changed through property maps of the
40 filtered graph. Vertex and edge descriptors of the filtered graph are
41 the same as, and interchangeable with, the vertex and edge descriptors
42 of the original graph.
44 <P>The
<a href=
"#num_vertices"><tt>num_vertices
</tt></a> and
<a
45 href=
"#num_edges"><tt>num_edges
</tt></a> functions do not filter
46 before returning results, so they return the number of vertices or
47 edges in the underlying graph, unfiltered
<a href=
"#2">[
2]
</a>.
52 In this example we will filter a graph's edges based on edge
53 weight. We will keep all edges with positive edge weight.
54 First, we create a predicate function object.
57 template
<typename EdgeWeightMap
>
58 struct 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);
65 EdgeWeightMap m_weight;
69 Now we create a graph and print out the filtered graph.
73 using namespace boost;
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;
79 enum { A, B, C, D, E, N };
80 const char* name =
"ABCDE";
89 positive_edge_weight
<EdgeWeightMap
> filter(get(edge_weight, g));
90 filtered_graph
<Graph, positive_edge_weight
<EdgeWeightMap
> >
93 std::cout
<< "filtered edge set: ";
94 print_edges(fg, name);
96 std::cout
<< "filtered out-edges:" << std::endl;
97 print_graph(fg, name);
104 filtered edge set: (A,B) (C,D) (D,B)
115 <H3>Template Parameters
</H3>
120 <th>Parameter
</th><th>Description
</th><th>Default
</th>
123 <TR><TD><TT>Graph
</TT></TD>
124 <TD>The underlying graph type.
</TD>
129 <TD><TT>EdgePredicate
</TT></TD> <TD>A function object that selects
130 which edges from the original graph will appear in the filtered
131 graph. The function object must model
<a
132 href=
"http://www.sgi.com/tech/stl/Predicate.html">Predicate
</a>. The
133 argument type for the function object must be the edge descriptor type
134 of the graph. Also, the predicate must be
<a
135 href=
"http://www.sgi.com/tech/stl/DefaultConstructible.html">Default Constructible
</a> <a href=
"#1">[
1]
</a>.
</TD>
140 <TD><TT>VertexPredicate
</TT></TD>
141 <TD>A function object that selects
142 which vertices from the original graph will appear in the filtered
143 graph. The function object must model
<a
144 href=
"http://www.sgi.com/tech/stl/Predicate.html">Predicate
</a>. The
145 argument type for the function object must be the vertex descriptor type
146 of the graph. Also, the predicate must be
<a
147 href=
"http://www.sgi.com/tech/stl/DefaultConstructible.html">Default Constructible
</a> <a href=
"#1">[
1]
</a>.
</TD>
148 <TD><TT>keep_all
</TT></TD>
157 This depends on the underlying graph type. If the underlying
158 <tt>Graph
</tt> type models
<a
159 href=
"./VertexAndEdgeListGraph.html">VertexAndEdgeListGraph
</a> and
<a
160 href=
"./PropertyGraph.html">PropertyGraph
</a> then so does the
161 filtered graph. If the underlying
<tt>Graph
</tt> type models fewer or
162 smaller concepts than these, then so does the filtered graph.
166 <H3>Where Defined
</H3>
169 <a href=
"../../../boost/graph/filtered_graph.hpp"><TT>boost/graph/filtered_graph.hpp
</TT></a>
173 <H2>Associated Types
</H2>
177 <tt>graph_traits
<filtered_graph
>::vertex_descriptor
</tt>
180 The 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>.
187 <tt>graph_traits
<filtered_graph
>::edge_descriptor
</tt><br>
189 The 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>.
195 <tt>graph_traits
<filtered_graph
>::vertex_iterator
</tt><br>
197 The type for the iterators returned by
<TT>vertices()
</TT>,
200 <a href=
"../../iterator/doc/filter_iterator.html">filter_iterator
</a><VertexPredicate, graph_traits
<Graph
>::vertex_iterator
>
202 The iterator is a model of
<a href=
"../../utility/MultiPassInputIterator.html">MultiPassInputIterator
</a>.
207 <tt>graph_traits
<filtered_graph
>::edge_iterator
</tt>
209 The type for the iterators returned by
<TT>edges()
</TT>, which is:
211 <a href=
"../../iterator/doc/filter_iterator.html">filter_iterator
</a><EdgePredicate, graph_traits
<Graph
>::edge_iterator
>
213 The iterator is a model of
<a href=
"../../utility/MultiPassInputIterator.html">MultiPassInputIterator
</a>.
218 <tt>graph_traits
<filtered_graph
>::out_edge_iterator
</tt>
220 The type for the iterators returned by
<TT>out_edges()
</TT>, which is:
222 <a href=
"../../iterator/doc/filter_iterator.html">filter_iterator
</a><EdgePredicate, graph_traits
<Graph
>::out_edge_iterator
>
224 The iterator is a model of
<a href=
"../../utility/MultiPassInputIterator.html">MultiPassInputIterator
</a>.
229 <tt>graph_traits
<filtered_graph
>::adjacency_iterator
</tt>
231 The type for the iterators returned by
<TT>adjacent_vertices()
</TT>.
233 The
<tt>adjacency_iterator
</tt> models the same iterator concept as
234 <tt>out_edge_iterator
</tt>.
238 <tt>graph_traits
<filtered_graph
>::directed_category
</tt><br>
240 Provides information about whether the graph is directed
241 (
<TT>directed_tag
</TT>) or undirected (
<TT>undirected_tag
</TT>).
245 <tt>graph_traits
<filtered_graph
>::edge_parallel_category
</tt><br>
247 This describes whether the graph class allows the insertion of
248 parallel edges (edges with the same source and target). The two tags
249 are
<TT>allow_parallel_edge_tag
</TT> and
250 <TT>disallow_parallel_edge_tag
</TT>.
254 <tt>graph_traits
<filtered_graph
>::vertices_size_type
</tt>
256 The type used for dealing with the number of vertices in the graph.
260 <tt>graph_traits
<filtered_graph
>::edges_size_type
</tt>
262 The type used for dealing with the number of edges in the graph.
266 <tt>graph_traits
<filtered_graph
>::degree_size_type
</tt>
268 The type used for dealing with the number of edges incident to a vertex
273 <tt>property_map
<filtered_graph, Property
>::type
</tt><br>
275 <tt>property_map
<filtered_graph, Property
>::const_type
</tt>
277 The property map type for vertex or edge properties in the graph.
278 The same property maps from the adapted graph are available
279 in the filtered graph.
283 <H2>Member Functions
</H2>
288 filtered_graph(Graph
& g, EdgePredicate
ep, VertexPredicate
vp)
290 Create a filtered graph based on the graph
<i>g
</i> and the
291 edge filter
<i>ep
</i> and vertex filter
<i>vp
</i>.
296 filtered_graph(Graph
& g, EdgePredicate
ep)
298 Create a filtered graph based on the graph
<i>g
</i> and the
299 edge filter
<i>ep
</i>. All vertices from the original graph
304 filtered_graph(const
filtered_graph
& x)
306 This creates a filtered graph for the same underlying graph
307 as
<i>x
</i>. Anotherwords, this is a shallow copy.
312 filtered_graph
& operator=(const
filtered_graph
& x)
314 This creates a filtered graph for the same underlying graph
315 as
<i>x
</i>. Anotherwords, this is a shallow copy.
321 <H2>Non-Member Functions
</H2>
323 <h4>Structure Access
</h4>
328 std::pair
<vertex_iterator,
vertex_iterator
>
329 vertices(const filtered_graph
& g)
331 Returns an iterator-range providing access to the vertex set of graph
337 std::pair
<edge_iterator,
edge_iterator
>
338 edges(const filtered_graph
& g)
340 Returns an iterator-range providing access to the edge set of graph
346 std::pair
<adjacency_iterator,
adjacency_iterator
>
347 adjacent_vertices(vertex_descriptor
u, const
filtered_graph
& g)
349 Returns an iterator-range providing access to the vertices adjacent to
350 vertex
<tt>u
</tt> in graph
<tt>g
</tt>.
356 std::pair
<out_edge_iterator,
out_edge_iterator
>
357 out_edges(vertex_descriptor
u, const
filtered_graph
& g)
359 Returns 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
361 iterator-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>
364 where
<tt>v
</tt> is a vertex adjacent to
<tt>u
</tt>.
369 std::pair
<in_edge_iterator,
in_edge_iterator
>
370 in_edges(vertex_descriptor
v, const
filtered_graph
& g)
372 Returns 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
375 vertex
<tt>u
</tt> that is adjacent to
<tt>v
</tt>, whether the graph is
376 directed or undirected.
382 source(edge_descriptor
e, const
filtered_graph
& g)
384 Returns the source vertex of edge
<tt>e
</tt>.
390 target(edge_descriptor
e, const
filtered_graph
& g)
392 Returns the target vertex of edge
<tt>e
</tt>.
398 out_degree(vertex_descriptor
u, const
filtered_graph
& g)
400 Returns the number of edges leaving vertex
<tt>u
</tt>.
406 in_degree(vertex_descriptor
u, const
filtered_graph
& g)
408 Returns the number of edges entering vertex
<tt>u
</tt>.
412 <pre><a name=
"num_vertices"></a>
414 num_vertices(const filtered_graph
& g)
416 Returns the number of vertices in the underlying graph
<a href=
"#2">[
2]
</a>.
420 <pre><a name=
"num_edges"></a>
422 num_edges(const filtered_graph
& g)
424 Returns the number of edges in the underlying graph
<a href=
"#2">[
2]
</a>.
429 std::pair
<edge_descriptor, bool
>
430 edge(vertex_descriptor
u, vertex_descriptor
v,
431 const
filtered_graph
& g)
433 Returns the edge connecting vertex
<tt>u
</tt> to vertex
<tt>v
</tt> in
439 template
<typename G, typename EP, typename VP
>
440 std::pair
<out_edge_iterator, out_edge_iterator
>
441 edge_range(vertex_descriptor u, vertex_descriptor v,
442 const filtered_graph
& g)
444 Returns a pair of out-edge iterators that give the range for all the
445 parallel edges from
<tt>u
</tt> to
<tt>v
</tt>. This function only works
446 when the underlying graph supports
<tt>edge_range
</tt>, which requires
447 that it sorts its out edges according to target vertex and allows
448 parallel edges. The
<tt>adjacency_list
</tt> class with
449 <tt>OutEdgeList=multisetS
</tt> is an example of such a graph.
454 <h4>Property Map Access
</h4>
459 template
<class
<a href=
"./PropertyTag.html">PropertyTag
</a>>
460 property_map
<filtered_graph, PropertyTag
>::type
461 get(PropertyTag, filtered_graph
& g)
463 template
<class
<a href=
"./PropertyTag.html">PropertyTag
</a>>
464 property_map
<filtered_graph, Tag
>::const_type
465 get(PropertyTag, const filtered_graph
& g)
467 Returns the property map object for the vertex property specified by
468 <TT>PropertyTag
</TT>. The
<TT>PropertyTag
</TT> must match one of the
469 properties specified in the graph's
<TT>VertexProperty
</TT> template
475 template
<class
<a href=
"./PropertyTag.html">PropertyTag
</a>, class X
>
476 typename property_traits
<property_map
<filtered_graph, PropertyTag
>::const_type>::value_type
477 get(PropertyTag, const filtered_graph
& g, X x)
479 This returns the property value for
<tt>x
</tt>, where
<tt>x
</tt> is either
480 a vertex or edge descriptor.
484 template
<class
<a href=
"./PropertyTag.html">PropertyTag
</a>, class X, class Value
>
486 put(PropertyTag, const filtered_graph
& g, X x, const Value& value)
488 This 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>
497 <a href=
"./property_map.html"><tt>property_map
</tt></a>,
498 <a href=
"./graph_traits.html"><tt>graph_traits
</tt></a>
503 <a name=
"1">[
1]
</a> The reason for requiring
<a
504 href=
"http://www.sgi.com/tech/stl/DefaultConstructible.html">Default
505 Constructible
</a> in the
<tt>EdgePredicate
</tt> and
506 <tt>VertexPredicate
</tt> types is that these predicates are stored
507 by-value (for performance reasons) in the filter iterator adaptor, and
508 iterators are required to be Default Constructible by the C++
511 <p> <a name=
"2">[
2]
</a> It would be nicer to return the number of
512 vertices (or edges) remaining after the filter has been applied, but
513 this has two problems. The first is that it would take longer to
514 calculate, and the second is that it would interact badly with the
515 underlying vertex/edge index mappings. The index mapping would no
516 longer fall in the range
<tt>[
0,num_vertices(g))
</tt> (resp.
<tt>[
0,
517 num_edges(g))
</tt>) which is assumed in many of the algorithms.
524 <TD nowrap
>Copyright
© 2000-
2001</TD><TD>
525 <A HREF=
"http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek
</A>,
526 Indiana University (
<A
527 HREF=
"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=
"http://www.osl.iu.edu/~lums">Andrew Lumsdaine
</A>,
530 Indiana University (
<A
531 HREF=
"mailto:lums@osl.iu.edu">lums@osl.iu.edu
</A>)