Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

FibonacciHeap.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <array>
35 #include <utility>
36 #include <functional>
37 
39 
40 namespace ogdf {
41 
42 
44 template<typename T>
46  template<typename, typename> friend class FibonacciHeap;
47 protected:
48  T value;
49 
50  size_t rank;
51  bool marked;
52 
57 
60  : rank(0), marked(false),
61  parent(nullptr), child(nullptr), prev(this), next(this)
62  {
63  }
64 
66  explicit FibonacciHeapNode(const T &nodeValue)
67  : value(nodeValue),
68  rank(0), marked(false),
69  parent(nullptr), child(nullptr), prev(this), next(this)
70  {
71  }
72 };
73 
74 
76 
86 template<typename T, typename C = std::less<T>>
87 class FibonacciHeap : public HeapBase<FibonacciHeap<T, C>, FibonacciHeapNode<T>, T, C>
88 {
89 
91 
92 public:
93 
100  explicit FibonacciHeap(const C &cmp = C(), int initialSize = -1);
101 
108  virtual ~FibonacciHeap();
109 
111  const T &top() const override;
112 
119  FibonacciHeapNode<T> *push(const T &value) override;
120 
126  void pop() override;
127 
137  void decrease(FibonacciHeapNode<T> *heapNode, const T &value) override;
138 
146  void merge(FibonacciHeap<T, C> &other) override;
147 
154  const T &value(FibonacciHeapNode<T> *heapNode) const override {
155  return heapNode->value;
156  }
157 
158 private:
160  FibonacciHeapNode<T> *m_minimal;
162  FibonacciHeapNode<T> *m_knot;
163 
165  std::array<FibonacciHeapNode<T> *, sizeof(size_t) * 8> m_ranked;
166 
168  void remove();
170  void compress();
171 
173  void link(FibonacciHeapNode<T> *root, FibonacciHeapNode<T> *child);
175  void detach(FibonacciHeapNode<T> *heapNode);
177  void merge(FibonacciHeapNode<T> *other);
179  void splice(FibonacciHeapNode<T> *target, FibonacciHeapNode<T> *heapNode);
181  void restore(FibonacciHeapNode<T> *heapNode);
182 
184  void release(FibonacciHeapNode<T> *heapNode);
185 };
186 
187 
188 template<typename T, typename C>
189 FibonacciHeap<T, C>::FibonacciHeap(const C &cmp, int /* unused parameter */)
190 : base_type(cmp), m_minimal(nullptr), m_knot(new FibonacciHeapNode<T>())
191 {
192  m_ranked.fill(nullptr);
193 }
194 
195 
196 template<typename T, typename C>
198 {
199  release(m_knot);
200 }
201 
202 
203 template<typename T, typename C>
204 void FibonacciHeap<T, C>::release(FibonacciHeapNode<T> *heapNode)
205 {
206  if(heapNode == nullptr) {
207  return;
208  }
209 
210  FibonacciHeapNode<T> *end = heapNode;
211  do {
212  release(heapNode->child);
213 
214  FibonacciHeapNode<T> *next = heapNode->next;
215  delete heapNode;
216  heapNode = next;
217  } while(heapNode != end);
218 }
219 
220 
221 template<typename T, typename C>
222 inline const T &FibonacciHeap<T, C>::top() const
223 {
224  return m_minimal->value;
225 }
226 
227 
228 template<typename T, typename C>
229 FibonacciHeapNode<T> *FibonacciHeap<T, C>::push(const T &value)
230 {
231  FibonacciHeapNode<T> *heapNode = new FibonacciHeapNode<T>(value);
232  splice(m_knot, heapNode);
233 
234  if(m_minimal == nullptr || this->comparator()(heapNode->value, m_minimal->value)) {
235  m_minimal = heapNode;
236  }
237 
238  return heapNode;
239 }
240 
241 
242 template<typename T, typename C>
244 {
245  // Special case for tree with only one node.
246  if(m_knot->next->next == m_knot &&
247  m_knot->next->child == nullptr)
248  {
249  m_knot->prev = m_knot->next = m_knot;
250  delete m_minimal;
251  m_minimal = nullptr;
252  return;
253  }
254 
255  remove();
256  compress();
257 
258  // Find new minimal node in compressed tree list.
259  m_minimal = m_knot->next;
260  for(auto it = m_knot->next->next; it != m_knot; it = it->next) {
261  if(this->comparator()(it->value, m_minimal->value)) {
262  m_minimal = it;
263  }
264  }
265 }
266 
267 
268 template<typename T, typename C>
269 void FibonacciHeap<T, C>::decrease(FibonacciHeapNode<T> *heapNode, const T &value)
270 {
271  heapNode->value = value;
272  if(this->comparator()(heapNode->value, m_minimal->value)) {
273  m_minimal = heapNode;
274  }
275 
276  restore(heapNode);
277 }
278 
279 
280 template<typename T, typename C>
282 {
283  if(other.m_minimal == nullptr) {
284  return;
285  }
286 
287  FibonacciHeapNode<T> *next = other.m_knot->next;
288  detach(other.m_knot);
289  merge(next);
290 
291  if(this->comparator()(other.m_minimal->value, m_minimal->value)) {
292  m_minimal = other.m_minimal;
293  }
294  other.m_minimal = nullptr;
295 }
296 
297 
298 template<typename T, typename C>
300 {
301  if(m_minimal->child) {
302  FibonacciHeapNode<T> *it = m_minimal->child;
303  do {
304  FibonacciHeapNode<T> *next = it->next;
305  it->parent = nullptr;
306  splice(m_knot, it);
307 
308  it = next;
309  } while(it != m_minimal->child);
310  }
311  detach(m_minimal);
312  delete m_minimal;
313 }
314 
315 
316 template<typename T, typename C>
318 {
319  size_t maxr = 0;
320 
321  for(auto it = m_knot->next; it != m_knot;) {
322  FibonacciHeapNode<T> *next = it->next;
323 
324  size_t r = it->rank;
325  maxr = std::max(r, maxr);
326  while(m_ranked[r]) {
327  if(this->comparator()(m_ranked[r]->value, it->value)) {
328  link(m_ranked[r], it);
329  it = m_ranked[r];
330  } else {
331  link(it, m_ranked[r]);
332  }
333  m_ranked[r] = nullptr;
334  r++;
335  maxr = std::max(maxr, r);
336  }
337  m_ranked[r] = it;
338 
339  it = next;
340  }
341 
342  for(size_t i = 0; i <= maxr; i++) {
343  m_ranked[i] = nullptr;
344  }
345 }
346 
347 
348 template<typename T, typename C>
350  FibonacciHeapNode<T> *root, FibonacciHeapNode<T> *child)
351 {
352  child->marked = false;
353  child->parent = root;
354  root->rank++;
355 
356  if(root->child) {
357  splice(root->child, child);
358  } else {
359  detach(child);
360  root->child = child;
361  }
362 }
363 
364 
365 template<typename T, typename C>
366 inline void FibonacciHeap<T, C>::detach(FibonacciHeapNode<T> *heapNode)
367 {
368  heapNode->prev->next = heapNode->next;
369  heapNode->next->prev = heapNode->prev;
370  heapNode->next = heapNode;
371  heapNode->prev = heapNode;
372 }
373 
374 
375 template<typename T, typename C>
376 inline void FibonacciHeap<T, C>::merge(FibonacciHeapNode<T> *other)
377 {
378  m_knot->next->prev = other->prev;
379  other->prev->next = m_knot->next;
380  m_knot->next = other;
381  other->prev = m_knot;
382 }
383 
384 
385 template<typename T, typename C>
387  FibonacciHeapNode<T> *target, FibonacciHeapNode<T> *heapNode)
388 {
389  detach(heapNode);
390  target->next->prev = heapNode;
391  heapNode->next = target->next;
392  target->next = heapNode;
393  heapNode->prev = target;
394 }
395 
396 
397 template<typename T, typename C>
398 inline void FibonacciHeap<T, C>::restore(FibonacciHeapNode<T> *heapNode)
399 {
400  for(;;) {
401  FibonacciHeapNode<T> *parent = heapNode->parent;
402  if(parent == nullptr) {
403  return;
404  }
405 
406  // We need to make sure parent has valid children after the splice.
407  parent->rank--;
408  if(parent->rank == 0) {
409  parent->child = nullptr;
410  } else if(parent->child == heapNode) {
411  parent->child = heapNode->next;
412  }
413 
414  heapNode->parent = nullptr;
415  splice(m_knot, heapNode);
416 
417  // If parent is unmarked we can stop cut cascade.
418  if(!parent->marked) {
419  parent->marked = true;
420  return;
421  }
422 
423  heapNode = parent;
424  }
425 }
426 
427 }
Fibonacci heap node.
Definition: FibonacciHeap.h:45
FibonacciHeapNode()
Creates empty root node.
Definition: FibonacciHeap.h:59
FibonacciHeapNode(const T &nodeValue)
Creates heap node with a given nodeValue.
Definition: FibonacciHeap.h:66
void remove()
Removes minimal tree and moves its children to tree list.
void link(FibonacciHeapNode< T > *root, FibonacciHeapNode< T > *child)
Makes child node a child of root node.
bool marked
Indicates whether node is marked or not.
Definition: FibonacciHeap.h:51
FibonacciHeapNode< T > * prev
Previous sibling of the node.
Definition: FibonacciHeap.h:55
Interface for heap implementations.
Fibonacci heap implementation.
Definition: FibonacciHeap.h:87
friend class FibonacciHeap
Definition: FibonacciHeap.h:46
FibonacciHeap(const C &cmp=C(), int initialSize=-1)
Creates empty Fibonacci heap.
T value
Value contained in the node.
Definition: FibonacciHeap.h:48
void restore(FibonacciHeapNode< T > *heapNode)
Restores heap ordering in heapNode by making (cascade) cut.
void decrease(FibonacciHeapNode< T > *heapNode, const T &value) override
Decreases value of the given heapNode to value.
const T & top() const override
Returns reference to the top element in the heap.
void release(FibonacciHeapNode< T > *heapNode)
Recursively releases memory starting at heapNode.
const T & value(FibonacciHeapNode< T > *heapNode) const override
Returns the value of the node.
virtual const C & comparator() const
Returns the comparator used to sort the values in the heap.
Definition: HeapBase.h:75
FibonacciHeapNode< T > * push(const T &value) override
Inserts a new node with given value into a heap.
FibonacciHeapNode< T > * m_minimal
Handle to the tree with lowest root priority.
FibonacciHeapNode< T > * m_knot
Used for efficient tree list manipulation.
FibonacciHeapNode< T > * next
Next sibling of the node.
Definition: FibonacciHeap.h:56
std::array< FibonacciHeapNode< T > *, sizeof(size_t) *8 > m_ranked
Used to compress trees.
FibonacciHeapNode< T > * parent
Parent of the node.
Definition: FibonacciHeap.h:53
Common interface for all heap classes.
Definition: HeapBase.h:55
virtual ~FibonacciHeap()
Destructs the heap.
void detach(FibonacciHeapNode< T > *heapNode)
Detaches given heapNode from its list and makes it self-circulate.
void merge(FibonacciHeap< T, C > &other) override
Merges in values of other heap.
The namespace for all OGDF objects.
size_t rank
Determines rank of a node.
Definition: FibonacciHeap.h:50
void splice(FibonacciHeapNode< T > *target, FibonacciHeapNode< T > *heapNode)
Moves heapNode from its list to the target list.
void pop() override
Removes the top element from the heap.
void compress()
Reduces number of trees inside a heap by linking ones with same degree.
FibonacciHeapNode< T > * child
First child of the node.
Definition: FibonacciHeap.h:54