fibheap.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* A Fibonacci heap datatype.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Daniel Berlin (dan@cgsoftware.com).
  4. This file is part of GCC.
  5. GCC 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. GCC 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 GCC; 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. /* 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(2) amortized. O(1) actual.
  28. Union: O(1) amortized. O(1) actual. */
  29. #ifndef _FIBHEAP_H_
  30. #define _FIBHEAP_H_
  31. #include "ansidecl.h"
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. typedef long fibheapkey_t;
  36. typedef struct fibheap
  37. {
  38. size_t nodes;
  39. struct fibnode *min;
  40. struct fibnode *root;
  41. } *fibheap_t;
  42. typedef struct fibnode
  43. {
  44. struct fibnode *parent;
  45. struct fibnode *child;
  46. struct fibnode *left;
  47. struct fibnode *right;
  48. fibheapkey_t key;
  49. void *data;
  50. #if defined (__GNUC__) && (!defined (SIZEOF_INT) || SIZEOF_INT < 4)
  51. __extension__ unsigned long int degree : 31;
  52. __extension__ unsigned long int mark : 1;
  53. #else
  54. unsigned int degree : 31;
  55. unsigned int mark : 1;
  56. #endif
  57. } *fibnode_t;
  58. extern fibheap_t fibheap_new (void);
  59. extern fibnode_t fibheap_insert (fibheap_t, fibheapkey_t, void *);
  60. extern int fibheap_empty (fibheap_t);
  61. extern fibheapkey_t fibheap_min_key (fibheap_t);
  62. extern fibheapkey_t fibheap_replace_key (fibheap_t, fibnode_t,
  63. fibheapkey_t);
  64. extern void *fibheap_replace_key_data (fibheap_t, fibnode_t,
  65. fibheapkey_t, void *);
  66. extern void *fibheap_extract_min (fibheap_t);
  67. extern void *fibheap_min (fibheap_t);
  68. extern void *fibheap_replace_data (fibheap_t, fibnode_t, void *);
  69. extern void *fibheap_delete_node (fibheap_t, fibnode_t);
  70. extern void fibheap_delete (fibheap_t);
  71. extern fibheap_t fibheap_union (fibheap_t, fibheap_t);
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75. #endif /* _FIBHEAP_H_ */