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