Slicer 5.9
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
Loading...
Searching...
No Matches
FibHeap.h
Go to the documentation of this file.
1//***************************************************************************
2// The Fibonacci heap implementation contained in FIBHEAP.H and FIBHEAP.CPP
3// is Copyright (c) 1996 by John Boyer
4//
5// Once this Fibonacci heap implementation (the software) has been published
6// by Dr. Dobb's Journal, permission to use and distribute the software is
7// granted provided that this copyright notice remains in the source and
8// and the author (John Boyer) is acknowledged in works that use this program.
9//
10// Every effort has been made to ensure that this implementation is free of
11// errors. Nonetheless, the author (John Boyer) assumes no liability regarding
12// your use of this software.
13//
14// The author would also be very glad to hear from anyone who uses the
15// software or has any feedback about it.
16// Email: jboyer@gulf.csc.uvic.ca
17//***************************************************************************
18
19// exclude from VTK wrapping
20#ifndef __VTK_WRAP__
21
22# ifndef FIBHEAP_H
23# define FIBHEAP_H
24
25# include <cstdio>
26# include <iostream>
27
28// type for cost function - single precision is enough
29typedef float NodeKeyValueType;
30
31// type for storing a pixel index
32// We could use 32-bit indices for images smaller than 4GB and 64-bit indices for larger images.
33// However, >4GB images would require about 300GB RAM to store all the indices and distances,
34// so for now we only support images smaller than 4GB.
35typedef unsigned int NodeIndexType;
36
37// .NAME FibHeapNode - Fibonacci Heap Node Class
38// .SECTION Description
39//
40// This class stores all data of a node in the Fibonacci heap and implements
41// basic operations on nodes (comparison, assignment, etc.).
42//
43// In the original implementation of John Boyer FibHeapNode was a base class
44// and the implementation was completed in a derived class
45// However, virtual methods reduced performance by a few percent and the
46// virtual method table added extra 8 bytes for the size of each node,
47// which was significant when one node represented a voxel (and we had
48// hundreds of millions of voxels in an image).
49
51{
52public:
55
64
65 ~FibHeapNode() = default;
66
68 inline NodeIndexType GetIndexValue() { return m_Index; }
69 inline void SetIndexValue(NodeIndexType indexValue) { m_Index = indexValue; }
70
73 inline NodeKeyValueType GetKeyValue() { return m_Key; }
74
76 inline void operator=(NodeKeyValueType newKeyVal) { m_Key = newKeyVal; }
77
79 inline void operator=(FibHeapNode& RHS) { m_Key = RHS.m_Key; }
80
82 inline bool operator==(FibHeapNode& RHS) { return m_Key == RHS.m_Key; }
83
85 inline bool operator<(FibHeapNode& RHS) { return m_Key < RHS.m_Key; }
86
87 void Print() { std::cout << m_Key; }
88
93 short m_Degree{ 0 };
94 bool m_Mark{ false };
95
98};
99
100// .NAME FibHeap - Fibonacci Heap Class
101// .SECTION Description
102//
103// This class implements a performance-optimized heap data structure.
104// Using amortized analysis, the asymptotically fastest heap
105// data structure is the Fibonacci heap. The constants are a little
106// high so the real speed gain will not be seen until larger data sets
107// are required, but in most cases, if the data set is small, then the
108// run-time will be negligible anyway.
109//
110// FibHeap class can then be used to Insert(), ExtractMin(), etc.,
111// instances of FibHeapNode class.
112
114{
115public:
117 virtual ~FibHeap();
118
125 void SetHeapNodes(FibHeapNode* heapNodes);
126
136 void Insert(FibHeapNode* NewNode);
137
139 void Union(FibHeap* OtherHeap);
140
142 inline FibHeapNode* Minimum() { return m_MinRoot; }
143
146
151 int DecreaseKey(FibHeapNode* theNode, NodeKeyValueType keyValue);
152
158 int Delete(FibHeapNode* theNode);
159
160 inline bool IsEmpty() { return (m_MinRoot == nullptr); }
161
162 // Extra utility functions
163 long GetNumNodes() { return m_NumNodes; };
164 long GetNumTrees() { return m_NumTrees; };
165 long GetNumMarkedNodes() { return m_NumMarkedNodes; };
166
172 void Print(FibHeapNode* Tree = nullptr, FibHeapNode* theParent = nullptr);
173
174private:
175 // Internal functions that help to implement the Standard Operations
176 inline void Exchange(FibHeapNode*& N1, FibHeapNode*& N2)
177 {
178 FibHeapNode* Temp;
179 Temp = N1;
180 N1 = N2;
181 N2 = Temp;
182 }
183
184 // Internal function that reorganizes heap as part of an ExtractMin().
185 // We must find new minimum in heap, which could be anywhere along the
186 // root list. The search could be O(n) since all nodes could be on
187 // the root list. So, we reorganize the tree into more of a binomial forest
188 // structure, and then find the new minimum on the consolidated O(lg n) sized
189 // root list, and in the process set each m_Parent pointer to nullptr, and count
190 // the number of resulting subtrees.
191 //
192 // Note that after a list of n inserts, there will be n nodes on the root
193 // list, so the first ExtractMin() will be O(n) regardless of whether or
194 // not we consolidate. However, the consolidation causes subsequent
195 // ExtractMin() operations to be O(lg n). Furthermore, the extra cost of
196 // the first ExtractMin() is covered by the higher amortized cost of
197 // Insert(), which is the real governing factor in how costly the first
198 // ExtractMin() will be.
199 void Consolidate();
200
201 // The node y is removed from the root list and becomes a subtree of node x.
202 void Link(FibHeapNode*, FibHeapNode*);
203 void AddToRootList(FibHeapNode*);
204
205 // Remove node x from the child list of its parent node y
206 void Cut(FibHeapNode*, FibHeapNode*);
207
208 // Cuts each node in parent list, putting successive ancestor nodes on the
209 // root list until we either arrive at the root list or until we find an
210 // ancestor that is unmarked. When a mark is set (which only happens during
211 // a cascading cut), it means that one child subtree has been lost; if a
212 // second subtree is lost later during another cascading cut, then we move
213 // the node to the root list so that it can be re-balanced on the next
214 // consolidate.
215 void CascadingCut(FibHeapNode*);
216
217 inline FibHeapNode* HeapNodeFromIndex(NodeIndexType index) { return index == FibHeapNode::NullNodeIndex ? nullptr : m_HeapNodes + index; }
218
219 FibHeapNode* m_MinRoot;
220 long m_NumNodes;
221 long m_NumTrees;
222 long m_NumMarkedNodes;
223 FibHeapNode* m_HeapNodes; // array containing all the heap nodes
224};
225
226# endif
227
228#endif //__VTK_WRAP__
unsigned int NodeIndexType
Definition FibHeap.h:35
float NodeKeyValueType
Definition FibHeap.h:29
void Print()
Definition FibHeap.h:87
NodeIndexType m_Right
Definition FibHeap.h:90
NodeIndexType m_Left
Definition FibHeap.h:89
bool operator<(FibHeapNode &RHS)
Compare key value.
Definition FibHeap.h:85
NodeIndexType m_Child
Definition FibHeap.h:92
NodeIndexType GetIndexValue()
Index stores this node's location in the node array.
Definition FibHeap.h:68
NodeKeyValueType m_Key
Definition FibHeap.h:96
NodeIndexType m_Index
Definition FibHeap.h:97
bool operator==(FibHeapNode &RHS)
Compare key value.
Definition FibHeap.h:82
bool m_Mark
Definition FibHeap.h:94
static const NodeIndexType NullNodeIndex
Definition FibHeap.h:53
void operator=(NodeKeyValueType newKeyVal)
Set key value (that the nodes are sorted based on)
Definition FibHeap.h:76
FibHeapNode()
Definition FibHeap.h:56
~FibHeapNode()=default
NodeIndexType m_Parent
Definition FibHeap.h:91
static const NodeKeyValueType NegativeInfinity
Definition FibHeap.h:54
short m_Degree
Definition FibHeap.h:93
void SetIndexValue(NodeIndexType indexValue)
Definition FibHeap.h:69
void operator=(FibHeapNode &RHS)
Set key value from another node.
Definition FibHeap.h:79
NodeKeyValueType GetKeyValue()
Definition FibHeap.h:73
virtual ~FibHeap()
FibHeapNode * ExtractMin()
ExtractMin() - O(n) worst-case actual; O(lg n) amortized.
long GetNumNodes()
Definition FibHeap.h:163
FibHeapNode * Minimum()
Minimum - O(1) actual; O(1) amortized.
Definition FibHeap.h:142
int DecreaseKey(FibHeapNode *theNode, NodeKeyValueType keyValue)
void SetHeapNodes(FibHeapNode *heapNodes)
bool IsEmpty()
Definition FibHeap.h:160
void Union(FibHeap *OtherHeap)
Union() - O(1) actual; O(1) amortized.
long GetNumTrees()
Definition FibHeap.h:164
long GetNumMarkedNodes()
Definition FibHeap.h:165
void Insert(FibHeapNode *NewNode)
int Delete(FibHeapNode *theNode)
void Print(FibHeapNode *Tree=nullptr, FibHeapNode *theParent=nullptr)