a00069.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
  5. <title>GTL - Graph Template Library: bfs.h Source File</title>
  6. <link href="doxygen.css" rel="stylesheet" type="text/css">
  7. </head>
  8. <body>
  9. <p class="links">
  10. <a href="../index.html">Home</a> |
  11. Documentation |
  12. <a href="../register.html">Download</a> |
  13. <a href="../platforms.html">Platforms</a> |
  14. <a href="../refer.html">Projects</a> |
  15. <a href="../lists.html">Mailing Lists</a> |
  16. <a href="../history.html">Version History</a>
  17. </p>
  18. <!-- Generated by Doxygen 1.5.3 -->
  19. <div class="tabs">
  20. <ul>
  21. <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
  22. <li><a href="classes.html"><span>Classes</span></a></li>
  23. <li class="current"><a href="files.html"><span>Files</span></a></li>
  24. <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
  25. </ul>
  26. </div>
  27. <h1>bfs.h</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">//==========================================================================</span>
  28. <a name="l00002"></a>00002 <span class="comment">//</span>
  29. <a name="l00003"></a>00003 <span class="comment">// bfs.h</span>
  30. <a name="l00004"></a>00004 <span class="comment">//</span>
  31. <a name="l00005"></a>00005 <span class="comment">//==========================================================================</span>
  32. <a name="l00006"></a>00006 <span class="comment">// $Id: bfs.h,v 1.14 2003/03/24 15:58:54 raitner Exp $</span>
  33. <a name="l00007"></a>00007
  34. <a name="l00008"></a>00008 <span class="preprocessor">#ifndef GTL_BFS_H</span>
  35. <a name="l00009"></a>00009 <span class="preprocessor"></span><span class="preprocessor">#define GTL_BFS_H</span>
  36. <a name="l00010"></a>00010 <span class="preprocessor"></span>
  37. <a name="l00011"></a>00011 <span class="preprocessor">#include &lt;GTL/GTL.h&gt;</span>
  38. <a name="l00012"></a>00012 <span class="preprocessor">#include &lt;GTL/algorithm.h&gt;</span>
  39. <a name="l00013"></a>00013 <span class="preprocessor">#include &lt;GTL/node_map.h&gt;</span>
  40. <a name="l00014"></a>00014
  41. <a name="l00015"></a>00015 <span class="preprocessor">#include &lt;deque&gt;</span>
  42. <a name="l00016"></a>00016
  43. <a name="l00017"></a>00017 __GTL_BEGIN_NAMESPACE
  44. <a name="l00018"></a>00018
  45. <a name="l00086"></a><a class="code" href="a00003.html">00086</a> <span class="keyword">class </span>GTL_EXTERN <a class="code" href="a00003.html" title="Breadth-First-Search (BFS) algorithm.">bfs</a> : <span class="keyword">public</span> <a class="code" href="a00001.html" title="Abstract baseclass for all algoritm-classes.">algorithm</a>
  46. <a name="l00087"></a>00087 {
  47. <a name="l00088"></a>00088 <span class="keyword">public</span>:
  48. <a name="l00089"></a>00089
  49. <a name="l00093"></a>00093 <a class="code" href="a00003.html" title="Breadth-First-Search (BFS) algorithm.">bfs</a> ();
  50. <a name="l00094"></a>00094
  51. <a name="l00098"></a>00098 <span class="keyword">virtual</span> ~<a class="code" href="a00003.html" title="Breadth-First-Search (BFS) algorithm.">bfs</a> ();
  52. <a name="l00099"></a>00099
  53. <a name="l00100"></a>00100 <span class="keywordtype">int</span> <a class="code" href="a00001.html#734b189509a8d6b56b65f8ff772d43ca" title="Applies algorithm to graph g.">run</a> (<a class="code" href="a00014.html" title="A directed or undirected graph.">graph</a>&amp; G);
  54. <a name="l00101"></a>00101
  55. <a name="l00111"></a><a class="code" href="a00003.html#6dd7e852f7768814aafba8962befca56">00111</a> <span class="keyword">virtual</span> <span class="keywordtype">int</span> <a class="code" href="a00001.html#76361fb03ad1cf643affc51821e43bed" title="Checks whether all preconditions are satisfied.">check</a> (<a class="code" href="a00014.html" title="A directed or undirected graph.">graph</a>&amp; G) { <span class="keywordflow">return</span> GTL_OK; }
  56. <a name="l00112"></a>00112
  57. <a name="l00113"></a>00113 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="a00001.html#21aba63d066ae7897de6ca7d8425c408" title="Resets algorithm.">reset</a> ();
  58. <a name="l00114"></a>00114
  59. <a name="l00115"></a>00115 <span class="comment">//-----------------------------------------------------------------------</span>
  60. <a name="l00116"></a>00116 <span class="comment">// Parameters</span>
  61. <a name="l00117"></a>00117 <span class="comment">//-----------------------------------------------------------------------</span>
  62. <a name="l00118"></a>00118
  63. <a name="l00128"></a><a class="code" href="a00003.html#23e2981c2ee617a6e12a8833d2db6210">00128</a> <span class="keywordtype">void</span> start_node (<span class="keyword">const</span> <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; n) {start = n;}
  64. <a name="l00129"></a>00129
  65. <a name="l00135"></a><a class="code" href="a00003.html#c5cf27683ad751cd6e4129141fc9b495">00135</a> <a class="code" href="a00020.html" title="A node in a graph.">node</a> start_node ()<span class="keyword"> const </span>{<span class="keywordflow">return</span> start;}
  66. <a name="l00136"></a>00136
  67. <a name="l00154"></a><a class="code" href="a00003.html#25fc51b1bfbbdd3afefe0a84c1bd2f6b">00154</a> <span class="keywordtype">void</span> scan_whole_graph (<span class="keywordtype">bool</span> <span class="keyword">set</span>) {whole_graph = <span class="keyword">set</span>;}
  68. <a name="l00155"></a>00155
  69. <a name="l00162"></a><a class="code" href="a00003.html#d698825ec64edfa808210de913a922d3">00162</a> <span class="keywordtype">bool</span> scan_whole_graph ()<span class="keyword"> const </span>{<span class="keywordflow">return</span> whole_graph;}
  70. <a name="l00163"></a>00163
  71. <a name="l00174"></a>00174 <span class="keywordtype">void</span> calc_level (<span class="keywordtype">bool</span> <span class="keyword">set</span>);
  72. <a name="l00175"></a>00175
  73. <a name="l00182"></a><a class="code" href="a00003.html#8a67bff3ab12226cf8f4bea4a14bd183">00182</a> <span class="keywordtype">bool</span> calc_level ()<span class="keyword"> const </span>{<span class="keywordflow">return</span> level_number != 0;}
  74. <a name="l00183"></a>00183
  75. <a name="l00193"></a>00193 <span class="keywordtype">void</span> store_non_tree_edges (<span class="keywordtype">bool</span> <span class="keyword">set</span>);
  76. <a name="l00194"></a>00194
  77. <a name="l00202"></a><a class="code" href="a00003.html#9e16858479ee2b4980b4dbe73202a9d6">00202</a> <span class="keywordtype">bool</span> store_non_tree_edges ()<span class="keyword"> const </span>{<span class="keywordflow">return</span> non_tree != 0;}
  78. <a name="l00203"></a>00203
  79. <a name="l00204"></a>00204
  80. <a name="l00214"></a>00214 <span class="keywordtype">void</span> store_preds (<span class="keywordtype">bool</span> <span class="keyword">set</span>);
  81. <a name="l00215"></a>00215
  82. <a name="l00222"></a><a class="code" href="a00003.html#1b73eb7f1fb984331781da890bd45029">00222</a> <span class="keywordtype">bool</span> store_preds ()<span class="keyword"> const </span>{<span class="keywordflow">return</span> preds != 0;}
  83. <a name="l00223"></a>00223
  84. <a name="l00230"></a><a class="code" href="a00003.html#a91531970fa0eb1c7e18d5455f54fe9f">00230</a> <span class="keywordtype">bool</span> reached (<span class="keyword">const</span> <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; n)<span class="keyword"> const</span>
  85. <a name="l00231"></a>00231 <span class="keyword"> </span>{<span class="keywordflow">return</span> bfs_number[n] != 0;}
  86. <a name="l00232"></a>00232
  87. <a name="l00242"></a><a class="code" href="a00003.html#ad3b67493e3d936f8ca3d1fe38d72842">00242</a> <span class="keywordtype">int</span> bfs_num (<span class="keyword">const</span> <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; n)<span class="keyword"> const </span>
  88. <a name="l00243"></a>00243 <span class="keyword"> </span>{<span class="keywordflow">return</span> bfs_number[n];}
  89. <a name="l00244"></a>00244
  90. <a name="l00254"></a><a class="code" href="a00003.html#01e0e18c3478faf9ea6b9140c1cc4102">00254</a> <span class="keywordtype">int</span> operator[] (<span class="keyword">const</span> <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; n)<span class="keyword"> const </span>
  91. <a name="l00255"></a>00255 <span class="keyword"> </span>{<span class="keywordflow">return</span> bfs_number[n];}
  92. <a name="l00256"></a>00256
  93. <a name="l00267"></a><a class="code" href="a00003.html#db141187b9b6c9fc78089eb87ba19ad4">00267</a> <span class="keywordtype">int</span> level (<span class="keyword">const</span> <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; n)<span class="keyword"> const</span>
  94. <a name="l00268"></a>00268 <span class="keyword"> </span>{assert (level_number); <span class="keywordflow">return</span> (*level_number)[n];}
  95. <a name="l00269"></a>00269
  96. <a name="l00283"></a><a class="code" href="a00003.html#eff99e4f0142cbb9977503844c0d45c9">00283</a> <a class="code" href="a00020.html" title="A node in a graph.">node</a> father (<span class="keyword">const</span> <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; n)<span class="keyword"> const</span>
  97. <a name="l00284"></a>00284 <span class="keyword"> </span>{assert (preds); <span class="keywordflow">return</span> (*preds)[n];}
  98. <a name="l00285"></a>00285
  99. <a name="l00289"></a><a class="code" href="a00003.html#a0b58a03ca2fc32117948ab27a806bd1">00289</a> <span class="keyword">typedef</span> list&lt;edge&gt;::const_iterator tree_edges_iterator;
  100. <a name="l00290"></a>00290
  101. <a name="l00300"></a><a class="code" href="a00003.html#50fbe73f807b39845bb2334548c9b8cb">00300</a> tree_edges_iterator tree_edges_begin ()<span class="keyword"> const </span>
  102. <a name="l00301"></a>00301 <span class="keyword"> </span>{<span class="keywordflow">return</span> tree.begin();}
  103. <a name="l00302"></a>00302
  104. <a name="l00309"></a><a class="code" href="a00003.html#609773f0e8e94dbdd330d5363c6d610c">00309</a> tree_edges_iterator tree_edges_end ()<span class="keyword"> const</span>
  105. <a name="l00310"></a>00310 <span class="keyword"> </span>{<span class="keywordflow">return</span> tree.end();}
  106. <a name="l00311"></a>00311
  107. <a name="l00315"></a><a class="code" href="a00003.html#cafce54954100cc7bc9f80eb318a7bee">00315</a> <span class="keyword">typedef</span> list&lt;node&gt;::const_iterator bfs_iterator;
  108. <a name="l00316"></a>00316
  109. <a name="l00322"></a><a class="code" href="a00003.html#7b83a88f71f210946be3ef212f4c2c37">00322</a> bfs_iterator begin ()<span class="keyword"> const </span>
  110. <a name="l00323"></a>00323 <span class="keyword"> </span>{<span class="keywordflow">return</span> bfs_order.begin();}
  111. <a name="l00324"></a>00324
  112. <a name="l00331"></a><a class="code" href="a00003.html#b1a9a095e1e757e8f71ba752bf0e898d">00331</a> bfs_iterator end ()<span class="keyword"> const </span>
  113. <a name="l00332"></a>00332 <span class="keyword"> </span>{<span class="keywordflow">return</span> bfs_order.end();}
  114. <a name="l00333"></a>00333
  115. <a name="l00337"></a><a class="code" href="a00003.html#ecd86c7c1f1086d4b6b11c2a0eb12afe">00337</a> <span class="keyword">typedef</span> list&lt;edge&gt;::const_iterator non_tree_edges_iterator;
  116. <a name="l00338"></a>00338
  117. <a name="l00345"></a><a class="code" href="a00003.html#0c7c3cb97a538907ddd0dc39bd884ffc">00345</a> non_tree_edges_iterator non_tree_edges_begin ()<span class="keyword"> const </span>
  118. <a name="l00346"></a>00346 <span class="keyword"> </span>{assert (non_tree); <span class="keywordflow">return</span> non_tree-&gt;begin(); }
  119. <a name="l00347"></a>00347
  120. <a name="l00355"></a><a class="code" href="a00003.html#37645ce2ffc6fb95afb56477025a64a8">00355</a> non_tree_edges_iterator non_tree_edges_end ()<span class="keyword"> const </span>
  121. <a name="l00356"></a>00356 <span class="keyword"> </span>{assert (non_tree); <span class="keywordflow">return</span> non_tree-&gt;end(); }
  122. <a name="l00357"></a>00357
  123. <a name="l00361"></a><a class="code" href="a00003.html#386ac6f3c63e38c3f5263e15c3ab9d01">00361</a> <span class="keyword">typedef</span> list&lt;bfs_iterator&gt;::const_iterator roots_iterator;
  124. <a name="l00362"></a>00362
  125. <a name="l00392"></a><a class="code" href="a00003.html#ae9df31655e7ef516426dfb99c2de888">00392</a> roots_iterator roots_begin ()<span class="keyword"> const </span>
  126. <a name="l00393"></a>00393 <span class="keyword"> </span>{<span class="keywordflow">return</span> roots.begin();}
  127. <a name="l00394"></a>00394
  128. <a name="l00401"></a><a class="code" href="a00003.html#31e77fd48aed1902d488a54f2f0ef6f8">00401</a> roots_iterator roots_end ()<span class="keyword"> const </span>
  129. <a name="l00402"></a>00402 <span class="keyword"> </span>{<span class="keywordflow">return</span> roots.end();}
  130. <a name="l00403"></a>00403
  131. <a name="l00410"></a><a class="code" href="a00003.html#50ca0ee41753b59ed7adfe9f60adf1e4">00410</a> <span class="keywordtype">int</span> number_of_reached_nodes ()<span class="keyword"> const</span>
  132. <a name="l00411"></a>00411 <span class="keyword"> </span>{<span class="keywordflow">return</span> reached_nodes;}
  133. <a name="l00412"></a>00412
  134. <a name="l00413"></a>00413 <span class="comment">//-----------------------------------------------------------------------</span>
  135. <a name="l00414"></a>00414 <span class="comment">// Handler</span>
  136. <a name="l00415"></a>00415 <span class="comment">//-----------------------------------------------------------------------</span>
  137. <a name="l00416"></a>00416
  138. <a name="l00422"></a><a class="code" href="a00003.html#b9af608a9b2d570a8688498bc3c53d7f">00422</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> init_handler (<a class="code" href="a00014.html" title="A directed or undirected graph.">graph</a>&amp; G) { };
  139. <a name="l00423"></a>00423
  140. <a name="l00429"></a><a class="code" href="a00003.html#d99b9aeb177a8a2e0ce1c3673082cddf">00429</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> end_handler (<a class="code" href="a00014.html" title="A directed or undirected graph.">graph</a>&amp; G) { };
  141. <a name="l00430"></a>00430
  142. <a name="l00437"></a><a class="code" href="a00003.html#3f161fe9f45e0a69892f355a889c3516">00437</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> popped_node_handler (<a class="code" href="a00014.html" title="A directed or undirected graph.">graph</a>&amp; G, <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; n) { };
  143. <a name="l00438"></a>00438
  144. <a name="l00448"></a><a class="code" href="a00003.html#db6604080f3376e9713e35cd9d7d8943">00448</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> finished_node_handler (<a class="code" href="a00014.html" title="A directed or undirected graph.">graph</a>&amp; G, <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; n) { };
  145. <a name="l00449"></a>00449
  146. <a name="l00460"></a><a class="code" href="a00003.html#0e45c8fe90e54bffe18b308868ccc111">00460</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> unused_node_handler (<a class="code" href="a00014.html" title="A directed or undirected graph.">graph</a>&amp; G, <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; n, <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; f) { };
  147. <a name="l00461"></a>00461
  148. <a name="l00472"></a><a class="code" href="a00003.html#d2cbb1e9c36f561da0c91fd142296f2a">00472</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> used_node_handler (<a class="code" href="a00014.html" title="A directed or undirected graph.">graph</a>&amp; G, <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; n, <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; f) { };
  149. <a name="l00473"></a>00473
  150. <a name="l00485"></a><a class="code" href="a00003.html#4f33f10121240390a5a50b5efc16c1f5">00485</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> new_start_handler (<a class="code" href="a00014.html" title="A directed or undirected graph.">graph</a>&amp; G, <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp; n) { };
  151. <a name="l00486"></a>00486
  152. <a name="l00487"></a>00487 <span class="keyword">private</span>:
  153. <a name="l00488"></a>00488
  154. <a name="l00489"></a>00489 <span class="keywordtype">void</span> bfs_sub (<a class="code" href="a00014.html" title="A directed or undirected graph.">graph</a>&amp;, <span class="keyword">const</span> <a class="code" href="a00020.html" title="A node in a graph.">node</a>&amp;, <a class="code" href="a00011.html" title="A specialized map with edges as keys.">edge_map&lt;int&gt;</a>*);
  155. <a name="l00490"></a>00490
  156. <a name="l00491"></a>00491 <span class="keyword">protected</span>:
  157. <a name="l00492"></a>00492
  158. <a name="l00493"></a>00493 <span class="comment">//-----------------------------------------------------------------------</span>
  159. <a name="l00494"></a>00494 <span class="comment">// Data</span>
  160. <a name="l00495"></a>00495 <span class="comment">//-----------------------------------------------------------------------</span>
  161. <a name="l00496"></a>00496
  162. <a name="l00500"></a><a class="code" href="a00003.html#5a4adad9562896536b8b58ab237e8478">00500</a> <span class="keywordtype">int</span> act_bfs_num;
  163. <a name="l00501"></a>00501
  164. <a name="l00505"></a><a class="code" href="a00003.html#ff965242124a7e3a6308de0c0ebfa741">00505</a> deque&lt;node&gt; qu;
  165. <a name="l00506"></a>00506
  166. <a name="l00512"></a><a class="code" href="a00003.html#35b0acd44887615142fd5f2fc6197452">00512</a> list&lt;node&gt; bfs_order;
  167. <a name="l00513"></a>00513
  168. <a name="l00519"></a><a class="code" href="a00003.html#a259f09ada9928cda41bcb540f685e80">00519</a> list&lt;edge&gt; tree;
  169. <a name="l00520"></a>00520
  170. <a name="l00524"></a><a class="code" href="a00003.html#59d0c5c5ad2715776b20b1aec03dbc3a">00524</a> <a class="code" href="a00021.html" title="A specialized map with nodes as keys.">node_map&lt;int&gt;</a> bfs_number;
  171. <a name="l00525"></a>00525
  172. <a name="l00529"></a><a class="code" href="a00003.html#c3db80b59d5db049199936445a6c2da8">00529</a> <span class="keywordtype">int</span> reached_nodes;
  173. <a name="l00530"></a>00530
  174. <a name="l00536"></a><a class="code" href="a00003.html#79d19028002766f7992fe94689217f99">00536</a> list&lt;bfs_iterator&gt; roots;
  175. <a name="l00537"></a>00537
  176. <a name="l00538"></a>00538 <span class="comment">//-----------------------------------------------------------------------</span>
  177. <a name="l00539"></a>00539 <span class="comment">// Optional</span>
  178. <a name="l00540"></a>00540 <span class="comment">//-----------------------------------------------------------------------</span>
  179. <a name="l00541"></a>00541
  180. <a name="l00547"></a><a class="code" href="a00003.html#6c08fbcc90d71f1cbdd03a1cdaa9dc99">00547</a> <span class="keywordtype">bool</span> whole_graph;
  181. <a name="l00548"></a>00548
  182. <a name="l00554"></a><a class="code" href="a00003.html#f2ab561d9e60a9fc2e25b02d1f807f96">00554</a> <a class="code" href="a00020.html" title="A node in a graph.">node</a> start;
  183. <a name="l00555"></a>00555
  184. <a name="l00561"></a><a class="code" href="a00003.html#ab92e9d128612c28324aafe4750dbc84">00561</a> <a class="code" href="a00021.html" title="A specialized map with nodes as keys.">node_map&lt;int&gt;</a>* level_number;
  185. <a name="l00562"></a>00562
  186. <a name="l00568"></a><a class="code" href="a00003.html#a6783e3e2ac4235403b37df3ee3ee968">00568</a> list&lt;edge&gt;* non_tree;
  187. <a name="l00569"></a>00569
  188. <a name="l00575"></a><a class="code" href="a00003.html#3bac5ed333bb78a30a67099c3b94aa0c">00575</a> <a class="code" href="a00021.html" title="A specialized map with nodes as keys.">node_map&lt;node&gt;</a>* preds;
  189. <a name="l00576"></a>00576 };
  190. <a name="l00577"></a>00577
  191. <a name="l00578"></a>00578 __GTL_END_NAMESPACE
  192. <a name="l00579"></a>00579
  193. <a name="l00580"></a>00580 <span class="preprocessor">#endif // GTL_BFS_H</span>
  194. <a name="l00581"></a>00581 <span class="preprocessor"></span>
  195. <a name="l00582"></a>00582 <span class="comment">//--------------------------------------------------------------------------</span>
  196. <a name="l00583"></a>00583 <span class="comment">// end of file</span>
  197. <a name="l00584"></a>00584 <span class="comment">//--------------------------------------------------------------------------</span>
  198. </pre></div> <p class="links">
  199. <a href="http://www.uni-passau.de/">University of Passau</a>
  200. &nbsp;-&nbsp;
  201. <a href="http://www.fmi.uni-passau.de/">FMI</a>
  202. &nbsp;-&nbsp;
  203. <a href="http://www.fmi.uni-passau.de/fmi/lehrstuehle/brandenburg/">Theoretical
  204. Computer Science</a>
  205. </p>
  206. <div class="copyright">
  207. Design &copy; 2002, 2003 <a href="mailto:raitner@fmi.uni-passau.de">Marcus Raitner</a>, University of Passau
  208. </div>
  209. </body>
  210. </html>