ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
FibHeap.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19//***************************************************************************
20// The Fibonacci heap implementation contained in FIBHEAP.H and FIBHEAP.CPP
21// is Copyright (c) 1996 by John Boyer
22//
23// Once this Fibonacci heap implementation (the software) has been published
24// by Dr. Dobb's Journal, permission to use and distribute the software is
25// granted provided that this copyright notice remains in the source and
26// and the author (John Boyer) is acknowledged in works that use this program.
27//
28// Every effort has been made to ensure that this implementation is free of
29// errors. Nonetheless, the author (John Boyer) assumes no liability regarding
30// your use of this software.
31//
32// The author would also be very glad to hear from anyone who uses the
33// software or has any feedback about it.
34// Email: jboyer@gulf.csc.uvic.ca
35//***************************************************************************
36
37// exclude from VTK wrapping
38#ifndef __VTK_WRAP__
39
40# ifndef FIBHEAP_H
41# define FIBHEAP_H
42
43# include <cstdio>
44# include <iostream>
45
46# include "GrowCutExport.h"
47
48// type for cost function - single precision is enough
49typedef float NodeKeyValueType;
50
51// type for storing a pixel index
52// We could use 32-bit indices for images smaller than 4GB and 64-bit indices for larger images.
53// However, >4GB images would require about 300GB RAM to store all the indices and distances,
54// so for now we only support images smaller than 4GB.
55typedef unsigned int NodeIndexType;
56
57// .NAME FibHeapNode - Fibonacci Heap Node Class
58// .SECTION Description
59//
60// This class stores all data of a node in the Fibonacci heap and implements
61// basic operations on nodes (comparison, assignment, etc.).
62//
63// In the original implementation of John Boyer FibHeapNode was a base class
64// and the implementation was completed in a derived class
65// However, virtual methods reduced performance by a few percent and the
66// virtual method table added extra 8 bytes for the size of each node,
67// which was significant when one node represented a voxel (and we had
68// hundreds of millions of voxels in an image).
69
70class GrowCut_EXPORT FibHeapNode
71{
72public:
75
83
84 ~FibHeapNode() = default;
85
87 inline NodeIndexType
89 {
90 return m_Index;
91 }
92 inline void
94 {
95 m_Index = indexValue;
96 }
97
100 inline NodeKeyValueType
102 {
103 return m_Key;
104 }
105
107 inline void
109 {
110 m_Key = newKeyVal;
111 }
112
114 inline void
116 {
117 m_Key = RHS.m_Key;
118 }
119
121 inline bool
123 {
124 return m_Key == RHS.m_Key;
125 }
126
128 inline bool
130 {
131 return m_Key < RHS.m_Key;
132 }
133
134 void
136 {
137 std::cout << m_Key;
138 }
139
144 short m_Degree{ 0 };
145 bool m_Mark{ false };
146
149};
150
151// .NAME FibHeap - Fibonacci Heap Class
152// .SECTION Description
153//
154// This class implements a performance-optimized heap data structure.
155// Using amortized analysis, the asymptotically fastest heap
156// data structure is the Fibonacci heap. The constants are a little
157// high so the real speed gain will not be seen until larger data sets
158// are required, but in most cases, if the data set is small, then the
159// run-time will be negligible anyway.
160//
161// FibHeap class can then be used to Insert(), ExtractMin(), etc.,
162// instances of FibHeapNode class.
163
165
166class GrowCut_EXPORT FibHeap
167{
168public:
170 virtual ~FibHeap();
171
178 void
180
190 void
191 Insert(FibHeapNode * NewNode);
192
194 void
195 Union(FibHeap * OtherHeap);
196
198 inline FibHeapNode *
200 {
201 return m_MinRoot;
202 }
203
207
212 int
214
220 int
221 Delete(FibHeapNode * theNode);
222
223 inline bool
225 {
226 return (m_MinRoot == nullptr);
227 }
228
229 // Extra utility functions
230 long
232 {
233 return m_NumNodes;
234 };
235 long
237 {
238 return m_NumTrees;
239 };
240 long
242 {
243 return m_NumMarkedNodes;
244 };
245
251 void
252 Print(FibHeapNode * Tree = nullptr, FibHeapNode * theParent = nullptr);
253
254private:
255 // Internal functions that help to implement the Standard Operations
256 inline void
258 {
259 FibHeapNode * Temp;
260 Temp = N1;
261 N1 = N2;
262 N2 = Temp;
263 }
264
265 // Internal function that reorganizes heap as part of an ExtractMin().
266 // We must find new minimum in heap, which could be anywhere along the
267 // root list. The search could be O(n) since all nodes could be on
268 // the root list. So, we reorganize the tree into more of a binomial forest
269 // structure, and then find the new minimum on the consolidated O(lg n) sized
270 // root list, and in the process set each m_Parent pointer to nullptr, and count
271 // the number of resulting subtrees.
272 //
273 // Note that after a list of n inserts, there will be n nodes on the root
274 // list, so the first ExtractMin() will be O(n) regardless of whether or
275 // not we consolidate. However, the consolidation causes subsequent
276 // ExtractMin() operations to be O(lg n). Furthermore, the extra cost of
277 // the first ExtractMin() is covered by the higher amortized cost of
278 // Insert(), which is the real governing factor in how costly the first
279 // ExtractMin() will be.
280 void
282
283 // The node y is removed from the root list and becomes a subtree of node x.
284 void
286 void
288
289 // Remove node x from the child list of its parent node y
290 void
292
293 // Cuts each node in parent list, putting successive ancestor nodes on the
294 // root list until we either arrive at the root list or until we find an
295 // ancestor that is unmarked. When a mark is set (which only happens during
296 // a cascading cut), it means that one child subtree has been lost; if a
297 // second subtree is lost later during another cascading cut, then we move
298 // the node to the root list so that it can be re-balanced on the next
299 // consolidate.
300 void
302
303 inline FibHeapNode *
305 {
306 return index == FibHeapNode::NullNodeIndex ? nullptr : m_HeapNodes + index;
307 }
308
313 FibHeapNode * m_HeapNodes; // array containing all the heap nodes
314};
315
316# endif /* FIBHEAP_H */
317
318#endif //__VTK_WRAP__
unsigned int NodeIndexType
Definition FibHeap.h:55
float NodeKeyValueType
Definition FibHeap.h:49
void Print()
Definition FibHeap.h:135
NodeIndexType m_Right
Definition FibHeap.h:141
NodeIndexType m_Left
Definition FibHeap.h:140
bool operator<(FibHeapNode &RHS)
Compare key value.
Definition FibHeap.h:129
NodeIndexType m_Child
Definition FibHeap.h:143
NodeIndexType GetIndexValue()
Index stores this node's location in the node array.
Definition FibHeap.h:88
NodeKeyValueType m_Key
Definition FibHeap.h:147
NodeIndexType m_Index
Definition FibHeap.h:148
bool operator==(FibHeapNode &RHS)
Compare key value.
Definition FibHeap.h:122
bool m_Mark
Definition FibHeap.h:145
static const NodeIndexType NullNodeIndex
Definition FibHeap.h:73
void operator=(NodeKeyValueType newKeyVal)
Set key value (that the nodes are sorted based on)
Definition FibHeap.h:108
FibHeapNode()
Definition FibHeap.h:76
~FibHeapNode()=default
NodeIndexType m_Parent
Definition FibHeap.h:142
static const NodeKeyValueType NegativeInfinity
Definition FibHeap.h:74
short m_Degree
Definition FibHeap.h:144
void SetIndexValue(NodeIndexType indexValue)
Definition FibHeap.h:93
void operator=(FibHeapNode &RHS)
Set key value from another node.
Definition FibHeap.h:115
NodeKeyValueType GetKeyValue()
Definition FibHeap.h:101
FibHeapNode * m_MinRoot
Definition FibHeap.h:309
virtual ~FibHeap()
FibHeapNode * ExtractMin()
ExtractMin() - O(n) worst-case actual; O(lg n) amortized.
void Consolidate()
void Exchange(FibHeapNode *&N1, FibHeapNode *&N2)
Definition FibHeap.h:257
long m_NumMarkedNodes
Definition FibHeap.h:312
long GetNumNodes()
Definition FibHeap.h:231
FibHeapNode * Minimum()
Minimum - O(1) actual; O(1) amortized.
Definition FibHeap.h:199
int DecreaseKey(FibHeapNode *theNode, NodeKeyValueType keyValue)
FibHeapNode * HeapNodeFromIndex(NodeIndexType index)
Definition FibHeap.h:304
void AddToRootList(FibHeapNode *)
void SetHeapNodes(FibHeapNode *heapNodes)
bool IsEmpty()
Definition FibHeap.h:224
void Union(FibHeap *OtherHeap)
Union() - O(1) actual; O(1) amortized.
long GetNumTrees()
Definition FibHeap.h:236
long GetNumMarkedNodes()
Definition FibHeap.h:241
void Insert(FibHeapNode *NewNode)
long m_NumTrees
Definition FibHeap.h:311
void Link(FibHeapNode *, FibHeapNode *)
void Cut(FibHeapNode *, FibHeapNode *)
int Delete(FibHeapNode *theNode)
FibHeapNode * m_HeapNodes
Definition FibHeap.h:313
void Print(FibHeapNode *Tree=nullptr, FibHeapNode *theParent=nullptr)
long m_NumNodes
Definition FibHeap.h:310
void CascadingCut(FibHeapNode *)