fibheap.h 2.87 KB
Newer Older
Daniel Berlin committed
1
/* A Fibonacci heap datatype.
2
   Copyright (C) 1998-2015 Free Software Foundation, Inc.
Daniel Berlin committed
3 4
   Contributed by Daniel Berlin (dan@cgsoftware.com).

5
This file is part of GCC.
Daniel Berlin committed
6
   
7
GCC is free software; you can redistribute it and/or modify it
Daniel Berlin committed
8 9 10 11
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

12
GCC is distributed in the hope that it will be useful, but
Daniel Berlin committed
13 14 15 16 17
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
18
along with GCC; see the file COPYING.  If not, write to
19 20
the Free Software Foundation, 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA.  */
Daniel Berlin committed
21

22 23
/* Fibonacci heaps are somewhat complex, but, there's an article in
   DDJ that explains them pretty well:
Daniel Berlin committed
24 25 26

   http://www.ddj.com/articles/1997/9701/9701o/9701o.htm?topic=algoritms

27
   Introduction to algorithms by Corman and Rivest also goes over them.
Daniel Berlin committed
28 29 30 31 32 33 34 35 36 37

   The original paper that introduced them is "Fibonacci heaps and their
   uses in improved network optimization algorithms" by Tarjan and
   Fredman (JACM 34(3), July 1987).

   Amortized and real worst case time for operations:

   ExtractMin: O(lg n) amortized. O(n) worst case.
   DecreaseKey: O(1) amortized.  O(lg n) worst case. 
   Insert: O(2) amortized. O(1) actual.  
38
   Union: O(1) amortized. O(1) actual.  */
Daniel Berlin committed
39 40 41 42

#ifndef _FIBHEAP_H_
#define _FIBHEAP_H_

43
#include "ansidecl.h"
Daniel Berlin committed
44

45 46 47 48
#ifdef __cplusplus
extern "C" {
#endif

49 50
typedef long fibheapkey_t;

Daniel Berlin committed
51 52 53 54 55 56
typedef struct fibheap
{
  size_t nodes;
  struct fibnode *min;
  struct fibnode *root;
} *fibheap_t;
57

Daniel Berlin committed
58 59 60 61 62 63 64 65
typedef struct fibnode
{
  struct fibnode *parent;
  struct fibnode *child;
  struct fibnode *left;
  struct fibnode *right;
  fibheapkey_t key;
  void *data;
66
#if defined (__GNUC__) && (!defined (SIZEOF_INT) || SIZEOF_INT < 4)
67 68
  __extension__ unsigned long int degree : 31;
  __extension__ unsigned long int mark : 1;
69
#else
70 71
  unsigned int degree : 31;
  unsigned int mark : 1;
72
#endif
Daniel Berlin committed
73 74
} *fibnode_t;

75 76 77 78 79 80 81 82 83 84 85 86 87 88
extern fibheap_t fibheap_new (void);
extern fibnode_t fibheap_insert (fibheap_t, fibheapkey_t, void *);
extern int fibheap_empty (fibheap_t);
extern fibheapkey_t fibheap_min_key (fibheap_t);
extern fibheapkey_t fibheap_replace_key (fibheap_t, fibnode_t,
                                         fibheapkey_t);
extern void *fibheap_replace_key_data (fibheap_t, fibnode_t,
                                       fibheapkey_t, void *);
extern void *fibheap_extract_min (fibheap_t);
extern void *fibheap_min (fibheap_t);
extern void *fibheap_replace_data (fibheap_t, fibnode_t, void *);
extern void *fibheap_delete_node (fibheap_t, fibnode_t);
extern void fibheap_delete (fibheap_t);
extern fibheap_t fibheap_union (fibheap_t, fibheap_t);
89

90 91 92 93
#ifdef __cplusplus
}
#endif

Daniel Berlin committed
94
#endif /* _FIBHEAP_H_ */