34#ifndef _CHOMP_STRUCT_FIBHEAP_H_
35#define _CHOMP_STRUCT_FIBHEAP_H_
61template <
class element>
204 out <<
"Fibonacci heap (min root = " << h.
minRoot <<
"):\n";
211 rootPtr = h.
nodes [rootPtr]. right;
212 }
while (rootPtr != h.
minRoot);
218template <
class element>
229template <
class element>
234 for (
int_t i = 0; i < depth; ++ i)
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";
248 NodePtr child = nodes [x]. child;
249 if (child == NullPtr)
253 showGraph (out, depth + 1, child);
254 child = nodes [child]. right;
255 }
while (child != nodes [x]. child);
259template <
class element>
261 minRoot (NullPtr), nodeCount (0), nodes (maxSize), arrayPrepared (0)
273template <
class element>
286template <
class element>
289 throw "Copy constructor for a Fibonacci heap not implemented.";
293template <
class element>
297 throw "Assignment operator for a Fibonacci heap not implemented.";
301template <
class element>
310 else if (value < minValue)
314 Node &node = nodes [nodePtr];
318 node. number = nodeCount;
319 node. parent = NullPtr;
320 node. child = NullPtr;
323 if (minRoot == NullPtr)
325 node. left = nodePtr;
326 node. right = nodePtr;
330 node. left = nodes [minRoot]. left;
331 node. right = minRoot;
332 nodes [node. left]. right = nodePtr;
333 nodes [node. right]. left = nodePtr;
337 if ((minRoot == NullPtr) || (value < nodes [minRoot]. key))
344template <
class element>
350template <
class element>
353 return nodes [number]. key;
393template <
class element>
397 if (minRoot == NullPtr)
407 int_t d = nodes [x]. degree;
410 node = nodes [node]. right;
413 while (arrayPrepared <= d)
414 auxArray [arrayPrepared ++] = NullPtr;
421 while (auxArray [d] != NullPtr)
427 if (nodes [y]. key < nodes [x]. key)
435 nodes [y]. mark =
false;
438 nodes [y]. parent = x;
439 NodePtr child = nodes [x]. child;
440 if (child == NullPtr)
442 nodes [x]. child = y;
444 nodes [y]. right = y;
448 nodes [y]. left = child;
449 nodes [y]. right = nodes [child]. right;
450 nodes [child]. right = y;
451 nodes [nodes [y]. right]. left = y;
453 ++ nodes [x]. degree;
456 auxArray [d] = NullPtr;
462 if (arrayPrepared <= d)
463 auxArray [arrayPrepared ++] = NullPtr;
473 }
while (node != minRoot);
477 for (
int_t d = 0; d < maxDegree; ++ d)
480 if (auxArray [d] == NullPtr)
484 NodePtr nodePtr = auxArray [d];
485 auxArray [d] = NullPtr;
486 Node &node = nodes [nodePtr];
489 if (minRoot == NullPtr)
491 node. left = nodePtr;
492 node. right = nodePtr;
496 node. left = minRoot;
497 node. right = nodes [minRoot]. right;
498 nodes [node. left]. right = nodePtr;
499 nodes [node. right]. left = nodePtr;
503 if ((minRoot == NullPtr) ||
504 (node. key < nodes [minRoot]. key))
512template <
class element>
517 Node &node = nodes [minRoot];
522 if (child != NullPtr)
525 node. child = NullPtr;
528 while (nodes [child]. parent != NullPtr)
530 nodes [child]. parent = NullPtr;
531 child = nodes [child]. right;
536 NodePtr rightChild = nodes [child]. right;
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;
547 if (node. right != nodePtr)
549 minRoot = node. right;
550 nodes [minRoot]. left = node. left;
551 nodes [node. left]. right = minRoot;
557 node. left = NullPtr;
558 node. right = NullPtr;
559 node. parent = NullPtr;
560 node. child = NullPtr;
569template <
class element>
576 nodes [x]. parent = NullPtr;
577 if (nodes [x]. right == x)
579 nodes [y]. child = NullPtr;
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;
592 -- nodes [y]. degree;
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;
603 nodes [x]. mark =
false;
608template <
class element>
613 while (!(nodes [y]. parent == NullPtr))
617 if (!nodes [y]. mark)
619 nodes [y]. mark =
true;
632template <
class element>
634 const element &value)
640 if (nodes [x]. degree == -1)
644 if (value < minValue)
648 nodes [x]. key = value;
652 if ((y != NullPtr) && (value < nodes [y]. key))
659 if (value < nodes [minRoot]. key)
665template <
class element>
668 element value = minValue;
669 DecreaseKey (number, value - 1);
This template contains the definition of a Fibonacci heap that can be used as an efficient priority q...
int_t arrayPrepared
The size of the prepared data in the array.
FibonacciHeap< element > & operator=(const FibonacciHeap< element > &)
The assignment operator is not allowed.
int_t NodePtr
The type of a pointer to a node in the Fibonacci heap.
int_t Minimum() const
Returns the number of the node that contains the minimum.
void CascadingCut(typename FibonacciHeap< element >::NodePtr y)
Does a cascading cut of the tree.
void Cut(const typename FibonacciHeap< element >::NodePtr &x, const typename FibonacciHeap< element >::NodePtr &y)
Cuts the tree.
~FibonacciHeap()
The destructor of a Fibonacci heap.
FibonacciHeap(int_t maxSize=0)
The constructor of an empty Fibonacci heap.
element minValue
The minimal value that ever appeared in the heap.
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.
int_t Insert(const element &value)
Inserts an element into the heap.
multitable< Node > nodes
The array of nodes which hold the elements inserted to the heap in this order.
void Consolidate()
Consolidates the Fibonacci heap by joining roots of equal degree.
void Delete(int_t number)
Removes the element identified by the given number from the heap.
NodePtr minRoot
A pointer to the minimal element in the list of roots.
int_t nodeCount
The total number of nodes inserted to the heap and allocated in the multitable "nodes".
element ExtractMinimum()
Extracts the minimum from the heap.
const element & Value(int_t number) const
Returns the value of the node with the given number.
void DecreaseKey(int_t number, const element &value)
Decreases the key of the element identified by the given number to the new value.
static const int_t NullPtr
The value used for a NULL pointer.
multitable< NodePtr > auxArray
An auto-expandable array used by the "Consolidate" procedure.
friend std::ostream & operator<<(std::ostream &out, const FibonacciHeap< element > &h)
Operator << shows the heap to the output stream.
A container for elements placed in a table (like a vector) that is actually built of many smaller tab...
int int_t
Index type for indexing arrays, counting cubes, etc.
This file contains the definition of the container "multitable" which is essentially an automatically...
This namespace contains the entire CHomP library interface.
The structure that holds a graph node for the graph representation of a Fibonacci heap.
NodePtr right
A pointer to the right sibling node.
NodePtr left
A pointer to the left sibling node.
NodePtr child
A pointer to one of the children nodes.
int_t number
The number of this node.
element key
The value of the element that is used to compare the elements.
NodePtr parent
A pointer to the parent node.
int_t degree
The degree of the node, i.e., the number of its children.
bool mark
A mark bit which indicates whether the node has lost a child since the last time it was made a child ...