filtered_graph.html 15 KB

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