fibheap.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* A Fibonacci heap datatype.
  2. Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2009
  3. Free Software Foundation, Inc.
  4. Contributed by Daniel Berlin (dan@cgsoftware.com).
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10. GCC is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING. If not, write to
  16. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  17. Boston, MA 02110-1301, USA. */
  18. /* Fibonacci heaps are somewhat complex, but, there's an article in
  19. DDJ that explains them pretty well:
  20. http://www.ddj.com/articles/1997/9701/9701o/9701o.htm?topic=algoritms
  21. Introduction to algorithms by Corman and Rivest also goes over them.
  22. The original paper that introduced them is "Fibonacci heaps and their
  23. uses in improved network optimization algorithms" by Tarjan and
  24. Fredman (JACM 34(3), July 1987).
  25. Amortized and real worst case time for operations:
  26. ExtractMin: O(lg n) amortized. O(n) worst case.
  27. DecreaseKey: O(1) amortized. O(lg n) worst case.
  28. Insert: O(2) amortized. O(1) actual.
  29. Union: O(1) amortized. O(1) actual. */
  30. #ifndef _FIBHEAP_H_
  31. #define _FIBHEAP_H_
  32. #include "ansidecl.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. typedef long fibheapkey_t;
  37. typedef struct fibheap
  38. {
  39. size_t nodes;
  40. struct fibnode *min;
  41. struct fibnode *root;
  42. } *fibheap_t;
  43. typedef struct fibnode
  44. {
  45. struct fibnode *parent;
  46. struct fibnode *child;
  47. struct fibnode *left;
  48. struct fibnode *right;
  49. fibheapkey_t key;
  50. void *data;
  51. #if defined (__GNUC__) && (!defined (SIZEOF_INT) || SIZEOF_INT < 4)
  52. __extension__ unsigned long int degree : 31;
  53. __extension__ unsigned long int mark : 1;
  54. #else
  55. unsigned int degree : 31;
  56. unsigned int mark : 1;
  57. #endif
  58. } *fibnode_t;
  59. extern fibheap_t fibheap_new (void);
  60. extern fibnode_t fibheap_insert (fibheap_t, fibheapkey_t, void *);
  61. extern int fibheap_empty (fibheap_t);
  62. extern fibheapkey_t fibheap_min_key (fibheap_t);
  63. extern fibheapkey_t fibheap_replace_key (fibheap_t, fibnode_t,
  64. fibheapkey_t);
  65. extern void *fibheap_replace_key_data (fibheap_t, fibnode_t,
  66. fibheapkey_t, void *);
  67. extern void *fibheap_extract_min (fibheap_t);
  68. extern void *fibheap_min (fibheap_t);
  69. extern void *fibheap_replace_data (fibheap_t, fibnode_t, void *);
  70. extern void *fibheap_delete_node (fibheap_t, fibnode_t);
  71. extern void fibheap_delete (fibheap_t);
  72. extern fibheap_t fibheap_union (fibheap_t, fibheap_t);
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. #endif /* _FIBHEAP_H_ */