faq.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <HTML>
  2. <!--
  3. Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 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: FAQ</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>Frequently Asked Questions</h1>
  16. <ol>
  17. <li>
  18. How do I perform an early exit from an algorithm such as BFS?<br>
  19. <p>
  20. Create a visitor that throws an exception when you want to cut off the
  21. search, then put your call to <tt>breadth_first_search</tt> inside of
  22. an appropriate try/catch block. This strikes many programmers as a
  23. misuse of exceptions, however, much thought was put into the decision
  24. to have exceptions has the preferred way to exit early. See boost
  25. email discussions for more details.
  26. </p>
  27. <li>
  28. Why is the visitor parameter passed by value rather than reference
  29. in the various BGL algorithms?<br>
  30. <p>
  31. One of the usage scenarios that we wanted to support with the
  32. algorithms was creating visitor objects on the fly, within the
  33. argument list of the call to the graph algorithm. In this situation,
  34. the visitor object is a temporary object. Now there is a truly
  35. unfortunate rule in the C++ standard that says a temporary cannot be
  36. bound to a non-const reference parameter. So we had to decide whether
  37. we wanted to support this kind of usage and call-by-value, or not and
  38. call-by-reference. We chose call-by-value, following in the footsteps
  39. of the STL (which passes functors by value). The disadvantage of this
  40. decision is that if your visitor contains state and changes that state
  41. during the algorithm, the change will be made to a copy of the visitor
  42. object, not the visitor object passed in. Therefore you may want the
  43. visitor to hold this state by pointer or reference.
  44. </p>
  45. <li>Why does the BGL interface use friend functions (or free functions)
  46. instead of member functions?<br>
  47. <p>
  48. For the most part, the differences between member functions and free
  49. functions are syntactic, and not very important, though people can get
  50. religious about them. However, we had one technical reason for
  51. favoring free functions. A programmer can overload a free function for
  52. a type coming from a 3rd party without modifying the source
  53. code/definition of that type. There are several uses of this in the
  54. BGL. For example, Stanford GraphBase and LEDA graphs can both be used
  55. in BGL algorithms because of overloads in <tt>stanford_graph.hpp</tt>
  56. and <tt>leda_graph.hpp</tt>. One can even use
  57. <tt>std::vector&lt;std::list&gt;</tt> as a graph due to the overloads
  58. in <tt>vector_as_graph.hpp</tt>.
  59. </p>
  60. <p>
  61. Of course, there is a way to adapt 3rd party classes into an interface
  62. with member functions. You create an adaptor class. However, the
  63. disadvantage of an adaptor class (compared to overloaded functions) is
  64. that one has to physically wrap and unwrap the objects as they go
  65. into/out of BGL algorithms. So the overloaded function route is more
  66. convenient. Granted, this is not a huge difference, but since there
  67. weren't other strong reasons, it was enough for us to choose free
  68. functions.
  69. </p>
  70. <p>
  71. Our religious reason for choosing free functions is to send the message
  72. that BGL is a generic library, and not a traditional object-oriented
  73. library. OO was hip in the 80s and 90s, but its time we moved beyond!
  74. </p>
  75. </li>
  76. <li>How do I create a graph where the edges are sorted/ordered? <br>
  77. <p>The example <a href="../example/ordered_out_edges.cpp">
  78. <tt>ordered_out_edges.cpp</tt></a> shows how to do this.</p>
  79. </li>
  80. <li>Why does the algorithm X work with <tt>adjacency_list</tt> where
  81. <tt>VertexList=vecS</tt> but not when <tt>VertexList=listS</tt>? <br><br>
  82. Often the reason is that the algorithm expects to find the
  83. <tt>vertex_index</tt> property stored in the graph. When
  84. <tt>VertexList=vecS</tt>, the <tt>adjacency_list</tt> automatically
  85. has a <tt>vertex_index</tt> property. However, when <tt>VertexList=listS</tt>
  86. this is not the case, and the <tt>vertex_index</tt> property must be
  87. explicitly added, and initialized. For example,
  88. <pre>
  89. // specify the graph type
  90. typedef adjacency_list&lt;listS, listS, undirectedS,
  91. property&lt;vertex_index_t, std::size_t&gt;,
  92. no_property
  93. &gt; graph_t;
  94. // construct a graph object
  95. graph_t G(num_nodes);
  96. // obtain a property map for the vertex_index property
  97. property_map&lt;graph_t, vertex_index_t&gt;::type
  98. index = get(vertex_index, G);
  99. // initialize the vertex_index property values
  100. graph_traits&lt;graph_t&gt;::vertex_iterator vi, vend;
  101. graph_traits&lt;graph_t&gt;::vertices_size_type cnt = 0;
  102. for(boost::tie(vi,vend) = vertices(G); vi != vend; ++vi)
  103. put(index, *vi, cnt++);
  104. </pre>
  105. </li>
  106. <li>When using algorithm X, why do I get an error about a property
  107. not being found, such as:
  108. <pre>
  109. ../../../boost/concept_check.hpp:209: no match for
  110. `boost::detail::error_property_not_found &amp; ==
  111. boost::detail::error_property_not_found &amp;'
  112. </pre>
  113. or a message such as:
  114. <pre>
  115. ../../..\boost/graph/depth_first_search.hpp(78) : error C2664: 'white'
  116. : cannot convert parameter 1 from
  117. 'struct boost::detail::error_property_not_found'
  118. to 'enum boost::default_color_type'
  119. </pre>
  120. The reason is that the algorithm expected to find some property (like color or
  121. weight) attached to the vertices or edges of the graph, but didn't
  122. find it. You need to either add an interior property to the graph, or
  123. create an exterior property map for the property and pass it as an
  124. argument to the algorithm.</li>
  125. </ol>
  126. <!-- LocalWords: gif ALT BGL std const STL GraphBase LEDA BFS stanford hpp OO
  127. -->
  128. <!-- LocalWords: leda cpp VertexList vecS listS undirectedS num cnt struct
  129. -->
  130. <!-- LocalWords: enum
  131. -->