fibonacci_heap.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // (C) Copyright Jeremy Siek 2004.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_FIBONACCI_HEAP_HPP
  6. #define BOOST_FIBONACCI_HEAP_HPP
  7. #if defined(__sgi) && !defined(__GNUC__)
  8. #include <math.h>
  9. #else
  10. #include <boost/config/no_tr1/cmath.hpp>
  11. #endif
  12. #include <iosfwd>
  13. #include <vector>
  14. #include <functional>
  15. #include <boost/config.hpp>
  16. #include <boost/property_map/property_map.hpp>
  17. //
  18. // An adaptation of Knuth's Fibonacci heap implementation
  19. // in "The Stanford Graph Base", pages 475-482.
  20. //
  21. namespace boost
  22. {
  23. template < class T, class Compare = std::less< T >,
  24. class ID = identity_property_map >
  25. class fibonacci_heap
  26. {
  27. typedef typename boost::property_traits< ID >::value_type size_type;
  28. typedef T value_type;
  29. protected:
  30. typedef fibonacci_heap self;
  31. typedef std::vector< size_type > LinkVec;
  32. typedef typename LinkVec::iterator LinkIter;
  33. public:
  34. fibonacci_heap(
  35. size_type n, const Compare& cmp, const ID& id = identity_property_map())
  36. : _key(n)
  37. , _left(n)
  38. , _right(n)
  39. , _p(n)
  40. , _mark(n)
  41. , _degree(n)
  42. , _n(0)
  43. , _root(n)
  44. , _id(id)
  45. , _compare(cmp)
  46. , _child(n)
  47. ,
  48. #if defined(BOOST_MSVC) || defined(__ICL) // need a new macro?
  49. new_roots(size_type(log(float(n))) + 5)
  50. {
  51. }
  52. #else
  53. new_roots(size_type(std::log(float(n))) + 5)
  54. {
  55. }
  56. #endif
  57. // 33
  58. void push(const T& d)
  59. {
  60. ++_n;
  61. size_type v = get(_id, d);
  62. _key[v] = d;
  63. _p[v] = nil();
  64. _degree[v] = 0;
  65. _mark[v] = false;
  66. _child[v] = nil();
  67. if (_root == nil())
  68. {
  69. _root = _left[v] = _right[v] = v;
  70. // std::cout << "root added" << std::endl;
  71. }
  72. else
  73. {
  74. size_type u = _left[_root];
  75. _left[v] = u;
  76. _right[v] = _root;
  77. _left[_root] = _right[u] = v;
  78. if (_compare(d, _key[_root]))
  79. _root = v;
  80. // std::cout << "non-root node added" << std::endl;
  81. }
  82. }
  83. T& top() { return _key[_root]; }
  84. const T& top() const { return _key[_root]; }
  85. // 38
  86. void pop()
  87. {
  88. --_n;
  89. int h = -1;
  90. size_type v, w;
  91. if (_root != nil())
  92. {
  93. if (_degree[_root] == 0)
  94. {
  95. v = _right[_root];
  96. }
  97. else
  98. {
  99. w = _child[_root];
  100. v = _right[w];
  101. _right[w] = _right[_root];
  102. for (w = v; w != _right[_root]; w = _right[w])
  103. _p[w] = nil();
  104. }
  105. while (v != _root)
  106. {
  107. w = _right[v];
  108. add_tree_to_new_roots(v, new_roots.begin(), h);
  109. v = w;
  110. }
  111. rebuild_root_list(new_roots.begin(), h);
  112. }
  113. }
  114. // 39
  115. inline void add_tree_to_new_roots(size_type v, LinkIter new_roots, int& h)
  116. {
  117. int r;
  118. size_type u;
  119. r = _degree[v];
  120. while (1)
  121. {
  122. if (h < r)
  123. {
  124. do
  125. {
  126. ++h;
  127. new_roots[h] = (h == r ? v : nil());
  128. } while (h < r);
  129. break;
  130. }
  131. if (new_roots[r] == nil())
  132. {
  133. new_roots[r] = v;
  134. break;
  135. }
  136. u = new_roots[r];
  137. new_roots[r] = nil();
  138. if (_compare(_key[u], _key[v]))
  139. {
  140. _degree[v] = r;
  141. _mark[v] = false;
  142. std::swap(u, v);
  143. }
  144. make_child(u, v, r);
  145. ++r;
  146. }
  147. _degree[v] = r;
  148. _mark[v] = false;
  149. }
  150. // 40
  151. void make_child(size_type u, size_type v, size_type r)
  152. {
  153. if (r == 0)
  154. {
  155. _child[v] = u;
  156. _left[u] = u;
  157. _right[u] = u;
  158. }
  159. else
  160. {
  161. size_type t = _child[v];
  162. _right[u] = _right[t];
  163. _left[u] = t;
  164. _right[t] = u;
  165. _left[_right[u]] = u;
  166. }
  167. _p[u] = v;
  168. }
  169. // 41
  170. inline void rebuild_root_list(LinkIter new_roots, int& h)
  171. {
  172. size_type u, v, w;
  173. if (h < 0)
  174. _root = nil();
  175. else
  176. {
  177. T d;
  178. u = v = new_roots[h];
  179. d = _key[u];
  180. _root = u;
  181. for (h--; h >= 0; --h)
  182. if (new_roots[h] != nil())
  183. {
  184. w = new_roots[h];
  185. _left[w] = v;
  186. _right[v] = w;
  187. if (_compare(_key[w], d))
  188. {
  189. _root = w;
  190. d = _key[w];
  191. }
  192. v = w;
  193. }
  194. _right[v] = u;
  195. _left[u] = v;
  196. }
  197. }
  198. // 34
  199. void update(const T& d)
  200. {
  201. size_type v = get(_id, d);
  202. assert(!_compare(_key[v], d));
  203. _key[v] = d;
  204. size_type p = _p[v];
  205. if (p == nil())
  206. {
  207. if (_compare(d, _key[_root]))
  208. _root = v;
  209. }
  210. else if (_compare(d, _key[p]))
  211. while (1)
  212. {
  213. size_type r = _degree[p];
  214. if (r >= 2)
  215. remove_from_family(v, p);
  216. insert_into_forest(v, d);
  217. size_type pp = _p[p];
  218. if (pp == nil())
  219. {
  220. --_degree[p];
  221. break;
  222. }
  223. if (_mark[p] == false)
  224. {
  225. _mark[p] = true;
  226. --_degree[p];
  227. break;
  228. }
  229. else
  230. --_degree[p];
  231. v = p;
  232. p = pp;
  233. }
  234. }
  235. inline size_type size() const { return _n; }
  236. inline bool empty() const { return _n == 0; }
  237. void print(std::ostream& os)
  238. {
  239. if (_root != nil())
  240. {
  241. size_type i = _root;
  242. do
  243. {
  244. print_recur(i, os);
  245. os << std::endl;
  246. i = _right[i];
  247. } while (i != _root);
  248. }
  249. }
  250. protected:
  251. // 35
  252. inline void remove_from_family(size_type v, size_type p)
  253. {
  254. size_type u = _left[v];
  255. size_type w = _right[v];
  256. _right[u] = w;
  257. _left[w] = u;
  258. if (_child[p] == v)
  259. _child[p] = w;
  260. }
  261. // 36
  262. inline void insert_into_forest(size_type v, const T& d)
  263. {
  264. _p[v] = nil();
  265. size_type u = _left[_root];
  266. _left[v] = u;
  267. _right[v] = _root;
  268. _left[_root] = _right[u] = v;
  269. if (_compare(d, _key[_root]))
  270. _root = v;
  271. }
  272. void print_recur(size_type x, std::ostream& os)
  273. {
  274. if (x != nil())
  275. {
  276. os << x;
  277. if (_degree[x] > 0)
  278. {
  279. os << "(";
  280. size_type i = _child[x];
  281. do
  282. {
  283. print_recur(i, os);
  284. os << " ";
  285. i = _right[i];
  286. } while (i != _child[x]);
  287. os << ")";
  288. }
  289. }
  290. }
  291. size_type nil() const { return _left.size(); }
  292. std::vector< T > _key;
  293. LinkVec _left, _right, _p;
  294. std::vector< bool > _mark;
  295. LinkVec _degree;
  296. size_type _n, _root;
  297. ID _id;
  298. Compare _compare;
  299. LinkVec _child;
  300. LinkVec new_roots;
  301. };
  302. } // namespace boost
  303. #endif // BOOST_FIBONACCI_HEAP_HPP