graph_concepts.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 Concepts</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="chapter:graph-concepts"></A>
  16. Graph Concepts
  17. </H1>
  18. <P>
  19. The heart of the Boost Graph Library (BGL) is the interface, or
  20. concepts (in the parlance of generic programming), that define how a
  21. graph can be examined and manipulated in a data-structure neutral
  22. fashion. In fact, the BGL interface need not even be implemented using
  23. a data-structure, as for some problems it is easier or more efficient
  24. to define a graph implicitly based on some functions.
  25. <P>
  26. The BGL interface does not appear as a single graph concept. Instead
  27. it is factored into much smaller pieces. The reason for this is that
  28. the purpose of a concept is to summarize the requirements for
  29. <i>particular</i> algorithms. Any one algorithm does not need every
  30. kind of graph operation, typically only a small subset. Furthermore,
  31. there are many graph data-structures that can not provide efficient
  32. implementations of all the operations, but provide highly efficient
  33. implementations of the operations necessary for a particular algorithm.
  34. By factoring the graph interface into many smaller concepts we
  35. provide the graph algorithm writer with a good selection from which to
  36. choose the concept that is the closest match for their algorithm.
  37. Note that because of the use of traits classes rather than member
  38. types, it is not safe (and often will not work) to define subclasses of BGL
  39. graph types; those types may be missing important traits and properties that
  40. were defined externally to the class definition.
  41. <H2>Graph Structure Concepts Overview</H2>
  42. <P>
  43. <A HREF="#fig:graph-concepts">Figure 1</A> shows the refinements
  44. relations between the graph concepts. The reason for factoring the
  45. graph interface into so many concepts is to encourage algorithm
  46. interfaces to require and use only the minimum interface of a graph,
  47. thereby increasing the reusability of the algorithm.
  48. <p></p>
  49. <DIV ALIGN="CENTER"><A NAME="fig:graph-concepts"></A></A>
  50. <TABLE>
  51. <CAPTION ALIGN="BOTTOM"><STRONG>Figure 1:</STRONG>
  52. The graph concepts and refinement relationships.
  53. </CAPTION>
  54. <TR><TD><IMG SRC="./figs/concepts.gif"></TD></TR>
  55. </TABLE>
  56. </DIV>
  57. <p></p>
  58. <A HREF="#tab:graph-concept-reqs">Table&nbsp;1</A>
  59. gives a summary of the valid expressions and associated types for the
  60. graph concepts and provides links to the detailed descriptions of
  61. each of the concepts. The notation used in the table is as follows.
  62. <h3>Notation</h3>
  63. <Table>
  64. <TR>
  65. <TD><tt>G</tt></TD>
  66. <TD>A type that is a model of Graph.</TD>
  67. </TR>
  68. <TR>
  69. <TD><tt>g</tt></TD>
  70. <TD>An object of type <tt>G</tt>.</TD>
  71. </TR>
  72. <TR>
  73. <TD><tt>e</tt></TD>
  74. <TD>An object of type <tt>boost::graph_traits&lt;G&gt;::edge_descriptor</tt>.</TD>
  75. </TR>
  76. <TR>
  77. <TD><tt>e_iter</tt></TD>
  78. <TD>An object of type <tt>boost::graph_traits&lt;G&gt;::out_edge_iterator</tt>.</TD>
  79. </TR>
  80. <TR>
  81. <TD><tt>u,v</tt></TD>
  82. <TD>Are objects of type <tt>boost::graph_traits&lt;G&gt;::vertex_descriptor</tt>.</TD>
  83. </TR>
  84. <TR>
  85. <TD><TT>ep</TT></TD><TD>is an object of type <TT>G::edge_property_type</TT></TD>
  86. </TR>
  87. <TR>
  88. <TD><TT>vp</TT></TD><TD>is an object of type <TT>G::vertex_property_type</TT></TD>
  89. </TR>
  90. <TR>
  91. <TD><tt>Property</tt></TD>
  92. <TD>A type used to specify a vertex or edge property.</TD>
  93. </TR>
  94. <TR>
  95. <TD><tt>property</tt></TD>
  96. <TD>An object of type <tt>Property</tt>.</td>
  97. </TR>
  98. </table>
  99. <P>
  100. <BR><P></P>
  101. <DIV ALIGN="CENTER"><A NAME="tab:graph-concept-reqs"></A>
  102. <TABLE>
  103. <CAPTION ALIGN="BOTTOM"><STRONG>Table 1:</STRONG>
  104. Summary of the graph concepts.
  105. </CAPTION>
  106. <TR><TD>
  107. <TABLE border>
  108. <TR><TH ALIGN="LEFT">
  109. <B>Expression</B> </TH>
  110. <TH ALIGN="LEFT" VALIGN="TOP"> <B>Return Type or Description</B> </TH>
  111. </TR>
  112. <TR><TD ALIGN="LEFT" COLSPAN=2>
  113. <a href="./Graph.html">Graph</a> </TD>
  114. </TR>
  115. <TR><TD ALIGN="LEFT">
  116. <TT>boost::graph_traits&lt;G&gt;::vertex_descriptor</TT> </TD>
  117. <TD ALIGN="LEFT" VALIGN="TOP"> The type for
  118. vertex representative objects. </TD>
  119. </TR>
  120. <TR><TD ALIGN="LEFT">
  121. <TT>boost::graph_traits&lt;G&gt;::edge_descriptor</TT> </TD>
  122. <TD ALIGN="LEFT" VALIGN="TOP"> The type for
  123. edge representative objects. </TD>
  124. </TR>
  125. <TR><TD ALIGN="LEFT">
  126. <TT>boost::graph_traits&lt;G&gt;::directed_category</TT> </TD>
  127. <TD ALIGN="LEFT" VALIGN="TOP"> Directed or undirected? </TD>
  128. </TR>
  129. <TR><TD ALIGN="LEFT">
  130. <TT>boost::graph_traits&lt;G&gt;::edge_parallel_category</TT> </TD>
  131. <TD ALIGN="LEFT" VALIGN="TOP"> Allow parallel edges? </TD>
  132. </TR>
  133. <TR><TD ALIGN="LEFT">
  134. <TT>boost::graph_traits&lt;G&gt;::traversal_category</TT> </TD> <TD
  135. ALIGN="LEFT" VALIGN="TOP">The ways in which the vertices and edges of
  136. the graph can be visited.</TD>
  137. </TR>
  138. <!---------------------------------------------------------------->
  139. <TR><TD ALIGN="LEFT" COLSPAN=2>
  140. <a href="./IncidenceGraph.html">IncidenceGraph</a> refines Graph </TD>
  141. </TR>
  142. <TR><TD ALIGN="LEFT">
  143. <TT>boost::graph_traits&lt;G&gt;::out_edge_iterator</TT> </TD>
  144. <TD ALIGN="LEFT" VALIGN="TOP"> Iterate through
  145. the out-edges. </TD>
  146. </TR>
  147. <TR><TD ALIGN="LEFT">
  148. <TT>boost::graph_traits&lt;G&gt;::degree_size_type</TT> </TD>
  149. <TD ALIGN="LEFT" VALIGN="TOP"> The integer type for
  150. vertex degree. </TD>
  151. </TR>
  152. <TR><TD ALIGN="LEFT">
  153. <TT>out_edges(v, g)</TT> </TD>
  154. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>std::pair&lt;out_edge_iterator, out_edge_iterator&gt;</TT> </TD>
  155. </TR>
  156. <TR><TD ALIGN="LEFT">
  157. <TT>source(e, g)</TT> </TD>
  158. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>vertex_descriptor</TT> </TD>
  159. </TR>
  160. <TR><TD ALIGN="LEFT">
  161. <TT>target(e, g)</TT> </TD>
  162. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>vertex_descriptor</TT> </TD>
  163. </TR>
  164. <TR><TD ALIGN="LEFT">
  165. <TT>out_degree(v, g)</TT> </TD>
  166. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>degree_size_type</TT> </TD>
  167. </TR>
  168. <!---------------------------------------------------------------->
  169. <TR><TD ALIGN="LEFT" COLSPAN=2>
  170. <a href="./BidirectionalGraph.html">BidirectionalGraph</a> refines
  171. IncidenceGraph </TD>
  172. </TR>
  173. <TR><TD ALIGN="LEFT">
  174. <TT>boost::graph_traits&lt;G&gt;::in_edge_iterator</TT> </TD>
  175. <TD ALIGN="LEFT" VALIGN="TOP"> Iterate through the in-edges. </TD>
  176. </TR>
  177. <TR><TD ALIGN="LEFT">
  178. <TT>in_edges(v, g)</TT> </TD>
  179. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>std::pair&lt;in_edge_iterator, in_edge_iterator&gt;</TT> </TD>
  180. </TR>
  181. <TR><TD ALIGN="LEFT">
  182. <TT>in_degree(v, g)</TT> </TD>
  183. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>degree_size_type</TT> </TD>
  184. </TR>
  185. <TR><TD ALIGN="LEFT">
  186. <TT>degree(e, g)</TT> </TD>
  187. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>degree_size_type</TT> </TD>
  188. </TR>
  189. <!---------------------------------------------------------------->
  190. <TR><TD ALIGN="LEFT" COLSPAN=2>
  191. <a href="./AdjacencyGraph.html">AdjacencyGraph</a> refines Graph</TD>
  192. </TR>
  193. <TR><TD ALIGN="LEFT">
  194. <TT>boost::graph_traits&lt;G&gt;::adjacency_iterator</TT> </TD>
  195. <TD ALIGN="LEFT" VALIGN="TOP"> Iterate through
  196. adjacent vertices. </TD>
  197. </TR>
  198. <TR><TD ALIGN="LEFT">
  199. <TT>adjacent_vertices(v, g)</TT> </TD>
  200. <TD ALIGN="LEFT" VALIGN="TOP"><TT>std::pair&lt;adjacency_iterator, adjacency_iterator&gt;</TT> </TD>
  201. </TR>
  202. <!---------------------------------------------------------------->
  203. <TR><TD ALIGN="LEFT" COLSPAN=2>
  204. <a href="./VertexListGraph.html">VertexListGraph</a> refines
  205. Graph</TD>
  206. </TR>
  207. <TR><TD ALIGN="LEFT">
  208. <TT>boost::graph_traits&lt;G&gt;::vertex_iterator</TT> </TD>
  209. <TD ALIGN="LEFT" VALIGN="TOP"> Iterate through the
  210. graph's vertex set. </TD>
  211. </TR>
  212. <TR><TD ALIGN="LEFT">
  213. <TT>boost::graph_traits&lt;G&gt;::vertices_size_type</TT> </TD>
  214. <TD ALIGN="LEFT" VALIGN="TOP"> The unsigned integer type for
  215. number of vertices in the graph. </TD>
  216. </TR>
  217. <TR><TD ALIGN="LEFT">
  218. <TT>vertices(g)</TT> </TD>
  219. <TD ALIGN="LEFT" VALIGN="TOP"><TT>std::pair&lt;vertex_iterator, vertex_iterator&gt;</TT> </TD>
  220. </TR>
  221. <TR><TD ALIGN="LEFT">
  222. <TT>num_vertices(g)</TT> </TD>
  223. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>vertices_size_type</TT> </TD>
  224. </TR>
  225. <!---------------------------------------------------------------->
  226. <TR><TD ALIGN="LEFT" COLSPAN=2>
  227. <a href="./EdgeListGraph.html">EdgeListGraph</a> refines Graph</TD>
  228. </TR>
  229. <TR><TD ALIGN="LEFT">
  230. <TT>boost::graph_traits&lt;G&gt;::edge_iterator</TT> </TD>
  231. <TD ALIGN="LEFT" VALIGN="TOP"> Iterate through the graph's
  232. edge set. </TD>
  233. </TR>
  234. <TR><TD ALIGN="LEFT">
  235. <TT>boost::graph_traits&lt;G&gt;::edges_size_type</TT> </TD>
  236. <TD ALIGN="LEFT" VALIGN="TOP"> The unsigned integer type for
  237. number of edges in the graph. </TD>
  238. </TR>
  239. <TR><TD ALIGN="LEFT">
  240. <TT>edges(g)</TT> </TD>
  241. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>std::pair&lt;edge_iterator, edge_iterator&gt;</TT> </TD>
  242. </TR>
  243. <TR><TD ALIGN="LEFT">
  244. <TT>num_edges(g)</TT> </TD>
  245. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>edges_size_type</TT> </TD>
  246. </TR>
  247. <TR><TD ALIGN="LEFT">
  248. <TT>source(e, g)</TT> </TD>
  249. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>vertex_descriptor</TT> </TD>
  250. </TR>
  251. <TR><TD ALIGN="LEFT">
  252. <TT>target(e, g)</TT> </TD>
  253. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>vertex_descriptor</TT> </TD>
  254. </TR>
  255. <!---------------------------------------------------------------->
  256. <TR><TD ALIGN="LEFT" COLSPAN=2>
  257. <a href="./AdjacencyMatrix.html">AdjacencyMatrix</a> refines Graph</TD>
  258. </TR>
  259. <TR><TD ALIGN="LEFT">
  260. <TT>edge(u, v, g)</TT> </TD>
  261. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>std::pair&lt;edge_descriptor, bool&gt;</TT> </TD>
  262. </TR>
  263. <TR><TD ALIGN="LEFT" COLSPAN=2>
  264. <a href="./MutableGraph.html">MutableGraph</a> refines
  265. Graph</TD>
  266. </TR>
  267. <TR><TD ALIGN="LEFT">
  268. <TT>add_vertex(g)</TT> </TD>
  269. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>vertex_descriptor</TT> </TD>
  270. </TR>
  271. <TR><TD ALIGN="LEFT">
  272. <TT>clear_vertex(v, g)</TT> </TD>
  273. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>void</TT> </TD>
  274. </TR>
  275. <TR><TD ALIGN="LEFT">
  276. <TT>remove_vertex(v, g)</TT> </TD>
  277. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>void</TT> </TD>
  278. </TR>
  279. <TR><TD ALIGN="LEFT">
  280. <TT>add_edge(u, v, g)</TT> </TD>
  281. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>std::pair&lt;edge_descriptor, bool&gt;</TT> </TD>
  282. </TR>
  283. <TR><TD ALIGN="LEFT">
  284. <TT>remove_edge(u, v, g)</TT> </TD>
  285. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>void</TT> </TD>
  286. </TR>
  287. <TR><TD ALIGN="LEFT">
  288. <TT>remove_edge(e, g)</TT> </TD>
  289. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>void</TT> </TD>
  290. </TR>
  291. <TR><TD ALIGN="LEFT">
  292. <TT>remove_edge(e_iter, g)</TT> </TD>
  293. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>void</TT> </TD>
  294. </TR>
  295. <!---------------------------------------------------------------->
  296. <TR><TD ALIGN="LEFT" COLSPAN=2>
  297. <a href="./MutablePropertyGraph.html">MutablePropertyGraph</a> refines
  298. Graph</TD>
  299. </TR>
  300. <TR><TD ALIGN="LEFT">
  301. <TT>add_vertex(vp, g)</TT> </TD>
  302. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>vertex_descriptor</TT> </TD>
  303. </TR>
  304. <TR><TD ALIGN="LEFT">
  305. <TT>add_edge(u, v, ep, g)</TT> </TD>
  306. <TD ALIGN="LEFT" VALIGN="TOP"> <TT>std::pair&lt;edge_descriptor,
  307. bool&gt;</TT> </TD>
  308. </TR>
  309. <!---------------------------------------------------------------->
  310. <TR>
  311. <TD ALIGN="LEFT" COLSPAN=2>
  312. <a href="./PropertyGraph.html">PropertyGraph</a> refines Graph</TD>
  313. </TR>
  314. <TR><TD ALIGN="LEFT">
  315. <TT>boost::property_map&lt;G, Property&gt;::type</TT> </TD>
  316. <TD ALIGN="LEFT" VALIGN="TOP">Type for a mutable property map.</TD>
  317. </TR>
  318. <TR><TD ALIGN="LEFT">
  319. <TT>boost::property_map&lt;G, Property&gt;::const_type</TT> </TD>
  320. <TD ALIGN="LEFT" VALIGN="TOP">Type for a non-mutable property map.</TD>
  321. </TR>
  322. <TR><TD ALIGN="LEFT">
  323. <TT>get(property, g)</TT> </TD>
  324. <TD ALIGN="LEFT" VALIGN="TOP"> Function to get a property map. </TD>
  325. </TR>
  326. <TR><TD ALIGN="LEFT">
  327. <TT>get(property,&nbsp;g,&nbsp;x)</TT>
  328. </TD>
  329. <TD ALIGN="LEFT" VALIGN="TOP">Get property value for vertex or edge <tt>x</tt>. </TD>
  330. </TR>
  331. <TR><TD ALIGN="LEFT">
  332. <TT>put(property,&nbsp;g,&nbsp;x,&nbsp;v)</TT>
  333. </TD>
  334. <TD ALIGN="LEFT" VALIGN="TOP">Set property value for vertex or edge
  335. <tt>x</tt> to <tt>v</tt>. </TD>
  336. </TR>
  337. </table>
  338. </table>
  339. </DIV><P></P>
  340. <BR>
  341. <P>
  342. <H2><A NAME="sec:undirected-graphs"></A>
  343. Undirected Graphs
  344. </H2>
  345. <P>
  346. The interface that the BGL provides for accessing and manipulating an
  347. undirected graph is the same as the interface for directed graphs
  348. described in the following sections, however there are some
  349. differences in the behaviour and semantics. For example, in a
  350. directed graph we can talk about out-edges and in-edges of a vertex.
  351. In an undirected graph there is no ``in'' and ``out'', there are just
  352. edges incident to a vertex. Nevertheless, in the BGL we still use the
  353. <TT>out_edges()</TT> function (or <TT>in_edges()</TT>) to access the
  354. incident edges in an undirected graph. Similarly, an undirected edge
  355. has no ``source'' and ``target'' but merely an unordered pair of
  356. vertices, but in the BGL we still use <TT>source()</TT> and
  357. <TT>target()</TT> to access these vertices. The reason the BGL does
  358. not provide a separate interface for undirected graphs is that many
  359. algorithms on directed graphs also work on undirected graphs, and it
  360. would be inconvenient to have to duplicate the algorithms just because
  361. of an interface difference. When using undirected graphs just mentally
  362. disregard the directionality in the function names. The example below
  363. demonstrates using the <TT>out_edges()</TT>, <TT>source()</TT>, and
  364. <TT>target()</TT> with an undirected graph. The source code for this
  365. example and the following one can be found in <a
  366. href="../example/undirected_adjacency_list.cpp"><TT>example/undirected_adjacency_list.cpp</TT></a>.
  367. <P>
  368. <PRE>
  369. const int V = 2;
  370. typedef ... UndirectedGraph;
  371. UndirectedGraph undigraph(V);
  372. std::cout &lt;&lt; "the edges incident to v: ";
  373. boost::graph_traits&lt;UndirectedGraph&gt;::out_edge_iterator e, e_end;
  374. boost::graph_traits&lt;UndirectedGraph&gt;::vertex_descriptor
  375. s = vertex(0, undigraph);
  376. for (boost::tie(e, e_end) = out_edges(s, undigraph); e != e_end; ++e)
  377. std::cout &lt;&lt; "(" &lt;&lt; source(*e, undigraph)
  378. &lt;&lt; "," &lt;&lt; target(*e, undigraph) &lt;&lt; ")" &lt;&lt; endl;
  379. </PRE>
  380. <P>
  381. Even though the interface is the same for undirected graphs, there are
  382. some behavioral differences because edge equality is defined
  383. differently. In a directed graph, edge <i>(u,v)</i> is never equal to edge
  384. <i>(v,u)</i>, but in an undirected graph they may be equal. If the
  385. undirected graph is a multigraph then <i>(u,v)</i> and <i>(v,u)</i> might be
  386. parallel edges. If the graph is not a multigraph then <i>(u,v)</i> and
  387. <i>(v,u)</i> must be the same edge.
  388. <P>
  389. In the example below the edge equality test will return <TT>false</TT>
  390. for the directed graph and <TT>true</TT> for the undirected graph. The
  391. difference also affects the meaning of <TT>add_edge()</TT>. In the
  392. example below, if we had also written <TT>add_edge(v, u,
  393. undigraph)</TT>, this would have added a parallel edge between
  394. <i>u</i> and <i>v</i> (provided the graph type allows parallel
  395. edges). The difference in edge equality also affects the association
  396. of edge properties. In the directed graph, the edges <i>(u,v)</i> and
  397. <i>(v,u)</i> can have distinct weight values, whereas in the
  398. undirected graph the weight of <i>(u,v)</i> is the same as the weight
  399. of <i>(v,u)</i> since they are the same edge.
  400. <P>
  401. <PRE>
  402. typedef ... DirectedGraph;
  403. DirectedGraph digraph(V);
  404. {
  405. boost::graph_traits&lt;DirectedGraph&gt;::vertex_descriptor u, v;
  406. u = vertex(0, digraph);
  407. v = vertex(1, digraph);
  408. add_edge(digraph, u, v, Weight(1.2));
  409. add_edge(digraph, v, u, Weight(2.4));
  410. boost::graph_traits&lt;DirectedGraph&gt;::edge_descriptor e1, e2;
  411. bool found;
  412. boost::tie(e1, found) = edge(u, v, digraph);
  413. boost::tie(e2, found) = edge(v, u, digraph);
  414. std::cout &lt;&lt; "in a directed graph is ";
  415. std::cout &lt;&lt; "(u,v) == (v,u) ? " &lt;&lt; (e1 == e2) &lt;&lt; std::endl;
  416. property_map&lt;DirectedGraph, edge_weight_t&gt;::type
  417. weight = get(edge_weight, digraph);
  418. cout &lt;&lt; "weight[(u,v)] = " &lt;&lt; get(weight, e1) &lt;&lt; endl;
  419. cout &lt;&lt; "weight[(v,u)] = " &lt;&lt; get(weight, e2) &lt;&lt; endl;
  420. }
  421. {
  422. boost::graph_traits&lt;UndirectedGraph&gt;::vertex_descriptor u, v;
  423. u = vertex(0, undigraph);
  424. v = vertex(1, undigraph);
  425. add_edge(undigraph, u, v, Weight(3.1));
  426. boost::graph_traits&lt;UndirectedGraph&gt;::edge_descriptor e1, e2;
  427. bool found;
  428. boost::tie(e1, found) = edge(u, v, undigraph);
  429. boost::tie(e2, found) = edge(v, u, undigraph);
  430. std::cout &lt;&lt; "in an undirected graph is ";
  431. std::cout &lt;&lt; "(u,v) == (v,u) ? " &lt;&lt; (e1 == e2) &lt;&lt; std::endl;
  432. property_map&lt;UndirectedGraph, edge_weight_t&gt;::type
  433. weight = get(edge_weight, undigraph);
  434. cout &lt;&lt; "weight[(u,v)] = " &lt;&lt; get(weight, e1) &lt;&lt; endl;
  435. cout &lt;&lt; "weight[(v,u)] = " &lt;&lt; get(weight, e2) &lt;&lt; endl;
  436. }
  437. </PRE>
  438. The output is:
  439. <PRE>
  440. in a directed graph is (u,v) == (v,u) ? 0
  441. weight[(u,v)] = 1.2
  442. weight[(v,u)] = 2.4
  443. in an undirected graph is (u,v) == (v,u) ? 1
  444. weight[(u,v)] = 3.1
  445. weight[(v,u)] = 3.1
  446. </PRE>
  447. <br>
  448. <HR>
  449. <TABLE>
  450. <TR valign=top>
  451. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  452. <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>)
  453. </TD></TR></TABLE>
  454. </BODY>
  455. </HTML>