fibonacci_heap.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /* Vector API for GNU compiler.
  2. Copyright (C) 1998-2015 Free Software Foundation, Inc.
  3. Contributed by Daniel Berlin (dan@cgsoftware.com).
  4. Re-implemented in C++ by Martin Liska <mliska@suse.cz>
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. /* Fibonacci heaps are somewhat complex, but, there's an article in
  18. DDJ that explains them pretty well:
  19. http://www.ddj.com/articles/1997/9701/9701o/9701o.htm?topic=algoritms
  20. Introduction to algorithms by Corman and Rivest also goes over them.
  21. The original paper that introduced them is "Fibonacci heaps and their
  22. uses in improved network optimization algorithms" by Tarjan and
  23. Fredman (JACM 34(3), July 1987).
  24. Amortized and real worst case time for operations:
  25. ExtractMin: O(lg n) amortized. O(n) worst case.
  26. DecreaseKey: O(1) amortized. O(lg n) worst case.
  27. Insert: O(1) amortized.
  28. Union: O(1) amortized. */
  29. #ifndef GCC_FIBONACCI_HEAP_H
  30. #define GCC_FIBONACCI_HEAP_H
  31. /* Forward definition. */
  32. template<class K, class V>
  33. class fibonacci_heap;
  34. /* Fibonacci heap node class. */
  35. template<class K, class V>
  36. class fibonacci_node
  37. {
  38. typedef fibonacci_node<K,V> fibonacci_node_t;
  39. friend class fibonacci_heap<K,V>;
  40. public:
  41. /* Default constructor. */
  42. fibonacci_node (): m_parent (NULL), m_child (NULL), m_left (this),
  43. m_right (this), m_degree (0), m_mark (0)
  44. {
  45. }
  46. /* Constructor for a node with given KEY. */
  47. fibonacci_node (K key): m_parent (NULL), m_child (NULL), m_left (this),
  48. m_right (this), m_key (key),
  49. m_degree (0), m_mark (0)
  50. {
  51. }
  52. /* Compare fibonacci node with OTHER node. */
  53. int compare (fibonacci_node_t *other)
  54. {
  55. if (m_key < other->m_key)
  56. return -1;
  57. if (m_key > other->m_key)
  58. return 1;
  59. return 0;
  60. }
  61. /* Compare the node with a given KEY. */
  62. int compare_data (K key)
  63. {
  64. return fibonacci_node_t (key).compare (this);
  65. }
  66. /* Remove fibonacci heap node. */
  67. fibonacci_node_t *remove ();
  68. /* Link the node with PARENT. */
  69. void link (fibonacci_node_t *parent);
  70. /* Return key associated with the node. */
  71. K get_key ()
  72. {
  73. return m_key;
  74. }
  75. /* Return data associated with the node. */
  76. V *get_data ()
  77. {
  78. return m_data;
  79. }
  80. private:
  81. /* Put node B after this node. */
  82. void insert_after (fibonacci_node_t *b);
  83. /* Insert fibonacci node B after this node. */
  84. void insert_before (fibonacci_node_t *b)
  85. {
  86. m_left->insert_after (b);
  87. }
  88. /* Parent node. */
  89. fibonacci_node *m_parent;
  90. /* Child node. */
  91. fibonacci_node *m_child;
  92. /* Left sibling. */
  93. fibonacci_node *m_left;
  94. /* Right node. */
  95. fibonacci_node *m_right;
  96. /* Key associated with node. */
  97. K m_key;
  98. /* Data associated with node. */
  99. V *m_data;
  100. #if defined (__GNUC__) && (!defined (SIZEOF_INT) || SIZEOF_INT < 4)
  101. /* Degree of the node. */
  102. __extension__ unsigned long int m_degree : 31;
  103. /* Mark of the node. */
  104. __extension__ unsigned long int m_mark : 1;
  105. #else
  106. /* Degree of the node. */
  107. unsigned int m_degree : 31;
  108. /* Mark of the node. */
  109. unsigned int m_mark : 1;
  110. #endif
  111. };
  112. /* Fibonacci heap class. */
  113. template<class K, class V>
  114. class fibonacci_heap
  115. {
  116. typedef fibonacci_node<K,V> fibonacci_node_t;
  117. friend class fibonacci_node<K,V>;
  118. public:
  119. /* Default constructor. */
  120. fibonacci_heap (K global_min_key): m_nodes (0), m_min (NULL), m_root (NULL),
  121. m_global_min_key (global_min_key)
  122. {
  123. }
  124. /* Destructor. */
  125. ~fibonacci_heap ()
  126. {
  127. while (m_min != NULL)
  128. delete (extract_minimum_node ());
  129. }
  130. /* Insert new node given by KEY and DATA associated with the key. */
  131. fibonacci_node_t *insert (K key, V *data);
  132. /* Return true if no entry is present. */
  133. bool empty ()
  134. {
  135. return m_nodes == 0;
  136. }
  137. /* Return the number of nodes. */
  138. size_t nodes ()
  139. {
  140. return m_nodes;
  141. }
  142. /* Return minimal key presented in the heap. */
  143. K min_key ()
  144. {
  145. if (m_min == NULL)
  146. gcc_unreachable ();
  147. return m_min->m_key;
  148. }
  149. /* For given NODE, set new KEY value. */
  150. K replace_key (fibonacci_node_t *node, K key)
  151. {
  152. K okey = node->m_key;
  153. replace_key_data (node, key, node->m_data);
  154. return okey;
  155. }
  156. /* For given NODE, decrease value to new KEY. */
  157. K decrease_key (fibonacci_node_t *node, K key)
  158. {
  159. gcc_assert (key <= node->m_key);
  160. return replace_key (node, key);
  161. }
  162. /* For given NODE, set new KEY and DATA value. */
  163. V *replace_key_data (fibonacci_node_t *node, K key, V *data);
  164. /* Extract minimum node in the heap. If RELEASE is specified,
  165. memory is released. */
  166. V *extract_min (bool release = true);
  167. /* Return value associated with minimum node in the heap. */
  168. V *min ()
  169. {
  170. if (m_min == NULL)
  171. return NULL;
  172. return m_min->m_data;
  173. }
  174. /* Replace data associated with NODE and replace it with DATA. */
  175. V *replace_data (fibonacci_node_t *node, V *data)
  176. {
  177. return replace_key_data (node, node->m_key, data);
  178. }
  179. /* Delete NODE in the heap. */
  180. V *delete_node (fibonacci_node_t *node, bool release = true);
  181. /* Union the heap with HEAPB. */
  182. fibonacci_heap *union_with (fibonacci_heap *heapb);
  183. private:
  184. /* Insert new NODE given by KEY and DATA associated with the key. */
  185. fibonacci_node_t *insert (fibonacci_node_t *node, K key, V *data);
  186. /* Insert it into the root list. */
  187. void insert_root (fibonacci_node_t *node);
  188. /* Remove NODE from PARENT's child list. */
  189. void cut (fibonacci_node_t *node, fibonacci_node_t *parent);
  190. /* Process cut of node Y and do it recursivelly. */
  191. void cascading_cut (fibonacci_node_t *y);
  192. /* Extract minimum node from the heap. */
  193. fibonacci_node_t * extract_minimum_node ();
  194. /* Remove root NODE from the heap. */
  195. void remove_root (fibonacci_node_t *node);
  196. /* Consolidate heap. */
  197. void consolidate ();
  198. /* Number of nodes. */
  199. size_t m_nodes;
  200. /* Minimum node of the heap. */
  201. fibonacci_node_t *m_min;
  202. /* Root node of the heap. */
  203. fibonacci_node_t *m_root;
  204. /* Global minimum given in the heap construction. */
  205. K m_global_min_key;
  206. };
  207. /* Remove fibonacci heap node. */
  208. template<class K, class V>
  209. fibonacci_node<K,V> *
  210. fibonacci_node<K,V>::remove ()
  211. {
  212. fibonacci_node<K,V> *ret;
  213. if (this == m_left)
  214. ret = NULL;
  215. else
  216. ret = m_left;
  217. if (m_parent != NULL && m_parent->m_child == this)
  218. m_parent->m_child = ret;
  219. m_right->m_left = m_left;
  220. m_left->m_right = m_right;
  221. m_parent = NULL;
  222. m_left = this;
  223. m_right = this;
  224. return ret;
  225. }
  226. /* Link the node with PARENT. */
  227. template<class K, class V>
  228. void
  229. fibonacci_node<K,V>::link (fibonacci_node<K,V> *parent)
  230. {
  231. if (parent->m_child == NULL)
  232. parent->m_child = this;
  233. else
  234. parent->m_child->insert_before (this);
  235. m_parent = parent;
  236. parent->m_degree++;
  237. m_mark = 0;
  238. }
  239. /* Put node B after this node. */
  240. template<class K, class V>
  241. void
  242. fibonacci_node<K,V>::insert_after (fibonacci_node<K,V> *b)
  243. {
  244. fibonacci_node<K,V> *a = this;
  245. if (a == a->m_right)
  246. {
  247. a->m_right = b;
  248. a->m_left = b;
  249. b->m_right = a;
  250. b->m_left = a;
  251. }
  252. else
  253. {
  254. b->m_right = a->m_right;
  255. a->m_right->m_left = b;
  256. a->m_right = b;
  257. b->m_left = a;
  258. }
  259. }
  260. /* Insert new node given by KEY and DATA associated with the key. */
  261. template<class K, class V>
  262. fibonacci_node<K,V>*
  263. fibonacci_heap<K,V>::insert (K key, V *data)
  264. {
  265. /* Create the new node. */
  266. fibonacci_node<K,V> *node = new fibonacci_node_t ();
  267. return insert (node, key, data);
  268. }
  269. /* Insert new NODE given by KEY and DATA associated with the key. */
  270. template<class K, class V>
  271. fibonacci_node<K,V>*
  272. fibonacci_heap<K,V>::insert (fibonacci_node_t *node, K key, V *data)
  273. {
  274. /* Set the node's data. */
  275. node->m_data = data;
  276. node->m_key = key;
  277. /* Insert it into the root list. */
  278. insert_root (node);
  279. /* If their was no minimum, or this key is less than the min,
  280. it's the new min. */
  281. if (m_min == NULL || node->m_key < m_min->m_key)
  282. m_min = node;
  283. m_nodes++;
  284. return node;
  285. }
  286. /* For given NODE, set new KEY and DATA value. */
  287. template<class K, class V>
  288. V*
  289. fibonacci_heap<K,V>::replace_key_data (fibonacci_node<K,V> *node, K key,
  290. V *data)
  291. {
  292. K okey;
  293. fibonacci_node<K,V> *y;
  294. V *odata = node->m_data;
  295. /* If we wanted to, we do a real increase by redeleting and
  296. inserting. */
  297. if (node->compare_data (key) > 0)
  298. {
  299. delete_node (node, false);
  300. node = new (node) fibonacci_node_t ();
  301. insert (node, key, data);
  302. return odata;
  303. }
  304. okey = node->m_key;
  305. node->m_data = data;
  306. node->m_key = key;
  307. y = node->m_parent;
  308. /* Short-circuit if the key is the same, as we then don't have to
  309. do anything. Except if we're trying to force the new node to
  310. be the new minimum for delete. */
  311. if (okey == key && okey != m_global_min_key)
  312. return odata;
  313. /* These two compares are specifically <= 0 to make sure that in the case
  314. of equality, a node we replaced the data on, becomes the new min. This
  315. is needed so that delete's call to extractmin gets the right node. */
  316. if (y != NULL && node->compare (y) <= 0)
  317. {
  318. cut (node, y);
  319. cascading_cut (y);
  320. }
  321. if (node->compare (m_min) <= 0)
  322. m_min = node;
  323. return odata;
  324. }
  325. /* Extract minimum node in the heap. */
  326. template<class K, class V>
  327. V*
  328. fibonacci_heap<K,V>::extract_min (bool release)
  329. {
  330. fibonacci_node<K,V> *z;
  331. V *ret = NULL;
  332. /* If we don't have a min set, it means we have no nodes. */
  333. if (m_min != NULL)
  334. {
  335. /* Otherwise, extract the min node, free the node, and return the
  336. node's data. */
  337. z = extract_minimum_node ();
  338. ret = z->m_data;
  339. if (release)
  340. delete (z);
  341. }
  342. return ret;
  343. }
  344. /* Delete NODE in the heap, if RELEASE is specified memory is released. */
  345. template<class K, class V>
  346. V*
  347. fibonacci_heap<K,V>::delete_node (fibonacci_node<K,V> *node, bool release)
  348. {
  349. V *ret = node->m_data;
  350. /* To perform delete, we just make it the min key, and extract. */
  351. replace_key (node, m_global_min_key);
  352. if (node != m_min)
  353. {
  354. fprintf (stderr, "Can't force minimum on fibheap.\n");
  355. abort ();
  356. }
  357. extract_min (release);
  358. return ret;
  359. }
  360. /* Union the heap with HEAPB. */
  361. template<class K, class V>
  362. fibonacci_heap<K,V>*
  363. fibonacci_heap<K,V>::union_with (fibonacci_heap<K,V> *heapb)
  364. {
  365. fibonacci_heap<K,V> *heapa = this;
  366. fibonacci_node<K,V> *a_root, *b_root, *temp;
  367. /* If one of the heaps is empty, the union is just the other heap. */
  368. if ((a_root = heapa->m_root) == NULL)
  369. {
  370. delete (heapa);
  371. return heapb;
  372. }
  373. if ((b_root = heapb->m_root) == NULL)
  374. {
  375. delete (heapb);
  376. return heapa;
  377. }
  378. /* Merge them to the next nodes on the opposite chain. */
  379. a_root->m_left->m_right = b_root;
  380. b_root->m_left->m_right = a_root;
  381. temp = a_root->m_left;
  382. a_root->m_left = b_root->m_left;
  383. b_root->m_left = temp;
  384. heapa->m_nodes += heapb->m_nodes;
  385. /* And set the new minimum, if it's changed. */
  386. if (heapb->min->compare (heapa->min) < 0)
  387. heapa->m_min = heapb->m_min;
  388. delete (heapb);
  389. return heapa;
  390. }
  391. /* Insert it into the root list. */
  392. template<class K, class V>
  393. void
  394. fibonacci_heap<K,V>::insert_root (fibonacci_node_t *node)
  395. {
  396. /* If the heap is currently empty, the new node becomes the singleton
  397. circular root list. */
  398. if (m_root == NULL)
  399. {
  400. m_root = node;
  401. node->m_left = node;
  402. node->m_right = node;
  403. return;
  404. }
  405. /* Otherwise, insert it in the circular root list between the root
  406. and it's right node. */
  407. m_root->insert_after (node);
  408. }
  409. /* Remove NODE from PARENT's child list. */
  410. template<class K, class V>
  411. void
  412. fibonacci_heap<K,V>::cut (fibonacci_node<K,V> *node,
  413. fibonacci_node<K,V> *parent)
  414. {
  415. node->remove ();
  416. parent->m_degree--;
  417. insert_root (node);
  418. node->m_parent = NULL;
  419. node->m_mark = 0;
  420. }
  421. /* Process cut of node Y and do it recursivelly. */
  422. template<class K, class V>
  423. void
  424. fibonacci_heap<K,V>::cascading_cut (fibonacci_node<K,V> *y)
  425. {
  426. fibonacci_node<K,V> *z;
  427. while ((z = y->m_parent) != NULL)
  428. {
  429. if (y->m_mark == 0)
  430. {
  431. y->m_mark = 1;
  432. return;
  433. }
  434. else
  435. {
  436. cut (y, z);
  437. y = z;
  438. }
  439. }
  440. }
  441. /* Extract minimum node from the heap. */
  442. template<class K, class V>
  443. fibonacci_node<K,V>*
  444. fibonacci_heap<K,V>::extract_minimum_node ()
  445. {
  446. fibonacci_node<K,V> *ret = m_min;
  447. fibonacci_node<K,V> *x, *y, *orig;
  448. /* Attach the child list of the minimum node to the root list of the heap.
  449. If there is no child list, we don't do squat. */
  450. for (x = ret->m_child, orig = NULL; x != orig && x != NULL; x = y)
  451. {
  452. if (orig == NULL)
  453. orig = x;
  454. y = x->m_right;
  455. x->m_parent = NULL;
  456. insert_root (x);
  457. }
  458. /* Remove the old root. */
  459. remove_root (ret);
  460. m_nodes--;
  461. /* If we are left with no nodes, then the min is NULL. */
  462. if (m_nodes == 0)
  463. m_min = NULL;
  464. else
  465. {
  466. /* Otherwise, consolidate to find new minimum, as well as do the reorg
  467. work that needs to be done. */
  468. m_min = ret->m_right;
  469. consolidate ();
  470. }
  471. return ret;
  472. }
  473. /* Remove root NODE from the heap. */
  474. template<class K, class V>
  475. void
  476. fibonacci_heap<K,V>::remove_root (fibonacci_node<K,V> *node)
  477. {
  478. if (node->m_left == node)
  479. m_root = NULL;
  480. else
  481. m_root = node->remove ();
  482. }
  483. /* Consolidate heap. */
  484. template<class K, class V>
  485. void fibonacci_heap<K,V>::consolidate ()
  486. {
  487. int D = 1 + 8 * sizeof (long);
  488. auto_vec<fibonacci_node<K,V> *> a (D);
  489. a.safe_grow_cleared (D);
  490. fibonacci_node<K,V> *w, *x, *y;
  491. int i, d;
  492. while ((w = m_root) != NULL)
  493. {
  494. x = w;
  495. remove_root (w);
  496. d = x->m_degree;
  497. while (a[d] != NULL)
  498. {
  499. y = a[d];
  500. if (x->compare (y) > 0)
  501. std::swap (x, y);
  502. y->link (x);
  503. a[d] = NULL;
  504. d++;
  505. }
  506. a[d] = x;
  507. }
  508. m_min = NULL;
  509. for (i = 0; i < D; i++)
  510. if (a[i] != NULL)
  511. {
  512. insert_root (a[i]);
  513. if (m_min == NULL || a[i]->compare (m_min) < 0)
  514. m_min = a[i];
  515. }
  516. }
  517. #endif // GCC_FIBONACCI_HEAP_H