using_adjacency_list.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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>Using the Boost Graph Library</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="SECTION00830000000000000000"></A>
  16. <A NAME="sec:using-adjacency-list"></A>
  17. <BR>
  18. Using <TT>adjacency_list</TT>
  19. </H1>
  20. This section describes the details of how use the
  21. <tt>adjacency_list</tt> class. The presentation is divided into the
  22. following topics:
  23. <OL>
  24. <li><A href="#sec:choosing-graph-type">Choosing the <TT>Edgelist</TT> and <TT>VertexList</TT></A>
  25. <li><a href="#sec:directed-and-undirected">Directed and Undirected
  26. Adjacency Lists</a>
  27. <li><A href="#sec:adjacency-list-properties">Internal Properties</A>
  28. <li><A href="#sec:custom-storage">Customizing the Adjacency List Storage</A>
  29. </ol>
  30. <P>
  31. <H2><A NAME="SECTION00831000000000000000"></A>
  32. <A NAME="sec:choosing-graph-type"></A>
  33. <BR>
  34. Choosing the <TT>Edgelist</TT> and <TT>VertexList</TT>
  35. </H2>
  36. <P>
  37. This section focuses on how to decide which version of the <a
  38. href="./adjacency_list.html"><TT>adjacency_list</TT></a> class to use
  39. in different situations. The <TT>adjacency_list</TT> is like a
  40. swiss-army knife in that it can be configured in many ways. The
  41. parameters that we will focus on in this section are <TT>OutEdgeList</TT>
  42. and <TT>VertexList</TT>, which control the underlying data structures
  43. that will be used to represent the graph. The choice of
  44. <TT>OutEdgeList</TT> and <TT>VertexList</TT> affects the time complexity
  45. of many of the graph operations and the space complexity of the graph
  46. object.
  47. <P>
  48. BGL uses containers from the STL such as
  49. <a href="http://www.boost.org/sgi/stl/Vector.html"><TT>std::vector</TT></a>,
  50. <a href="http://www.boost.org/sgi/stl/List.html"><TT>std::list</TT></a>,
  51. and <a href="http://www.boost.org/sgi/stl/set.html"><TT>std::set</TT></a>
  52. to represent the set of vertices and the adjacency structure
  53. (out-edges and in-edges) of the graph. There are several selector
  54. types that are used to specify the choice of container for
  55. <TT>OutEdgeList</TT> and <TT>VertexList</TT>.
  56. <P>
  57. <UL>
  58. <LI><TT>vecS</TT> selects <TT>std::vector</TT>.</LI>
  59. <LI><TT>listS</TT> selects <TT>std::list</TT>.</LI>
  60. <LI><TT>slistS</TT> selects <TT>std::slist</TT>.</LI>
  61. <LI><TT>setS</TT> selects <TT>std::set</TT>.</LI>
  62. <LI><TT>multisetS</TT> selects <TT>std::multiset</TT>.</LI>
  63. <LI><TT>hash_setS</TT> selects <TT>boost::unordered_set</TT>.</LI>
  64. </UL>
  65. <P>
  66. <H3>Choosing the <TT>VertexList</TT> type</A></H3>
  67. <P>
  68. The <TT>VertexList</TT> parameter determines what kind of container
  69. will be used to represent the vertex set, or two-dimensional structure
  70. of the graph. The container must model <a
  71. href="http://www.boost.org/sgi/stl/Sequence.html">Sequence</a> or
  72. <a
  73. href="http://www.boost.org/sgi/stl/RandomAccessContainer.html">RandomAccessContainer</a>. In
  74. general, <TT>listS</TT> is a good choice if you need to add and remove
  75. vertices quickly. The price for this is extra space overhead compared
  76. to choosing <TT>vecS</TT>.
  77. <P>
  78. <H4>Space Complexity</H4>
  79. <P>
  80. The <TT>std::list</TT> has a higher per-vertex space overhead than the
  81. <TT>std::vector</TT>, storing three extra pointers per vertex.
  82. <P>
  83. <H4>Time Complexity</H4>
  84. <P>
  85. The choice of <TT>VertexList</TT> affects the time complexity of the
  86. following operations.
  87. <ul>
  88. <li>
  89. <pre>
  90. add_vertex()
  91. </PRE>
  92. This operation is amortized constant time for both <TT>vecS</TT> and
  93. <TT>listS</TT> (implemented with <TT>push_back()</TT>). However, when
  94. the <TT>VertexList</TT> type is <TT>vecS</TT> the time for this
  95. operation is occasionally large because the vector will be
  96. reallocated and the whole graph copied.
  97. <P></p>
  98. <li>
  99. <PRE>
  100. remove_vertex()
  101. </PRE>
  102. This operation is constant time for <TT>listS</TT> and <i>O(V + E)</i> for
  103. <TT>vecS</TT>. The large time complexity for <TT>vecS</TT> is because
  104. the vertex descriptors (which in this case are indices that correspond
  105. to the vertices' place in the vertex list) must be adjusted in the
  106. out-edges for the whole graph.
  107. <P></P>
  108. <li>
  109. <PRE>
  110. vertex()
  111. </PRE>
  112. This operation is constant time for <TT>vecS</TT> and for
  113. <TT>listS</TT>.
  114. </ul>
  115. <P>
  116. <H3><A NAME="SECTION00831200000000000000">
  117. Choosing the <TT>OutEdgeList</TT> type</A>
  118. </H3>
  119. <P>
  120. The <TT>OutEdgeList</TT> parameter determines what kind of container will
  121. be used to store the out-edges (and possibly in-edges) for each vertex
  122. in the graph. The containers used for edge lists must either satisfy
  123. the requirements for <a
  124. href="http://www.boost.org/sgi/stl/Sequence.html">Sequence</a> or for
  125. <a
  126. href="http://www.boost.org/sgi/stl/AssociativeContainer.html">AssociativeContainer</a>.
  127. <P>
  128. One of the first things to consider when choosing the
  129. <TT>OutEdgeList</TT> is whether you want <TT>adjacency_list</TT> to
  130. enforce the absence of parallel edges in the graph (that is, enforce
  131. that the graph not become a multi-graph). If you want this enforced
  132. then use the <TT>setS</TT> or <TT>hash_setS</TT> selectors. If you
  133. want to represent a multi-graph, or know that you will not be
  134. inserting parallel edges into the graph, then choose one of the <a
  135. href="http://www.boost.org/sgi/stl/Sequence.html">Sequence</a>
  136. types: <TT>vecS</TT>, <TT>listS</TT>, or <TT>slistS</TT>.
  137. You will also want to take into account the differences in time and space
  138. complexity for the various graph operations. Below we use <i>V</i> for
  139. the total number of vertices in the graph and <i>E</i> for the total
  140. number of edges. Operations not discussed here are constant time.
  141. <P>
  142. <H4>Space Complexity</H4>
  143. <P>
  144. The selection of the <TT>OutEdgeList</TT> affects the amount of space
  145. overhead per edge in the graph object. In the order of least space to
  146. most space, the selectors are <TT>vecS</TT>, <TT>slistS</TT>,
  147. <TT>listS</TT>, and <TT>setS</TT>.
  148. <P>
  149. <H4>Time Complexity</H4>
  150. <P>
  151. In the following description of the time complexity for various
  152. operations, we use <i>E/V</i> inside of the ``big-O'' notation to
  153. express the length of an out-edge list. Strictly speaking this is not
  154. accurate because <i>E/V</i> merely gives the average number of edges
  155. per vertex in a random graph. The worst-case number of out-edges for a
  156. vertex is <i>V</i> (unless it is a multi-graph). For sparse graphs
  157. <i>E/V</i> is typically much smaller than <i>V</i> and can be
  158. considered a constant.
  159. <P>
  160. <P> <P>
  161. <UL>
  162. <LI>
  163. <PRE>
  164. add_edge()
  165. </PRE>
  166. When the <TT>OutEdgeList</TT> is a <a
  167. href="http://www.boost.org/sgi/stl/UniqueAssociativeContainer.html">UniqueAssociativeContainer</a>
  168. like <TT>std::set</TT> the absence of parallel edges is enforced when
  169. an edge is added. The extra lookup involved has time complexity
  170. <i>O(log(E/V))</i>. The <TT>OutEdgeList</TT> types that model <a
  171. href="http://www.boost.org/sgi/stl/Sequence.html">Sequence</a> do
  172. not perform this check and therefore <TT>add_edge()</TT> is amortized
  173. constant time. This means that it if you don't care whether the graph
  174. has parallel edges, or know that the input to the graph does not
  175. contain them, then it is better to use the sequence-based
  176. <TT>OutEdgeList</TT>. The <TT>add_edge()</TT> for the sequence-based
  177. <TT>OutEdgeList</TT> is implemented with <TT>push_front()</TT> or
  178. <TT>push_back()</TT>. However, for <TT>std::list</TT> and
  179. <TT>std::slist</TT> this operation will typically be faster than with
  180. <TT>std::vector</TT> which occasionally reallocates and copies all
  181. elements.
  182. <p></p>
  183. <li>
  184. <PRE>
  185. remove_edge()
  186. </PRE>
  187. For sequence-based <TT>OutEdgeList</TT> types this operation is
  188. implemented with <TT>std::remove_if()</TT> which means the average
  189. time is <i>E/V</i>. For set-based <TT>OutEdgeList</TT> types this is
  190. implemented with the <TT>erase()</TT> member function, which has
  191. average time <i>log(E/V)</i>.
  192. <p></p>
  193. <li>
  194. <PRE>
  195. edge()
  196. </PRE>
  197. The time complexity for this operation is <i>O(E/V)</i> when the
  198. <TT>OutEdgeList</TT> type is a <a
  199. href="http://www.boost.org/sgi/stl/Sequence.html">Sequence</a> and it
  200. is <i>O(log(E/V))</i> when the <TT>OutEdgeList</TT> type is an <a
  201. href="http://www.boost.org/sgi/stl/AssociativeContainer.html">AssociativeContainer</a>.
  202. <p></p>
  203. <li>
  204. <PRE>
  205. clear_vertex()
  206. </PRE>
  207. For directed graphs with sequence-based <TT>OutEdgeList</TT> types the time
  208. complexity is <i>O(V + E)</i>, while for associative container based
  209. <TT>OutEdgeList</TT> types the operation is faster, with time complexity
  210. <i>O(V log(E/V))</i>. For undirected graphs this operation is
  211. <i>O(E<sup>2</sup>/V<sup>2</sup>)</i> or <i>O(E/V log(E/V))</i>.
  212. <p></p>
  213. <li>
  214. <PRE>
  215. remove_vertex()
  216. </PRE>
  217. The time complexity for this operation is <i>O(V + E)</i> regardless of the
  218. <TT>OutEdgeList</TT> type.
  219. <p></p>
  220. <li>
  221. <PRE>
  222. out_edge_iterator::operator++()
  223. </PRE>
  224. This operation is constant time for all the <TT>OneD</TT> types.
  225. However, there is a significant constant factor time difference
  226. between the various types, which is important since this operation is
  227. the work-horse of most graph algorithms. The speed of
  228. this operation in order of fastest to slowest is
  229. <TT>vecS</TT>, <TT>slistS</TT>, <TT>listS</TT>, <TT>setS</TT>,
  230. <TT>hash_setS</TT>.
  231. <p></p>
  232. <li>
  233. <PRE>
  234. in_edge_iterator::operator++()
  235. </PRE>
  236. This operation is constant time and exhibits a similar speed
  237. ordering as the <TT>out_edge_iterator</TT> with respect to
  238. the <TT>OutEdgeList</TT> selection.
  239. <p></p>
  240. <li>
  241. <PRE>
  242. vertex_iterator::operator++()
  243. </PRE>
  244. This operation is constant time and fast (same speed as incrementing a
  245. pointer). The selection of <TT>OneD</TT> does not affect the speed of
  246. this operation.
  247. <p></p>
  248. <li>
  249. <PRE>
  250. edge_iterator::operator++()
  251. </PRE>
  252. This operation is constant time and exhibits a similar speed ordering
  253. as the <TT>out_edge_iterator</TT> with respect to the <TT>OutEdgeList</TT>
  254. selection. Traversing through the whole edge set is <i>O(V + E)</i>.
  255. <p></p>
  256. <li>
  257. <PRE>
  258. adjacency_iterator::operator++()
  259. </PRE>
  260. This operation is constant time and exhibits a similar speed
  261. ordering as the <TT>out_edge_iterator</TT> with respect to
  262. the <TT>OutEdgeList</TT> selection.
  263. <p></p>
  264. </ul>
  265. <P>
  266. <P>
  267. <H2><a name="sec:directed-and-undirected">Directed and Undirected Adjacency Lists</H2>
  268. <P>
  269. The <TT>adjacency_list</TT> class can be used to represent both
  270. directed and undirected graphs, depending on the argument passed to
  271. the <TT>Directed</TT> template parameter. Selecting <TT>directedS</TT>
  272. or <TT>bidirectionalS</TT> choose a directed graph, whereas
  273. <TT>undirectedS</TT> selects the representation for an undirected
  274. graph. See Section <A
  275. HREF="graph_concepts.html#sec:undirected-graphs">Undirected Graphs</A>
  276. for a description of the difference between directed and undirected
  277. graphs in BGL. The <TT>bidirectionalS</TT> selector specifies that the
  278. graph will provide the <TT>in_edges()</TT> function as well as the
  279. <TT>out_edges()</TT> function. This imposes twice as much space
  280. overhead per edge, which is why <TT>in_edges()</TT> is optional.
  281. <P>
  282. <H2><A NAME="sec:adjacency-list-properties"></A>
  283. Internal Properties
  284. </H2>
  285. <P>
  286. Properties can be attached to the vertices or edges of an
  287. <TT>adjacency_list</TT> graph via the property interface. The template
  288. parameters <TT>VertexProperty</TT> and <TT>EdgeProperty</TT> of the
  289. <TT>adjacency_list</TT> class are meant to be filled by these interior
  290. properties.
  291. <p><b>NOTE</b>: The Boost Graph Library supports two interchangeable methods for
  292. specifying interior properties: <a href="bundles.html">bundled properties</a>
  293. and property lists. The former is easier to use and requires less effort,
  294. whereas the latter is compatible with older, broken compilers and is
  295. backward-compatible with Boost versions prior to 1.32.0. If you absolutely
  296. require these compatibility features, read on to learn about property lists.
  297. Otherwise, we strongly suggest that you read about the <a href="bundles.html">bundled
  298. properties</a> mechanism.
  299. <p>One may specify internal properties via property lists, which are build from instances of the
  300. property class declared as follows.
  301. <P>
  302. <PRE>
  303. template &lt;class PropertyTag, class T, class NextProperty = no_property&gt;
  304. struct property;
  305. </PRE>
  306. <P>
  307. The <a href="./PropertyTag.html"><TT>PropertyTag</TT></a> template
  308. parameter is a tag class that simply identifies or gives a unique name
  309. to the property. There are several predefined tags, and it is easy to
  310. add more.
  311. <P>
  312. <PRE>
  313. struct vertex_index_t { };
  314. struct vertex_index1_t { };
  315. struct vertex_index2_t { };
  316. struct edge_index_t { };
  317. struct graph_name_t { };
  318. struct vertex_name_t { };
  319. struct edge_name_t { };
  320. struct edge_weight_t { };
  321. struct edge_weight2_t { };
  322. struct edge_capacity_t { };
  323. struct edge_residual_capacity_t { };
  324. struct edge_reverse_t { };
  325. struct vertex_distance_t { };
  326. struct vertex_root_t { };
  327. struct vertex_all_t { };
  328. struct edge_all_t { };
  329. struct graph_all_t { };
  330. struct vertex_color_t { };
  331. struct vertex_rank_t { };
  332. struct vertex_predecessor_t { };
  333. struct vertex_isomorphism_t { };
  334. struct vertex_invariant_t { };
  335. struct vertex_invariant1_t { };
  336. struct vertex_invariant2_t { };
  337. struct vertex_degree_t { };
  338. struct vertex_out_degree_t { };
  339. struct vertex_in_degree_t { };
  340. struct vertex_discover_time_t { };
  341. struct vertex_finish_time_t { };
  342. </PRE>
  343. <P>
  344. The <b><TT>T</TT></b> template parameter of <TT>property</TT>
  345. specifies the type of the property values. The type <tt>T</tt> must be
  346. <a
  347. href="http://www.boost.org/sgi/stl/DefaultConstructible.html">Default
  348. Constructible</a>, <a
  349. href="../../utility/Assignable.html">Assignable</a>, and <a
  350. href="../../utility/CopyConstructible.html">Copy Constructible</a>.
  351. Like the containers of the C++ Standard Library, the property objects
  352. of type <tt>T</tt> are held by-value inside of the graph.
  353. <p>
  354. The <b><TT>NextProperty</TT></b> parameter allows <TT>property</TT>
  355. types to be nested, so that an arbitrary number of properties can be
  356. attached to the same graph.
  357. <P>
  358. The following code shows how a vertex and edge property type can be
  359. assembled and used to create a graph type. We have attached a distance
  360. property with values of type <TT>float</TT> and a name property with
  361. values of type <TT>std::string</TT> to the vertices of the graph. We
  362. have attached a weight property with values of type <TT>float</TT> to
  363. the edges of the graph.
  364. <P>
  365. <PRE>
  366. typedef property&lt;vertex_distance_t, float,
  367. property&lt;vertex_name_t, std::string&gt; &gt; VertexProperty;
  368. typedef property&lt;edge_weight_t, float&gt; EdgeProperty;
  369. typedef adjacency_list&lt;mapS, vecS, undirectedS,
  370. VertexProperty, EdgeProperty&gt; Graph;
  371. Graph g(num_vertices); // construct a graph object
  372. </PRE>
  373. <P>
  374. The property values are then read from and written to using property
  375. maps. See Section <A HREF="using_property_maps.html#sec:interior-properties">Interior
  376. Properties</A> for a description of how to obtain property maps
  377. from a graph, and read Section <A
  378. HREF="./using_property_maps.html">Property Maps</A> for how
  379. to use property maps.
  380. <P>
  381. <H3><A NAME="sec:custom-edge-properties"></A>
  382. Custom Edge Properties
  383. </H3>
  384. <P>
  385. Creating your own property types and properties is easy; just define
  386. a tag class for your new property. The property tag class will need to
  387. define <tt>num</tt> with a unique integer ID, and <tt>kind</tt> which
  388. should be either <tt>edge_property_tag</tt>,
  389. <tt>vertex_property_tag</tt>, or <tt>graph_property_tag</tt>.
  390. <P>
  391. <PRE>
  392. struct flow_t {
  393. typedef edge_property_tag kind;
  394. };
  395. struct capacity_t {
  396. typedef edge_property_tag kind;
  397. };
  398. </PRE>
  399. <p>
  400. You can also use enum's instead of struct's to create tag types. Create an enum
  401. type for each property inside the boost namespace. The first part of the name of
  402. the enum type must be <tt>edge</tt>, <tt>vertex</tt>, or <tt>graph</tt> followed
  403. by an underscore, the new property name, and a <tt>_t</tt> at the end. Inside
  404. the enum, define a value with the same name minus the <tt>_t</tt>. Then invoke
  405. the <tt>BOOST_INSTALL_PROPERTY</tt> macro.
  406. <pre>
  407. namespace boost {
  408. enum edge_flow_t { edge_flow };
  409. enum edge_capacity_t { edge_capacity };
  410. BOOST_INSTALL_PROPERTY(edge, flow);
  411. BOOST_INSTALL_PROPERTY(edge, capacity);
  412. }
  413. </pre>
  414. <P>
  415. Now you can use your new property tag in the definition of properties just as
  416. you would one of the builtin tags.
  417. <P>
  418. <PRE>
  419. typedef property&lt;capacity_t, int&gt; Cap;
  420. typedef property&lt;flow_t, int, Cap&gt; EdgeProperty;
  421. typedef adjacency_list&lt;vecS, vecS, no_property, EdgeProperty&gt; Graph;
  422. </PRE>
  423. <P>
  424. Just as before, the property maps for these properties can be
  425. obtained from the graph via the
  426. <TT>get(Property, g)</TT> function.
  427. <P>
  428. <PRE>
  429. property_map&lt;Graph, capacity_t&gt;::type capacity
  430. = get(capacity_t(), G);
  431. property_map&lt;Graph, flow_t&gt;::type flow
  432. = get(flow_t(), G);
  433. </PRE>
  434. <P>
  435. The file <TT>edge_property.cpp</TT> shows the complete source
  436. code for this example.
  437. <P>
  438. <H3><A NAME="SECTION00833200000000000000"></A>
  439. <A NAME="sec:custom-vertex-properties"></A>
  440. <BR>
  441. Custom Vertex Properties
  442. </H3>
  443. <P>
  444. Creating your own properties to attach to vertices is just as easy as
  445. for edges. Here we want to attach people's first names to the vertices
  446. in the graph.
  447. <P>
  448. <PRE>
  449. struct first_name_t {
  450. typedef vertex_property_tag kind;
  451. };
  452. </PRE>
  453. <P>
  454. Now we can use the new tag in the <TT>property</TT> class and use that in
  455. the assembly of a graph type. The following code shows creating the
  456. graph type, and then creating the graph object. We fill in the edges
  457. and also assign names to the vertices. The edges will represent ``who
  458. owes who''.
  459. <P>
  460. <PRE>
  461. typedef property&lt;first_name_t, std::string&gt; FirstNameProperty;
  462. typedef adjacency_list&lt;vecS, vecS, directedS,
  463. FirstNameProperty&gt; MyGraphType;
  464. typedef pair&lt;int,int&gt; Pair;
  465. Pair edge_array[11] = { Pair(0,1), Pair(0,2), Pair(0,3),
  466. Pair(0,4), Pair(2,0), Pair(3,0),
  467. Pair(2,4), Pair(3,1), Pair(3,4),
  468. Pair(4,0), Pair(4,1) };
  469. MyGraphType G(5);
  470. for (int i = 0; i &lt; 11; ++i)
  471. add_edge(edge_array[i].first, edge_array[i].second, G);
  472. property_map&lt;MyGraphType, first_name_t&gt;::type
  473. name = get(first_name_t(), G);
  474. boost::put(name, 0, "Jeremy");
  475. boost::put(name, 1, "Rich");
  476. boost::put(name, 2, "Andrew");
  477. boost::put(name, 3, "Jeff");
  478. name[4] = "Kinis"; // you can use operator[] too
  479. who_owes_who(edges(G).first, edges(G).second, G);
  480. </PRE>
  481. <P>
  482. The <TT>who_owes_who()</TT> function written for this example was
  483. implemented in a generic style. The input is templated so we do not
  484. know the actual graph type. To find out the type of the property
  485. map for our first-name property, we need to use the
  486. <TT>property_map</TT> traits class. The <TT>const_type</TT>
  487. is used since the graph parameter is const. Once we have the property
  488. map type, we can deduce the value type of the property using the
  489. <TT>property_traits</TT> class. In this example, we know that the
  490. property's value type will be <TT>std::string</TT>, but written in this
  491. generic fashion the <TT>who_owes_who()</TT> function could work with
  492. other property value types.
  493. <P>
  494. <PRE>
  495. template &lt;class EdgeIter, class Graph&gt;
  496. void who_owes_who(EdgeIter first, EdgeIter last, const Graph&amp; G)
  497. {
  498. // Access the propety acessor type for this graph
  499. typedef typename property_map&lt;Graph,
  500. first_name_t&gt;::const_type NameMap;
  501. NameMap name = get(first_name, G);
  502. typedef typename boost::property_traits&lt;NameMap&gt;
  503. ::value_type NameType;
  504. NameType src_name, targ_name;
  505. while (first != last) {
  506. src_name = boost::get(name, source(*first, G));
  507. targ_name = boost::get(name, target(*first, G));
  508. cout &lt;&lt; src_name &lt;&lt; " owes "
  509. &lt;&lt; targ_name &lt;&lt; " some money" &lt;&lt; endl;
  510. ++first;
  511. }
  512. </PRE>
  513. The output is:
  514. <PRE>
  515. Jeremy owes Rich some money
  516. Jeremy owes Andrew some money
  517. Jeremy owes Jeff some money
  518. Jeremy owes Kinis some money
  519. Andrew owes Jeremy some money
  520. Andrew owes Kinis some money
  521. Jeff owes Jeremy some money
  522. Jeff owes Rich some money
  523. Jeff owes Kinis some money
  524. Kinis owes Jeremy some money
  525. Kinis owes Rich some money
  526. </PRE>
  527. The complete source code to this example is in the file
  528. <TT>interior_property_map.cpp</TT>.
  529. <P>
  530. <H2><A NAME="sec:custom-storage"></A>
  531. Customizing the Adjacency List Storage
  532. </H2>
  533. <P>
  534. The <TT>adjacency_list</TT> is constructed out of two kinds of
  535. containers. One type of container to hold all the vertices in the
  536. graph, and another type of container for the out-edge list (and
  537. potentially in-edge list) for each vertex. BGL provides selector
  538. classes that allow the user to choose between several of the containers
  539. from the STL. It is also possible to use your own container types.
  540. When customizing the <TT>VertexList</TT> you need to define a container
  541. generator as described below. When customizing the <TT>OutEdgeList</TT> you
  542. will need to define a container generator and the parallel edge
  543. traits. The file <TT>container_gen.cpp</TT> has an example of
  544. how to use a custom storage type.
  545. <P>
  546. <H3><A NAME="SECTION00834100000000000000">
  547. Container Generator</A>
  548. </H3>
  549. <P>
  550. The <TT>adjacency_list</TT> class uses a traits class called
  551. <TT>container_gen</TT> to map the <TT>OutEdgeList</TT> and <TT>VertexList</TT>
  552. selectors to the actual container types used for the graph storage.
  553. The default version of the traits class is listed below, along with an
  554. example of how the class is specialized for the <TT>listS</TT> selector.
  555. <P>
  556. <PRE>
  557. namespace boost {
  558. template &lt;class Selector, class ValueType&gt;
  559. struct container_gen { };
  560. template &lt;class ValueType&gt;
  561. struct container_gen&lt;listS, ValueType&gt; {
  562. typedef std::list&lt;ValueType&gt; type;
  563. };
  564. }
  565. </PRE>
  566. <P>
  567. To use some other container of your choice, define a
  568. selector class and then specialize the <TT>container_gen</TT>
  569. for your selector.
  570. <P>
  571. <PRE>
  572. struct custom_containerS { }; // your selector
  573. namespace boost {
  574. // the specialization for your selector
  575. template &lt;class ValueType&gt;
  576. struct container_gen&lt;custom_containerS, ValueType&gt; {
  577. typedef custom_container&lt;ValueType&gt; type;
  578. };
  579. }
  580. </PRE>
  581. <P>
  582. There may also be situations when you want to use a container that has
  583. more template parameters than just <TT>ValueType</TT>. For instance,
  584. you may want to supply the allocator type. One way to do this is to
  585. hard-code in the extra parameters within the specialization of
  586. <TT>container_gen</TT>. However, if you want more flexibility then you
  587. can add a template parameter to the selector class. In the code below
  588. we show how to create a selector that lets you specify the allocator
  589. to be used with the <TT>std::list</TT>.
  590. <P>
  591. <PRE>
  592. template &lt;class Allocator&gt;
  593. struct list_with_allocatorS { };
  594. namespace boost {
  595. template &lt;class Alloc, class ValueType&gt;
  596. struct container_gen&lt;list_with_allocatorS&lt;Alloc&gt;, ValueType&gt;
  597. {
  598. typedef typename Alloc::template rebind&lt;ValueType&gt;::other Allocator;
  599. typedef std::list&lt;ValueType, Allocator&gt; type;
  600. };
  601. }
  602. // now you can define a graph using std::list
  603. // and a specific allocator
  604. typedef adjacency_list&lt; list_with_allocatorS&lt; std::allocator&lt;int&gt; &gt;, vecS, directedS&gt; MyGraph;
  605. </PRE>
  606. <P>
  607. <H3><A NAME="SECTION00834300000000000000">
  608. Push and Erase for the Custom Container</A>
  609. </H3>
  610. <P>
  611. You must also tell the <TT>adjacency_list</TT> how elements can be
  612. efficiently added and removed from the custom container. This is
  613. accomplished by overloading the <TT>push()</TT> and <TT>erase()</TT>
  614. functions for the custom container type. The <TT>push()</TT> function
  615. should return an iterator pointing to the newly inserted element and a
  616. <TT>bool</TT> flag saying whether the edge was inserted.
  617. <PRE>
  618. template &lt;class T&gt;
  619. std::pair&lt;typename custom_container&lt;T&gt;::iterator, bool&gt;
  620. push(custom_container&lt;T&gt;&amp; c, const T&amp; v)
  621. {
  622. // this implementation may need to change for your container
  623. c.push_back(v);
  624. return std::make_pair(boost::prior(c.end()), true);
  625. }
  626. template &lt;class T&gt;
  627. void erase(custom_container&lt;T&gt;&amp; c, const T&amp; x)
  628. {
  629. // this implementation may need to change for your container
  630. c.erase(std::remove(c.begin(), c.end(), x), c.end());
  631. }
  632. </PRE>
  633. <P> There are default <TT>push()</TT> and <TT>erase()</TT> functions
  634. implemented for the STL container types.
  635. <H3><A NAME="SECTION00834200000000000000">
  636. Parallel Edge Traits</A>
  637. </H3>
  638. <P>
  639. When customizing the <TT>OutEdgeList</TT>, you must also specialize
  640. the <TT>parallel_edge_traits</TT> class to specify whether the
  641. container type allows parallel edges (and is a <a
  642. href="http://www.boost.org/sgi/stl/Sequence.html">Sequence</a>) or if
  643. the container does not allow parallel edges (and is an <a
  644. href="http://www.boost.org/sgi/stl/AssociativeContainer.html">AssociativeContainer</a>).
  645. <P>
  646. <PRE>
  647. template &lt;&gt;
  648. struct parallel_edge_traits&lt;custom_containerS&gt; {
  649. typedef allow_parallel_edge_tag type;
  650. };
  651. </PRE>
  652. <br>
  653. <HR>
  654. <TABLE>
  655. <TR valign=top>
  656. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  657. <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>)
  658. </TD></TR></TABLE>
  659. </BODY>
  660. </HTML>