grid_graph.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  2. <!--
  3. Copyright &copy; 2009 Trustees of Indiana University
  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. <html>
  9. <head>
  10. <title>Boost Graph Library: Grid Graph</title>
  11. <style type="text/css">
  12. <!--
  13. body {
  14. color: black;
  15. background-color: white;
  16. }
  17. a {
  18. color: blue;
  19. }
  20. a:visited {
  21. color: #551A8B;
  22. }
  23. .code
  24. {
  25. border-left-style: groove;
  26. border-left-width: 1px;
  27. padding-left: 2em;
  28. }
  29. -->
  30. </style>
  31. </head>
  32. <body>
  33. <img src="../../../boost.png" alt="C++ Boost" width="277" height="86" />
  34. <br />
  35. <h1>
  36. <tt>grid_graph</tt>
  37. </h1>
  38. <ul>
  39. <li><a href="#overview">Overview</a></li>
  40. <li><a href="#creating">Creating a Grid Graph</a></li>
  41. <li><a href="#indexing">Indexing</a></li>
  42. <li><a href="#member">Grid Graph Member Functions</a></li>
  43. </ul>
  44. <h3 id="overview">Overview</h3>
  45. <p>
  46. A <tt>grid_graph</tt> represents a multi-dimensional,
  47. rectangular grid of vertices with user-defined dimension lengths
  48. and wrapping.
  49. </p>
  50. <p>
  51. <tt>grid_graph</tt> models:
  52. <ul>
  53. <li><a href="IncidenceGraph.html">Incidence Graph</a></li>
  54. <li><a href="AdjacencyGraph.html">Adjacency Graph</a></li>
  55. <li><a href="VertexListGraph.html">Vertex List Graph</a></li>
  56. <li><a href="EdgeListGraph.html">Edge List Graph</a></li>
  57. <li><a href="BidirectionalGraph.html">Bidirectional Graph</a></li>
  58. <li><a href="AdjacencyMatrix.html">Adjacency Matrix</a></li>
  59. </ul>
  60. </p>
  61. <p>
  62. Defined in
  63. <a href="../../../boost/graph/grid_graph.hpp"><tt>boost/graph/grid_graph.hpp</tt></a>
  64. with all functions in the <tt>boost</tt> namespace. A simple examples of creating and iterating over a grid_graph is available here <a href="../../../libs/graph/example/grid_graph_example.cpp"><tt>libs/graph/example/grid_graph_example.cpp</tt></a>. An example of adding properties to a grid_graph is also available <a href="../../../libs/graph/example/grid_graph_example.cpp"><tt>libs/graph/example/grid_graph_properties.cpp</tt></a>
  65. </p>
  66. <h4>Template Parameters</h4>
  67. <pre class="code">
  68. <span class="keyword">template</span> &lt;<span class="name">std</span>::<span class="type">size_t</span> <span class="name">Dimensions</span>,
  69. <span class="keyword">typename</span> <span class="name">VertexIndex</span> = <span class="name">std</span>::<span class="type">size_t</span>,
  70. <span class="keyword">typename</span> <span class="name">EdgeIndex</span> = <span class="name">VertexIndex</span>&gt;
  71. <span class="keyword">class</span> grid_graph;
  72. </pre>
  73. <ul>
  74. <li>
  75. <tt>Dimensions</tt> - Number of dimensions in the graph
  76. </li>
  77. <li>
  78. <tt>VertexIndex</tt> - Type used for vertex indices, defaults to <tt>std::size_t</tt>
  79. </li>
  80. <li>
  81. <tt>EdgeIndex</tt> - Type used for edge indices, defaults to the same type as <tt>VertexIndex</tt>
  82. </li>
  83. </ul>
  84. <h3 id="creating">Creating a Grid Graph</h3>
  85. <p>
  86. The constructor to <tt>grid_graph</tt> has several overloads to aid in configuring each dimension:
  87. </p>
  88. <pre class="code">
  89. <span class="comment">// Defines a grid_graph that does not wrap.</span>
  90. <span class="type">grid_graph</span>&lt;...&gt;(<span class="name">boost</span>:<span class="type">array</span>&lt;<span class="name">VertexIndex</span>, <span class="name">Dimensions</span>&gt; dimension_lengths);
  91. <span class="comment">// Defines a grid_graph where all dimensions are either wrapped or unwrapped.</span>
  92. <span class="type">grid_graph</span>&lt;...&gt;(<span class="name">boost</span>:<span class="type">array</span>&lt;<span class="name">VertexIndex</span>, <span class="name">Dimensions</span>&gt; dimension_lengths,
  93. <span class="keyword">bool</span> wrap_all_dimensions);
  94. <span class="comment">// Defines a grid_graph where the wrapping for each dimension is specified individually.</span>
  95. <span class="type">grid_graph</span>&lt;...&gt;(<span class="name">boost</span>:<span class="type">array</span>&lt;<span class="name">VertexIndex</span>, <span class="name">Dimensions</span>&gt; dimension_lengths,
  96. <span class="name">boost</span>:<span class="type">array</span>&lt;<span class="keyword">bool</span>, <span class="name">Dimensions</span>&gt; wrap_dimension);
  97. </pre>
  98. <h4>Example</h4>
  99. <pre class="code">
  100. <span class="comment">// Define dimension lengths, a 3x3 in this case</span>
  101. <span class="name">boost</span>::<span class="type">array</span>&lt;<span class="name">std</span>::<span class="type">size_t</span>, <span class="literal">2</span>&gt; lengths = { { <span class="literal">3</span>, <span class="literal">3</span> } };
  102. <span class="comment">// Create a 3x3 two-dimensional, unwrapped grid graph (Figure 1)</span>
  103. <span class="type">grid_graph</span>&lt;<span class="literal">2</span>&gt; graph(lengths);
  104. <span class="comment">// Create a 3x3 two-dimensional, wrapped grid graph (Figure 2)</span>
  105. <span class="type">grid_graph</span>&lt;<span class="literal">2</span>&gt; graph(lengths, <span class="keyword">true</span>);
  106. </pre>
  107. <p style="margin-left: 10px;">
  108. <img src="figs/grid_graph_unwrapped.png" />
  109. <br />
  110. <em style="font-size: 0.8em"><b>Figure 1:</b> A 3x3 two-dimensional, unwrapped grid graph</em>
  111. </p>
  112. <br />
  113. <p style="margin-left: 10px;">
  114. <img src="figs/grid_graph_wrapped.png" />
  115. <br />
  116. <em style="font-size: 0.8em"><b>Figure 2:</b> A 3x3 two-dimensional, wrapped grid graph</em>
  117. </p>
  118. <br />
  119. <h3 id="indexing">Indexing</h3>
  120. <p>
  121. The <tt>grid_graph</tt> supports addressing vertices and edges
  122. by index. The following functions allow you to convert between
  123. vertices, edges, and their associated indices:
  124. </p>
  125. <pre class="code">
  126. <span class="keyword">typedef</span> <span class="type">grid_graph</span>&lt;...&gt; <span class="name">Graph</span>;
  127. <span class="keyword">typedef</span> <span class="type">graph_traits</span>&lt;<span class="name">Graph</span>&gt; <span class="name">Traits</span>;
  128. <span class="comment">// Get the vertex associated with vertex_index</span>
  129. <span class="name">Traits</span>::<span class="type">vertex_descriptor</span>
  130. vertex(<span class="name">Traits</span>::<span class="type">vertices_size_type</span> vertex_index,
  131. <span class="keyword">const</span> <span class="name">Graph&amp;</span> graph);
  132. <span class="comment">// Get the index associated with vertex</span>
  133. <span class="name">Traits</span>::<span class="type">vertices_size_type</span>
  134. get(<span class="name">boost</span>::<span class="type">vertex_index_t</span>,
  135. <span class="keyword">const</span> <span class="name">Graph&amp;</span> graph,
  136. <span class="name">Traits</span>::<span class="type">vertex_descriptor</span> vertex);
  137. <span class="comment">// Get the edge associated with edge_index</span>
  138. <span class="name">Traits</span>::<span class="type">edge_descriptor</span>
  139. edge_at(<span class="name">Traits</span>::<span class="type">edges_size_type</span> edge_index,
  140. <span class="keyword">const</span> <span class="name">Graph&amp;</span> graph);
  141. <span class="comment">// Get the index associated with edge</span>
  142. <span class="name">Traits</span>::<span class="type">edges_size_type</span>
  143. get(<span class="name">boost</span>::<span class="type">edge_index_t</span>,
  144. <span class="keyword">const</span> <span class="name">Graph&amp;</span> graph,
  145. <span class="name">Traits</span>::<span class="type">edge_descriptor</span> edge);
  146. <span class="comment">// Get the out-edge associated with vertex and out_edge_index</span>
  147. <span class="name">Traits</span>::<span class="type">edge_descriptor</span>
  148. out_edge_at(<span class="name">Traits</span>::<span class="type">vertex_descriptor</span> vertex,
  149. <span class="name">Traits</span>::<span class="type">degree_size_type</span> out_edge_index,
  150. <span class="keyword">const</span> <span class="name">Graph&amp;</span> graph);
  151. <span class="comment">// Get the out-edge associated with vertex and in_edge_index</span>
  152. <span class="name">Traits</span>::<span class="type">edge_descriptor</span>
  153. in_edge_at(<span class="name">Traits</span>::<span class="type">vertex_descriptor</span> vertex,
  154. <span class="name">Traits</span>::<span class="type">degree_size_type</span> in_edge_index,
  155. <span class="keyword">const</span> <span class="name">Graph&amp;</span> graph);
  156. </pre>
  157. <h4>Example</h4>
  158. <pre class="code">
  159. <span class="keyword">typedef</span> <span class="type">grid_graph</span>&lt;2&gt; <span class="name">Graph</span>;
  160. <span class="keyword">typedef</span> <span class="type">graph_traits</span>&lt;<span class="name">Graph</span>&gt; <span class="name">Traits</span>;
  161. <span class="comment">// Create a 3x3, unwrapped grid_graph (Figure 3)</span>
  162. <span class="name">boost</span>::<span class="type">array</span>&lt;<span class="name">std</span>::<span class="type">size_t</span>, <span class="literal">2</span>&gt; lengths = { { <span class="literal">3</span>, <span class="literal">3</span> } };
  163. <span class="name">Graph</span> graph(lengths);
  164. <span class="comment">// Do a round-trip test of the vertex index functions</span>
  165. <span class="keyword">for</span> (<span class="name">Traits</span>::<span class="type">vertices_size_type</span> v_index = <span class="literal">0</span>;
  166. v_index &lt; num_vertices(graph); ++v_index) {
  167. <span class="comment">// The two indices should always be equal</span>
  168. <span class="name">std</span>::cout &lt;&lt; <span class="literal">&quot;Index of vertex &quot;</span> &lt;&lt; v_index &lt;&lt; <span class="literal">&quot; is &quot;</span> &lt;&lt;
  169. get(<span class="name">boost</span>::vertex_index, graph, vertex(v_index, graph)) &lt;&lt; <span class="name">std</span>::endl;
  170. }
  171. <span class="comment">// Do a round-trip test of the edge index functions</span>
  172. <span class="keyword">for</span> (<span class="name">Traits</span>::<span class="type">edges_size_type</span> e_index = <span class="literal">0</span>;
  173. e_index &lt; num_edges(graph); ++e_index) {
  174. <span class="comment">// The two indices should always be equal</span>
  175. <span class="name">std</span>::cout &lt;&lt; <span class="literal">&quot;Index of edge &quot;</span> &lt;&lt; e_index &lt;&lt; <span class="literal">&quot; is &quot;</span> &lt;&lt;
  176. get(<span class="name">boost</span>::edge_index, graph, edge_at(e_index, graph)) &lt;&lt; <span class="name">std</span>::endl;
  177. }
  178. </pre>
  179. <p style="margin-left: 10px;">
  180. <img src="figs/grid_graph_indexed.png" />
  181. <br />
  182. <em style="font-size: 0.8em"><b>Figure 3:</b> 3x3 unwrapped grid_graph with vertex and edge indices shown.</em>
  183. </p>
  184. <br />
  185. <h3 id="member">Member Functions</h3>
  186. <p>
  187. There are several <tt>grid_graph</tt> specific member functions available:
  188. </p>
  189. <pre class="code">
  190. <span class="keyword">typedef</span> <span class="type">grid_graph</span>&lt;...&gt; <span class="name">Graph</span>;
  191. <span class="keyword">typedef</span> <span class="type">graph_traits</span>&lt;<span class="name">Graph</span>&gt; <span class="name">Traits</span>;
  192. <span class="comment">// Returns the number of dimensions</span>
  193. <span class="name">std</span>::<span class="type">size_t</span> dimensions();
  194. <span class="comment">// Returns the length of a dimension</span>
  195. <span class="name">Traits</span>::<span class="type">vertices_size_type</span> length(<span class="name">std</span>::<span class="type">size_t</span> dimension);
  196. <span class="comment">// Returns true if the dimension wraps, false if not</span>
  197. <span class="keyword">bool</span> wrapped(<span class="name">std</span>::<span class="type">size_t</span> dimension);
  198. <span class="comment">// Returns the &quot;next&quot; vertex in a dimension at a given distance. If the dimension
  199. // is unwrapped, next will stop at the last vertex in the dimension.
  200. </span><span class="name">Traits</span>::<span class="type">vertex_descriptor</span> next(<span class="name">Traits</span>::<span class="type">vertex_descriptor</span> vertex,
  201. <span class="name">std</span>::<span class="type">size_t</span> dimension,
  202. <span class="name">Traits</span>::<span class="type">vertices_size_type</span> distance = <span class="literal">1</span>);
  203. <span class="comment">// Returns the &quot;previous&quot; vertex in a dimension at a given distance. If the
  204. // dimension is unwrapped, previous will stop at the beginning vertex in the dimension.
  205. </span><span class="name">Traits</span>::<span class="type">vertex_descriptor</span> previous(<span class="name">Traits</span>::<span class="type">vertex_descriptor</span> vertex,
  206. <span class="name">std</span>::<span class="type">size_t</span> dimension,
  207. <span class="name">Traits</span>::<span class="type">vertices_size_type</span> distance = <span class="literal">1</span>);
  208. </pre>
  209. <h4>Example</h4>
  210. <pre class="code">
  211. <span class="keyword">typedef</span> <span class="type">grid_graph</span>&lt;<span class="literal">3</span>&gt; <span class="name">Graph</span>;
  212. <span class="keyword">typedef</span> <span class="type">graph_traits</span>&lt;<span class="name">Graph</span>&gt; <span class="name">Traits</span>;
  213. <span class="comment">// Define a 3x5x7 grid_graph where the second dimension doesn&apos;t wrap</span>
  214. <span class="name">boost</span>::<span class="type">array</span>&lt;<span class="name">std</span>::<span class="type">size_t</span>, <span class="literal">3</span>&gt; lengths = { { <span class="literal">3</span>, <span class="literal">5</span>, <span class="literal">7</span> } };
  215. <span class="name">boost</span>::<span class="type">array</span>&lt;<span class="keyword">bool</span>, <span class="literal">3</span>&gt; wrapped = { { <span class="keyword">true</span>, <span class="keyword">false</span>, <span class="keyword">true</span> } };
  216. <span class="name">Graph</span> graph(lengths, wrapped);
  217. <span class="comment">// Print number of dimensions</span>
  218. <span class="name">std</span>::cout &lt;&lt; graph.dimensions() &lt;&lt; <span class="name">std</span>::endl; <span class="comment">// prints &quot;3&quot;</span>
  219. <span class="comment">// Print dimension lengths (same order as in the lengths array)</span>
  220. <span class="name">std</span>::cout &lt;&lt; graph.length(<span class="literal">0</span>) &lt;&lt; <span class="literal">&quot;x&quot;</span> &lt;&lt; graph.length(<span class="literal">1</span>) <<
  221. <span class="literal">&quot;x&quot;</span> &lt;&lt; graph.length(<span class="literal">2</span>) &lt;&lt; <span class="name">std</span>::endl; <span class="comment">// prints &quot;3x5x7&quot;</span>
  222. <span class="comment">// Print dimension wrapping (W = wrapped, U = unwrapped)</span>
  223. <span class="name">std</span>::cout &lt;&lt; graph.wrapped(<span class="literal">0</span>) ? <span class="literal">&quot;W&quot;</span> : <span class="literal">&quot;U&quot;</span> &lt;&lt; <span class="literal">&quot;, &quot;</span> <<
  224. graph.wrapped(<span class="literal">1</span>) ? <span class="literal">&quot;W&quot;</span> : <span class="literal">&quot;U&quot;</span> &lt;&lt; <span class="literal">&quot;, &quot;</span> <<
  225. graph.wrapped(<span class="literal">2</span>) ? <span class="literal">&quot;W&quot;</span> : <span class="literal">&quot;U&quot;</span> &lt;&lt; <span class="name">std</span>::endl; <span class="comment">// prints &quot;W, U, W&quot;</span>
  226. <span class="comment">// Define a simple function to print vertices</span>
  227. <span class="keyword">void</span> print_vertex(<span class="name">Traits</span>::<span class="type">vertex_descriptor</span> vertex_to_print) {
  228. <span class="name">std</span>::cout &lt;&lt; <span class="literal">&quot;(&quot;</span> &lt;&lt; vertex_to_print[<span class="literal">0</span>] &lt;&lt; <span class="literal">&quot;, &quot;</span> &lt;&lt; vertex_to_print[<span class="literal">1</span>] <<
  229. <span class="literal">&quot;, &quot;</span> &lt;&lt; vertex_to_print[<span class="literal">2</span>] &lt;&lt; <span class="literal">&quot;)&quot;</span> &lt;&lt; <span class="name">std</span>::endl;
  230. }
  231. <span class="comment">// Start with the first vertex in the graph</span>
  232. <span class="name">Traits</span>::<span class="type">vertex_descriptor</span> first_vertex = vertex(<span class="literal">0</span>, graph);
  233. print_vertex(first_vertex); <span class="comment">// prints &quot;(0, 0, 0)&quot;</span>
  234. <span class="comment">// Print the next vertex in dimension 0</span>
  235. print_vertex(graph.next(first_vertex, <span class="literal">0</span>)); <span class="comment">// prints &quot;(1, 0, 0)&quot;</span>
  236. <span class="comment">// Print the next vertex in dimension 1</span>
  237. print_vertex(graph.next(first_vertex, <span class="literal">1</span>)); <span class="comment">// prints &quot;(0, 1, 0)&quot;</span>
  238. <span class="comment">// Print the 5th next vertex in dimension 2</span>
  239. print_vertex(graph.next(first_vertex, <span class="literal">2</span>, <span class="literal">5</span>)); <span class="comment">// prints &quot;(0, 0, 5)&quot;</span>
  240. <span class="comment">// Print the previous vertex in dimension 0 (wraps)</span>
  241. print_vertex(graph.previous(first_vertex, <span class="literal">0</span>)); <span class="comment">// prints &quot;(2, 0, 0)&quot;</span>
  242. <span class="comment">// Print the previous vertex in dimension 1 (doesn&apos;t wrap, so it&apos;s the same)</span>
  243. print_vertex(graph.previous(first_vertex, <span class="literal">1</span>)); <span class="comment">// prints &quot;(0, 0, 0)&quot;</span>
  244. <span class="comment">// Print the 20th previous vertex in dimension 2 (wraps around twice)</span>
  245. print_vertex(graph.previous(first_vertex, <span class="literal">2</span>, <span class="literal">20</span>)); <span class="comment">// prints &quot;(0, 0, 1)&quot;</span>
  246. </pre>
  247. <hr />
  248. Copyright &copy; 2009 Trustees of Indiana University
  249. </body>
  250. </html>