fibheap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /* A Fibonacci heap datatype.
  2. Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
  3. Contributed by Daniel Berlin (dan@cgsoftware.com).
  4. This file is part of GNU CC.
  5. GNU CC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU CC is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU CC; see the file COPYING. If not, write to
  15. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #ifdef HAVE_LIMITS_H
  21. #include <limits.h>
  22. #endif
  23. #ifdef HAVE_STDLIB_H
  24. #include <stdlib.h>
  25. #endif
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #include "libiberty.h"
  30. #include "fibheap.h"
  31. #define FIBHEAPKEY_MIN LONG_MIN
  32. static void fibheap_ins_root (fibheap_t, fibnode_t);
  33. static void fibheap_rem_root (fibheap_t, fibnode_t);
  34. static void fibheap_consolidate (fibheap_t);
  35. static void fibheap_link (fibheap_t, fibnode_t, fibnode_t);
  36. static void fibheap_cut (fibheap_t, fibnode_t, fibnode_t);
  37. static void fibheap_cascading_cut (fibheap_t, fibnode_t);
  38. static fibnode_t fibheap_extr_min_node (fibheap_t);
  39. static int fibheap_compare (fibheap_t, fibnode_t, fibnode_t);
  40. static int fibheap_comp_data (fibheap_t, fibheapkey_t, void *, fibnode_t);
  41. static fibnode_t fibnode_new (void);
  42. static void fibnode_insert_after (fibnode_t, fibnode_t);
  43. #define fibnode_insert_before(a, b) fibnode_insert_after (a->left, b)
  44. static fibnode_t fibnode_remove (fibnode_t);
  45. /* Create a new fibonacci heap. */
  46. fibheap_t
  47. fibheap_new (void)
  48. {
  49. return (fibheap_t) xcalloc (1, sizeof (struct fibheap));
  50. }
  51. /* Create a new fibonacci heap node. */
  52. static fibnode_t
  53. fibnode_new (void)
  54. {
  55. fibnode_t node;
  56. node = (fibnode_t) xcalloc (1, sizeof *node);
  57. node->left = node;
  58. node->right = node;
  59. return node;
  60. }
  61. static inline int
  62. fibheap_compare (fibheap_t heap ATTRIBUTE_UNUSED, fibnode_t a, fibnode_t b)
  63. {
  64. if (a->key < b->key)
  65. return -1;
  66. if (a->key > b->key)
  67. return 1;
  68. return 0;
  69. }
  70. static inline int
  71. fibheap_comp_data (fibheap_t heap, fibheapkey_t key, void *data, fibnode_t b)
  72. {
  73. struct fibnode a;
  74. a.key = key;
  75. a.data = data;
  76. return fibheap_compare (heap, &a, b);
  77. }
  78. /* Insert DATA, with priority KEY, into HEAP. */
  79. fibnode_t
  80. fibheap_insert (fibheap_t heap, fibheapkey_t key, void *data)
  81. {
  82. fibnode_t node;
  83. /* Create the new node. */
  84. node = fibnode_new ();
  85. /* Set the node's data. */
  86. node->data = data;
  87. node->key = key;
  88. /* Insert it into the root list. */
  89. fibheap_ins_root (heap, node);
  90. /* If their was no minimum, or this key is less than the min,
  91. it's the new min. */
  92. if (heap->min == NULL || node->key < heap->min->key)
  93. heap->min = node;
  94. heap->nodes++;
  95. return node;
  96. }
  97. /* Return the data of the minimum node (if we know it). */
  98. void *
  99. fibheap_min (fibheap_t heap)
  100. {
  101. /* If there is no min, we can't easily return it. */
  102. if (heap->min == NULL)
  103. return NULL;
  104. return heap->min->data;
  105. }
  106. /* Return the key of the minimum node (if we know it). */
  107. fibheapkey_t
  108. fibheap_min_key (fibheap_t heap)
  109. {
  110. /* If there is no min, we can't easily return it. */
  111. if (heap->min == NULL)
  112. return 0;
  113. return heap->min->key;
  114. }
  115. /* Union HEAPA and HEAPB into a new heap. */
  116. fibheap_t
  117. fibheap_union (fibheap_t heapa, fibheap_t heapb)
  118. {
  119. fibnode_t a_root, b_root, temp;
  120. /* If one of the heaps is empty, the union is just the other heap. */
  121. if ((a_root = heapa->root) == NULL)
  122. {
  123. free (heapa);
  124. return heapb;
  125. }
  126. if ((b_root = heapb->root) == NULL)
  127. {
  128. free (heapb);
  129. return heapa;
  130. }
  131. /* Merge them to the next nodes on the opposite chain. */
  132. a_root->left->right = b_root;
  133. b_root->left->right = a_root;
  134. temp = a_root->left;
  135. a_root->left = b_root->left;
  136. b_root->left = temp;
  137. heapa->nodes += heapb->nodes;
  138. /* And set the new minimum, if it's changed. */
  139. if (fibheap_compare (heapa, heapb->min, heapa->min) < 0)
  140. heapa->min = heapb->min;
  141. free (heapb);
  142. return heapa;
  143. }
  144. /* Extract the data of the minimum node from HEAP. */
  145. void *
  146. fibheap_extract_min (fibheap_t heap)
  147. {
  148. fibnode_t z;
  149. void *ret = NULL;
  150. /* If we don't have a min set, it means we have no nodes. */
  151. if (heap->min != NULL)
  152. {
  153. /* Otherwise, extract the min node, free the node, and return the
  154. node's data. */
  155. z = fibheap_extr_min_node (heap);
  156. ret = z->data;
  157. free (z);
  158. }
  159. return ret;
  160. }
  161. /* Replace both the KEY and the DATA associated with NODE. */
  162. void *
  163. fibheap_replace_key_data (fibheap_t heap, fibnode_t node,
  164. fibheapkey_t key, void *data)
  165. {
  166. void *odata;
  167. fibheapkey_t okey;
  168. fibnode_t y;
  169. /* If we wanted to, we could actually do a real increase by redeleting and
  170. inserting. However, this would require O (log n) time. So just bail out
  171. for now. */
  172. if (fibheap_comp_data (heap, key, data, node) > 0)
  173. return NULL;
  174. odata = node->data;
  175. okey = node->key;
  176. node->data = data;
  177. node->key = key;
  178. y = node->parent;
  179. /* Short-circuit if the key is the same, as we then don't have to
  180. do anything. Except if we're trying to force the new node to
  181. be the new minimum for delete. */
  182. if (okey == key && okey != FIBHEAPKEY_MIN)
  183. return odata;
  184. /* These two compares are specifically <= 0 to make sure that in the case
  185. of equality, a node we replaced the data on, becomes the new min. This
  186. is needed so that delete's call to extractmin gets the right node. */
  187. if (y != NULL && fibheap_compare (heap, node, y) <= 0)
  188. {
  189. fibheap_cut (heap, node, y);
  190. fibheap_cascading_cut (heap, y);
  191. }
  192. if (fibheap_compare (heap, node, heap->min) <= 0)
  193. heap->min = node;
  194. return odata;
  195. }
  196. /* Replace the DATA associated with NODE. */
  197. void *
  198. fibheap_replace_data (fibheap_t heap, fibnode_t node, void *data)
  199. {
  200. return fibheap_replace_key_data (heap, node, node->key, data);
  201. }
  202. /* Replace the KEY associated with NODE. */
  203. fibheapkey_t
  204. fibheap_replace_key (fibheap_t heap, fibnode_t node, fibheapkey_t key)
  205. {
  206. int okey = node->key;
  207. fibheap_replace_key_data (heap, node, key, node->data);
  208. return okey;
  209. }
  210. /* Delete NODE from HEAP. */
  211. void *
  212. fibheap_delete_node (fibheap_t heap, fibnode_t node)
  213. {
  214. void *ret = node->data;
  215. /* To perform delete, we just make it the min key, and extract. */
  216. fibheap_replace_key (heap, node, FIBHEAPKEY_MIN);
  217. if (node != heap->min)
  218. {
  219. fprintf (stderr, "Can't force minimum on fibheap.\n");
  220. abort ();
  221. }
  222. fibheap_extract_min (heap);
  223. return ret;
  224. }
  225. /* Delete HEAP. */
  226. void
  227. fibheap_delete (fibheap_t heap)
  228. {
  229. while (heap->min != NULL)
  230. free (fibheap_extr_min_node (heap));
  231. free (heap);
  232. }
  233. /* Determine if HEAP is empty. */
  234. int
  235. fibheap_empty (fibheap_t heap)
  236. {
  237. return heap->nodes == 0;
  238. }
  239. /* Extract the minimum node of the heap. */
  240. static fibnode_t
  241. fibheap_extr_min_node (fibheap_t heap)
  242. {
  243. fibnode_t ret = heap->min;
  244. fibnode_t x, y, orig;
  245. /* Attach the child list of the minimum node to the root list of the heap.
  246. If there is no child list, we don't do squat. */
  247. for (x = ret->child, orig = NULL; x != orig && x != NULL; x = y)
  248. {
  249. if (orig == NULL)
  250. orig = x;
  251. y = x->right;
  252. x->parent = NULL;
  253. fibheap_ins_root (heap, x);
  254. }
  255. /* Remove the old root. */
  256. fibheap_rem_root (heap, ret);
  257. heap->nodes--;
  258. /* If we are left with no nodes, then the min is NULL. */
  259. if (heap->nodes == 0)
  260. heap->min = NULL;
  261. else
  262. {
  263. /* Otherwise, consolidate to find new minimum, as well as do the reorg
  264. work that needs to be done. */
  265. heap->min = ret->right;
  266. fibheap_consolidate (heap);
  267. }
  268. return ret;
  269. }
  270. /* Insert NODE into the root list of HEAP. */
  271. static void
  272. fibheap_ins_root (fibheap_t heap, fibnode_t node)
  273. {
  274. /* If the heap is currently empty, the new node becomes the singleton
  275. circular root list. */
  276. if (heap->root == NULL)
  277. {
  278. heap->root = node;
  279. node->left = node;
  280. node->right = node;
  281. return;
  282. }
  283. /* Otherwise, insert it in the circular root list between the root
  284. and it's right node. */
  285. fibnode_insert_after (heap->root, node);
  286. }
  287. /* Remove NODE from the rootlist of HEAP. */
  288. static void
  289. fibheap_rem_root (fibheap_t heap, fibnode_t node)
  290. {
  291. if (node->left == node)
  292. heap->root = NULL;
  293. else
  294. heap->root = fibnode_remove (node);
  295. }
  296. /* Consolidate the heap. */
  297. static void
  298. fibheap_consolidate (fibheap_t heap)
  299. {
  300. fibnode_t a[1 + 8 * sizeof (long)];
  301. fibnode_t w;
  302. fibnode_t y;
  303. fibnode_t x;
  304. int i;
  305. int d;
  306. int D;
  307. D = 1 + 8 * sizeof (long);
  308. memset (a, 0, sizeof (fibnode_t) * D);
  309. while ((w = heap->root) != NULL)
  310. {
  311. x = w;
  312. fibheap_rem_root (heap, w);
  313. d = x->degree;
  314. while (a[d] != NULL)
  315. {
  316. y = a[d];
  317. if (fibheap_compare (heap, x, y) > 0)
  318. {
  319. fibnode_t temp;
  320. temp = x;
  321. x = y;
  322. y = temp;
  323. }
  324. fibheap_link (heap, y, x);
  325. a[d] = NULL;
  326. d++;
  327. }
  328. a[d] = x;
  329. }
  330. heap->min = NULL;
  331. for (i = 0; i < D; i++)
  332. if (a[i] != NULL)
  333. {
  334. fibheap_ins_root (heap, a[i]);
  335. if (heap->min == NULL || fibheap_compare (heap, a[i], heap->min) < 0)
  336. heap->min = a[i];
  337. }
  338. }
  339. /* Make NODE a child of PARENT. */
  340. static void
  341. fibheap_link (fibheap_t heap ATTRIBUTE_UNUSED,
  342. fibnode_t node, fibnode_t parent)
  343. {
  344. if (parent->child == NULL)
  345. parent->child = node;
  346. else
  347. fibnode_insert_before (parent->child, node);
  348. node->parent = parent;
  349. parent->degree++;
  350. node->mark = 0;
  351. }
  352. /* Remove NODE from PARENT's child list. */
  353. static void
  354. fibheap_cut (fibheap_t heap, fibnode_t node, fibnode_t parent)
  355. {
  356. fibnode_remove (node);
  357. parent->degree--;
  358. fibheap_ins_root (heap, node);
  359. node->parent = NULL;
  360. node->mark = 0;
  361. }
  362. static void
  363. fibheap_cascading_cut (fibheap_t heap, fibnode_t y)
  364. {
  365. fibnode_t z;
  366. while ((z = y->parent) != NULL)
  367. {
  368. if (y->mark == 0)
  369. {
  370. y->mark = 1;
  371. return;
  372. }
  373. else
  374. {
  375. fibheap_cut (heap, y, z);
  376. y = z;
  377. }
  378. }
  379. }
  380. static void
  381. fibnode_insert_after (fibnode_t a, fibnode_t b)
  382. {
  383. if (a == a->right)
  384. {
  385. a->right = b;
  386. a->left = b;
  387. b->right = a;
  388. b->left = a;
  389. }
  390. else
  391. {
  392. b->right = a->right;
  393. a->right->left = b;
  394. a->right = b;
  395. b->left = a;
  396. }
  397. }
  398. static fibnode_t
  399. fibnode_remove (fibnode_t node)
  400. {
  401. fibnode_t ret;
  402. if (node == node->left)
  403. ret = NULL;
  404. else
  405. ret = node->left;
  406. if (node->parent != NULL && node->parent->child == node)
  407. node->parent->child = ret;
  408. node->right->left = node->left;
  409. node->left->right = node->right;
  410. node->parent = NULL;
  411. node->left = node;
  412. node->right = node;
  413. return ret;
  414. }