The Original CHomP Software
Classes | Public Types | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
chomp::homology::FibonacciHeap< element > Class Template Reference

This template contains the definition of a Fibonacci heap that can be used as an efficient priority queue, for example, in the Dijxtra graph algorithm. More...

#include <fibheap.h>

Classes

struct  Node
 The structure that holds a graph node for the graph representation of a Fibonacci heap. More...
 

Public Types

typedef int_t NodePtr
 The type of a pointer to a node in the Fibonacci heap. More...
 

Public Member Functions

 FibonacciHeap (int_t maxSize=0)
 The constructor of an empty Fibonacci heap. More...
 
 ~FibonacciHeap ()
 The destructor of a Fibonacci heap. More...
 
int_t Insert (const element &value)
 Inserts an element into the heap. More...
 
int_t Minimum () const
 Returns the number of the node that contains the minimum. More...
 
const element & Value (int_t number) const
 Returns the value of the node with the given number. More...
 
element ExtractMinimum ()
 Extracts the minimum from the heap. More...
 
void DecreaseKey (int_t number, const element &value)
 Decreases the key of the element identified by the given number to the new value. More...
 
void Delete (int_t number)
 Removes the element identified by the given number from the heap. More...
 

Static Public Attributes

static const int_t NullPtr
 The value used for a NULL pointer. More...
 

Private Member Functions

 FibonacciHeap (const FibonacciHeap< element > &)
 The copy constructor is not allowed. More...
 
FibonacciHeap< element > & operator= (const FibonacciHeap< element > &)
 The assignment operator is not allowed. More...
 
void Consolidate ()
 Consolidates the Fibonacci heap by joining roots of equal degree. More...
 
void Cut (const typename FibonacciHeap< element >::NodePtr &x, const typename FibonacciHeap< element >::NodePtr &y)
 Cuts the tree. More...
 
void CascadingCut (typename FibonacciHeap< element >::NodePtr y)
 Does a cascading cut of the tree. More...
 
void showGraph (std::ostream &out, int_t depth, const typename FibonacciHeap< element >::NodePtr &x) const
 Shows a graph starting at the given node using DFS. More...
 

Private Attributes

NodePtr minRoot
 A pointer to the minimal element in the list of roots. More...
 
int_t nodeCount
 The total number of nodes inserted to the heap and allocated in the multitable "nodes". More...
 
multitable< Nodenodes
 The array of nodes which hold the elements inserted to the heap in this order. More...
 
element minValue
 The minimal value that ever appeared in the heap. More...
 
multitable< NodePtrauxArray
 An auto-expandable array used by the "Consolidate" procedure. More...
 
int_t arrayPrepared
 The size of the prepared data in the array. More...
 

Friends

std::ostream & operator<< (std::ostream &out, const FibonacciHeap< element > &h)
 Operator << shows the heap to the output stream. More...
 

Detailed Description

template<class element>
class chomp::homology::FibonacciHeap< element >

This template contains the definition of a Fibonacci heap that can be used as an efficient priority queue, for example, in the Dijxtra graph algorithm.

Note that if the "Delete" function is used then the elements must support the arithmetic subtraction of 1 to create a value that is strictly smaller than the given one (like the integers). This is a very specialized implementation which does not support the "Union" operation. Moreover, the "Delete" or "ExtractMinimum" don't actually free the memory used by the deleted elements in this implementation. See the description of the functions in this class for more information.

Definition at line 62 of file fibheap.h.

Member Typedef Documentation

◆ NodePtr

template<class element >
typedef int_t chomp::homology::FibonacciHeap< element >::NodePtr

The type of a pointer to a node in the Fibonacci heap.

Definition at line 66 of file fibheap.h.

Constructor & Destructor Documentation

◆ FibonacciHeap() [1/2]

template<class element >
chomp::homology::FibonacciHeap< element >::FibonacciHeap ( int_t  maxSize = 0)
inline

The constructor of an empty Fibonacci heap.

The maximal number of elements may be given if it is known.

Definition at line 260 of file fibheap.h.

260 :
261 minRoot (NullPtr), nodeCount (0), nodes (maxSize), arrayPrepared (0)
262{
263 // allocate a new pool for elements if this is the first heap
264// if (!heapsInUse)
265// p = new pool<typename FibonacciHeap<element>::Node>;
266
267 // increase the number of heaps in use
268// ++ heapsInUse;
269
270 return;
271} /* FibonacciHeap::FibonacciHeap */
int_t arrayPrepared
The size of the prepared data in the array.
Definition: fibheap.h:185
multitable< Node > nodes
The array of nodes which hold the elements inserted to the heap in this order.
Definition: fibheap.h:164
NodePtr minRoot
A pointer to the minimal element in the list of roots.
Definition: fibheap.h:153
int_t nodeCount
The total number of nodes inserted to the heap and allocated in the multitable "nodes".
Definition: fibheap.h:157
static const int_t NullPtr
The value used for a NULL pointer.
Definition: fibheap.h:69

◆ ~FibonacciHeap()

template<class element >
chomp::homology::FibonacciHeap< element >::~FibonacciHeap
inline

The destructor of a Fibonacci heap.

Definition at line 274 of file fibheap.h.

275{
276 // decrease the number of heaps in use
277// -- heapsInUse;
278
279 // delete the pool for elements if this was the last heap
280// if (!heapsInUse)
281// delete p;
282
283 return;
284} /* FibonacciHeap::~FibonacciHeap */

◆ FibonacciHeap() [2/2]

template<class element >
chomp::homology::FibonacciHeap< element >::FibonacciHeap ( const FibonacciHeap< element > &  )
inlineprivate

The copy constructor is not allowed.

Definition at line 287 of file fibheap.h.

288{
289 throw "Copy constructor for a Fibonacci heap not implemented.";
290 return;
291} /* FibonacciHeap::FibonacciHeap */

Member Function Documentation

◆ CascadingCut()

template<class element >
void chomp::homology::FibonacciHeap< element >::CascadingCut ( typename FibonacciHeap< element >::NodePtr  y)
inlineprivate

Does a cascading cut of the tree.

Definition at line 609 of file fibheap.h.

611{
612 // do the cascading cut while the node has a parent
613 while (!(nodes [y]. parent == NullPtr))
614 {
615 // if this is the first time the node lost its child,
616 // then just mark the node and exit the cascading cut
617 if (!nodes [y]. mark)
618 {
619 nodes [y]. mark = true;
620 return;
621 }
622
623 // cut the node and continue the cascading cut
624 // with its parent
625 NodePtr z = nodes [y]. parent;
626 Cut (y, z);
627 y = z;
628 }
629 return;
630} /* FibonacciHeap::CascadingCut */
int_t NodePtr
The type of a pointer to a node in the Fibonacci heap.
Definition: fibheap.h:66
void Cut(const typename FibonacciHeap< element >::NodePtr &x, const typename FibonacciHeap< element >::NodePtr &y)
Cuts the tree.
Definition: fibheap.h:571

◆ Consolidate()

template<class element >
void chomp::homology::FibonacciHeap< element >::Consolidate
inlineprivate

Consolidates the Fibonacci heap by joining roots of equal degree.

Definition at line 394 of file fibheap.h.

395{
396 // do nothing if the heap is empty
397 if (minRoot == NullPtr)
398 return;
399
400 // for each node in the root list of the heap...
401 NodePtr node = minRoot;
402 int_t maxDegree = 0;
403 do
404 {
405 // take the node for the loop and get its degree
406 NodePtr x = node;
407 int_t d = nodes [x]. degree;
408
409 // prepare the next node from the root list for the next time
410 node = nodes [node]. right;
411
412 // expand the auxiliary array if necessary
413 while (arrayPrepared <= d)
415
416 // update the strict upper limit on the degree used
417 if (maxDegree <= d)
418 maxDegree = d + 1;
419
420 // join this tree with another one of the same degree if any
421 while (auxArray [d] != NullPtr)
422 {
423 // take the node of the same degree as x
424 NodePtr y = auxArray [d];
425
426 // swap the node pointers if necessary
427 if (nodes [y]. key < nodes [x]. key)
428 {
429 NodePtr tmp = x;
430 x = y;
431 y = tmp;
432 }
433
434 // clear the mark of the node y
435 nodes [y]. mark = false;
436
437 // make the node y a child of the node x
438 nodes [y]. parent = x;
439 NodePtr child = nodes [x]. child;
440 if (child == NullPtr)
441 {
442 nodes [x]. child = y;
443 nodes [y]. left = y;
444 nodes [y]. right = y;
445 }
446 else
447 {
448 nodes [y]. left = child;
449 nodes [y]. right = nodes [child]. right;
450 nodes [child]. right = y;
451 nodes [nodes [y]. right]. left = y;
452 }
453 ++ nodes [x]. degree;
454
455 // clear the entry which stored the node of degree d
456 auxArray [d] = NullPtr;
457
458 // increase the degree to the degree of x
459 ++ d;
460
461 // expand the auxiliary array if necessary
462 if (arrayPrepared <= d)
464
465 // update the strict upper limit on the degree used
466 if (maxDegree <= d)
467 maxDegree = d + 1;
468 }
469
470 // put this node in the array
471 auxArray [d] = x;
472
473 } while (node != minRoot);
474
475 // reconstruct the root list from the auxiliary array
477 for (int_t d = 0; d < maxDegree; ++ d)
478 {
479 // skip entries of the array which don't point to nodes
480 if (auxArray [d] == NullPtr)
481 continue;
482
483 // take the node pointer of the array
484 NodePtr nodePtr = auxArray [d];
485 auxArray [d] = NullPtr;
486 Node &node = nodes [nodePtr];
487
488 // link this node to the root list
489 if (minRoot == NullPtr)
490 {
491 node. left = nodePtr;
492 node. right = nodePtr;
493 }
494 else
495 {
496 node. left = minRoot;
497 node. right = nodes [minRoot]. right;
498 nodes [node. left]. right = nodePtr;
499 nodes [node. right]. left = nodePtr;
500 }
501
502 // update the pointer to the minimal root node if necessary
503 if ((minRoot == NullPtr) ||
504 (node. key < nodes [minRoot]. key))
505 {
506 minRoot = nodePtr;
507 }
508 }
509 return;
510} /* FibonacciHeap::Consolidate */
multitable< NodePtr > auxArray
An auto-expandable array used by the "Consolidate" procedure.
Definition: fibheap.h:182
int int_t
Index type for indexing arrays, counting cubes, etc.
Definition: config.h:115

◆ Cut()

template<class element >
void chomp::homology::FibonacciHeap< element >::Cut ( const typename FibonacciHeap< element >::NodePtr x,
const typename FibonacciHeap< element >::NodePtr y 
)
inlineprivate

Cuts the tree.

Definition at line 570 of file fibheap.h.

573{
574 // remove x from the children of y:
575 // case 1: if x is the only child of y
576 nodes [x]. parent = NullPtr;
577 if (nodes [x]. right == x)
578 {
579 nodes [y]. child = NullPtr;
580 }
581 //case 2: if there are also other children of y
582 else
583 {
584 NodePtr leftChild = nodes [x]. left;
585 NodePtr rightChild = nodes [x]. right;
586 nodes [y]. child = rightChild;
587 nodes [leftChild]. right = rightChild;
588 nodes [rightChild]. left = leftChild;
589 }
590
591 // update the degree of y
592 -- nodes [y]. degree;
593
594 // add x to the root list of the heap
595 NodePtr leftRoot = minRoot;
596 NodePtr rightRoot = nodes [minRoot]. right;
597 nodes [x]. left = leftRoot;
598 nodes [x]. right = rightRoot;
599 nodes [leftRoot]. right = x;
600 nodes [rightRoot]. left = x;
601
602 // clear the marking of the node x if any
603 nodes [x]. mark = false;
604
605 return;
606} /* FibonacciHeap::Cut */

◆ DecreaseKey()

template<class element >
void chomp::homology::FibonacciHeap< element >::DecreaseKey ( int_t  number,
const element &  value 
)
inline

Decreases the key of the element identified by the given number to the new value.

Definition at line 633 of file fibheap.h.

635{
636 // translate the number to a node pointer
637 NodePtr x (number);
638
639 // ignore this action if the node is no longer in the heap
640 if (nodes [x]. degree == -1)
641 return;
642
643 // update the minimal value in the heap
644 if (value < minValue)
645 minValue = value;
646
647 // update the value of the node
648 nodes [x]. key = value;
649
650 // cut the tree so that the node x is in the root list
651 NodePtr y = nodes [x]. parent;
652 if ((y != NullPtr) && (value < nodes [y]. key))
653 {
654 Cut (x, y);
655 CascadingCut (y);
656 }
657
658 // update the pointer to the minimal node in the root list
659 if (value < nodes [minRoot]. key)
660 minRoot = x;
661
662 return;
663} /* FibonacciHeap::DecreaseKey */
void CascadingCut(typename FibonacciHeap< element >::NodePtr y)
Does a cascading cut of the tree.
Definition: fibheap.h:610
element minValue
The minimal value that ever appeared in the heap.
Definition: fibheap.h:170

◆ Delete()

template<class element >
void chomp::homology::FibonacciHeap< element >::Delete ( int_t  number)
inline

Removes the element identified by the given number from the heap.

Definition at line 666 of file fibheap.h.

667{
668 element value = minValue;
669 DecreaseKey (number, value - 1);
671 minValue = value;
672 return;
673} /* FibonacciHeap::Delete */
element ExtractMinimum()
Extracts the minimum from the heap.
Definition: fibheap.h:513
void DecreaseKey(int_t number, const element &value)
Decreases the key of the element identified by the given number to the new value.
Definition: fibheap.h:633

◆ ExtractMinimum()

template<class element >
element chomp::homology::FibonacciHeap< element >::ExtractMinimum
inline

Extracts the minimum from the heap.

The element is removed from the heap and its value is returned.

Definition at line 513 of file fibheap.h.

514{
515 // determine the node with the minimum to extract
516 NodePtr nodePtr = minRoot;
517 Node &node = nodes [minRoot];
518
519 // make the children of the node become root nodes
520 // and attach them to the root list
521 NodePtr child = node. child;
522 if (child != NullPtr)
523 {
524 // clear the child pointer in the parent
525 node. child = NullPtr;
526
527 // clear the parent pointers in the children
528 while (nodes [child]. parent != NullPtr)
529 {
530 nodes [child]. parent = NullPtr;
531 child = nodes [child]. right;
532 }
533
534 // attach the children to the root list
535 NodePtr leftChild = child;
536 NodePtr rightChild = nodes [child]. right;
537 NodePtr leftRoot = nodePtr;
538 NodePtr rightRoot = node. right;
539 nodes [leftRoot]. right = rightChild;
540 nodes [rightRoot]. left = leftChild;
541 nodes [leftChild]. right = rightRoot;
542 nodes [rightChild]. left = leftRoot;
543 }
544
545 // make the min root pointer point at any other node
546 // and remove the node from the root list
547 if (node. right != nodePtr)
548 {
549 minRoot = node. right;
550 nodes [minRoot]. left = node. left;
551 nodes [node. left]. right = minRoot;
552 }
553 else
555
556 // reset the fields in the node to avoid any confusion in the future
557 node. left = NullPtr;
558 node. right = NullPtr;
559 node. parent = NullPtr;
560 node. child = NullPtr;
561 node. degree = -1;
562
563 // consolidate the heap
564 Consolidate ();
565
566 return node. key;
567} /* FibonacciHeap::ExtractMinimum */
void Consolidate()
Consolidates the Fibonacci heap by joining roots of equal degree.
Definition: fibheap.h:394

◆ Insert()

template<class element >
int_t chomp::homology::FibonacciHeap< element >::Insert ( const element &  value)
inline

Inserts an element into the heap.

Returns the number of the node which is to be used for decreasing the key or removing this element from the heap. The numbers are returned in the ascending order, starting at 0.

Definition at line 302 of file fibheap.h.

303{
304 // allocate memory for the new node
305 NodePtr nodePtr = nodeCount;
306
307 // update the minimal value
308 if (!nodeCount)
309 minValue = value;
310 else if (value < minValue)
311 minValue = value;
312
313 // fill in the fields of the new node
314 Node &node = nodes [nodePtr];
315 node. key = value;
316 node. mark = false;
317 node. degree = 0;
318 node. number = nodeCount;
319 node. parent = NullPtr;
320 node. child = NullPtr;
321
322 // insert the node to the unordered bidirectional list of roots
323 if (minRoot == NullPtr)
324 {
325 node. left = nodePtr;
326 node. right = nodePtr;
327 }
328 else
329 {
330 node. left = nodes [minRoot]. left;
331 node. right = minRoot;
332 nodes [node. left]. right = nodePtr;
333 nodes [node. right]. left = nodePtr;
334 }
335
336 // make a correction to the pointer to the minimal root if necessary
337 if ((minRoot == NullPtr) || (value < nodes [minRoot]. key))
338 minRoot = nodePtr;
339
340 // return the number of the new node and increase the number of nodes
341 return nodeCount ++;
342} /* FibonacciHeap::Insert */

◆ Minimum()

template<class element >
int_t chomp::homology::FibonacciHeap< element >::Minimum
inline

Returns the number of the node that contains the minimum.

This is the number given to the node at its insertion.

Definition at line 345 of file fibheap.h.

346{
347 return minRoot;
348} /* FibonacciHeap::Minimum */

◆ operator=()

template<class element >
FibonacciHeap< element > & chomp::homology::FibonacciHeap< element >::operator= ( const FibonacciHeap< element > &  )
inlineprivate

The assignment operator is not allowed.

Definition at line 294 of file fibheap.h.

296{
297 throw "Assignment operator for a Fibonacci heap not implemented.";
298 return *this;
299} /* FibonacciHeap::operator = */

◆ showGraph()

template<class element >
void chomp::homology::FibonacciHeap< element >::showGraph ( std::ostream &  out,
int_t  depth,
const typename FibonacciHeap< element >::NodePtr x 
) const
private

Shows a graph starting at the given node using DFS.

Definition at line 230 of file fibheap.h.

232{
233 // show this node
234 for (int_t i = 0; i < depth; ++ i)
235 out << "| ";
236 out << "+-- [" << nodes [x]. number << ": " << nodes [x]. key << "]";
237 if (nodes [x]. left != NullPtr)
238 out << " left=" << nodes [x]. left;
239 if (nodes [x]. right != NullPtr)
240 out << " right=" << nodes [x]. right;
241 if (nodes [x]. parent != NullPtr)
242 out << " parent=" << nodes [x]. parent;
243 if (nodes [x]. child != NullPtr)
244 out << " child=" << nodes [x]. child;
245 out << " deg=" << nodes [x]. degree << "\n";
246
247 // show all the children trees
248 NodePtr child = nodes [x]. child;
249 if (child == NullPtr)
250 return;
251 do
252 {
253 showGraph (out, depth + 1, child);
254 child = nodes [child]. right;
255 } while (child != nodes [x]. child);
256 return;
257} /* FibonacciHeap::showGraph */
void showGraph(std::ostream &out, int_t depth, const typename FibonacciHeap< element >::NodePtr &x) const
Shows a graph starting at the given node using DFS.
Definition: fibheap.h:230

◆ Value()

template<class element >
const element & chomp::homology::FibonacciHeap< element >::Value ( int_t  number) const
inline

Returns the value of the node with the given number.

Definition at line 351 of file fibheap.h.

352{
353 return nodes [number]. key;
354} /* FibonacciHeap::Value */

Friends And Related Function Documentation

◆ operator<<

template<class element >
std::ostream & operator<< ( std::ostream &  out,
const FibonacciHeap< element > &  h 
)
friend

Operator << shows the heap to the output stream.

Essentially, this might be useful for debugging purposes only.

Definition at line 201 of file fibheap.h.

203 {
204 out << "Fibonacci heap (min root = " << h. minRoot << "):\n";
205 NodePtr rootPtr = h. minRoot;
206 if (rootPtr == NullPtr)
207 return out;
208 do
209 {
210 h. showGraph (out, 0, rootPtr);
211 rootPtr = h. nodes [rootPtr]. right;
212 } while (rootPtr != h. minRoot);
213 return out;
214 } /* operator << */

Member Data Documentation

◆ arrayPrepared

template<class element >
int_t chomp::homology::FibonacciHeap< element >::arrayPrepared
private

The size of the prepared data in the array.

Definition at line 185 of file fibheap.h.

◆ auxArray

template<class element >
multitable<NodePtr> chomp::homology::FibonacciHeap< element >::auxArray
private

An auto-expandable array used by the "Consolidate" procedure.

Definition at line 182 of file fibheap.h.

◆ minRoot

template<class element >
NodePtr chomp::homology::FibonacciHeap< element >::minRoot
private

A pointer to the minimal element in the list of roots.

Definition at line 153 of file fibheap.h.

◆ minValue

template<class element >
element chomp::homology::FibonacciHeap< element >::minValue
private

The minimal value that ever appeared in the heap.

This value minus 1 is used as a substitute for the minus infinity while deleting elements from the heap with the use of the "Delete" function.

Definition at line 170 of file fibheap.h.

◆ nodeCount

template<class element >
int_t chomp::homology::FibonacciHeap< element >::nodeCount
private

The total number of nodes inserted to the heap and allocated in the multitable "nodes".

Definition at line 157 of file fibheap.h.

◆ nodes

template<class element >
multitable<Node> chomp::homology::FibonacciHeap< element >::nodes
private

The array of nodes which hold the elements inserted to the heap in this order.

The indices of these elements are returned while inserting them to the heap and can be used to access these nodes directly, e.g., in order to decrease their keys or delete them from the heap.

Definition at line 164 of file fibheap.h.

◆ NullPtr

template<class element >
const int_t chomp::homology::FibonacciHeap< element >::NullPtr
static

The value used for a NULL pointer.

Definition at line 69 of file fibheap.h.


The documentation for this class was generated from the following file: