From http://dsqiu.iteye.com/blog/1714961
1. Introduction to pairing heap
The Fibonacci Heap has two main disadvantages: programming implementation is difficult and the actual efficiency is not as fast as the theory (because of its storage structure and four pointers ). Pairing heap is proposed to make up for two shortcomings of the Fibonacci heap-the time complexity of simple programming operations is the same as that of the Fibonacci heap.
Pairing heap is actually a tree with a heap (maximum or minimum heap) nature. Its features are not determined by its structure, but by its operations (insert, merge,.
1.1 pairing heap ADT
C code
PairingHeapNode{intkey;structPairingHeapNode*child;structPairingHeapNode*sibling;structPairingHeapNode*prev;}PairHeap;
2. pairing heap operations
Note: The Graphic Process is demonstrated by the largest heap, but the code is written by the smallest heap. Sorry!
2. 1. merge two child heaps
C code
Static pairheap * merge_subheaps (pairheap * P, pairheap * q) {If (q = NULL) return P; else if (p-> key <= Q-> key) {q-> Prev = P; P-> sibling = Q-> sibling; If (p-> sibling! = NULL) P-> sibling-> Prev = P; q-> sibling = p-> child; if (Q-> sibling! = NULL) q-> sibling-> Prev = Q; P-> child = Q; return P;} else {q-> Prev = p-> Prev; p-> Prev = Q; P-> sibling = Q-> child; if (p-> sibling! = NULL) P-> sibling-> Prev = P; q-> child = P; return Q ;}}
2.2. Insert a node
C code
Pairheap * pairheap_insert (pairheap * P, int key) {pairheap * node; node = (pairheap *) malloc (sizeof (* node); If (node = NULL) return NULL; node-> key = key; node-> child = node-> sibling = node-> Prev = NULL; If (P = NULL) return node; elsereturn merge_subheaps (p, node );}
2. 3. Add (decrease) a keyword
C code
Pairheap * pairheap_decreasekey (pairheap * P, pairheap * POs, int d) {If (d <0) return P; pos-> key = pos-> key-D; if (P = POS) return P; If (POS-> sibling! = NULL) pos-> sibling-> Prev = pos-> Prev; If (POS-> Prev-> child = POS) pos-> Prev-> child = pos-> sibling; elsepos-> Prev-> sibling = p-> sibling; P-> sibling = NULL; return merge_subheaps (p, pos );}
2. 5. Delete the smallest Node
3. Complete pairing heap code implementation
C code
# Include <stdlib. h> typedef struct pairingheapnode {intkey; operator * child; operator * sibling; operator * Prev;} pairheap; static pairheap * merge_subheaps (pairheap * P, pairheap * q ); static pairheap * combine_siblings (pairheap * P); pairheap * pairheap_insert (pairheap * P, int key) {pairheap * node; node = (pairheap *) malloc (sizeof (* node); If (node = NULL) return NULL; node-> key = K Ey; node-> child = node-> sibling = node-> Prev = NULL; If (P = NULL) return node; elsereturn merge_subheaps (p, node );} pairheap * pairheap_decreasekey (pairheap * P, pairheap * POs, int d) {If (d <0) return P; pos-> key = pos-> key-D; if (P = POS) return P; If (POS-> sibling! = NULL) pos-> sibling-> Prev = pos-> Prev; If (POS-> Prev-> child = POS) pos-> Prev-> child = pos-> sibling; elsepos-> Prev-> sibling = p-> sibling; P-> sibling = NULL; return merge_subheaps (p, pos);} pairheap * pairheap_deletemin (int * Key, pairheap * P) {pairheap * new_root; If (P = NULL) return NULL; else {* Key = p-> key; If (p-> child! = NULL) new_root = combine_siblings (p-> child); free (p);} return new_root;} static pairheap * combine_siblings (pairheap * P) {pairheap * tree_array [1024]; int I, count; If (p-> sibling = NULL) return P; For (COUNT = 0; P! = NULL; count ++) {tree_array [count] = P; P-> Prev-> sibling = NULL; P = p-> sibling;} tree_array [count] = NULL; for (I = 1; I <count; I ++) tree_array [I] = merge_subheaps (tree_array [I-1], tree_array [I]); return tree_array [count-1];} static pairheap * merge_subheaps (pairheap * P, pairheap * q) {If (q = NULL) return P; else if (p-> key <= Q-> key) {q-> Prev = P; P-> sibling = Q-> sibling; If (p-> sibling! = NULL) P-> sibling-> Prev = P; q-> sibling = p-> child; if (Q-> sibling! = NULL) q-> sibling-> Prev = Q; P-> child = Q; return P;} else {q-> Prev = p-> Prev; p-> Prev = Q; P-> sibling = Q-> child; if (p-> sibling! = NULL) P-> sibling-> Prev = P; q-> child = P; return Q ;}}
Summary
Finally, I have made a summary. This article is hard to write. For example, I haven't found an explanation for the cascade cut operation in the Fibonacci heap, the pairing heap definition has not yet been found, but it can only be understood and tested by myself (for example, pairing heap is not clearly defined. I personally understand that the uniqueness of pairing heap must be in his operations, that is, the behavior determines the feature), but it is still relatively complete, hoping to help you. If you have any suggestions, comments, or supplements, please do not hesitate to raise them. Thank you very much. For more information, see the Internet.
Refer:
① Cool ~ World of travel:
Http://mindlee.net/2011/09/26/binomial-heaps/
② Björn B. BRANDENBURG: http://www.cs.unc.edu /~ BBB/# binomial_heaps
③ Cool ~ World of travel:
Http://mindlee.net/2011/09/29/fibonacci-heaps/
Adoo's blog: http://www.roading.org/algorithm/introductiontoalgorithm/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E5%A0%86fibonacci-heaps.html
⑤ Golden_shadow: http://blog.csdn.net/golden_shadow/article/details/6216921
6 Sartaj Sahni: http://www.cise.ufl.edu /~ Sahni/dsaaj/enrich/C13/pairing.htm
7 C ++ template Fibonacci heap, with demonstration: http://ideone.com/9jYnv
Signature "the pairing heap: a new form of self-adjusting Heap": http://www.cs.cmu.edu/afs/cs.cmu.edu/user/sleator/www/papers/pairing-heaps.pdf
BytesVikas Tandi: Http://programmingpraxis.com/2009/08/14/pairing-heaps/
⑩ Http://www.cise.ufl.edu /~ Sahni/cop5536/slides/lec156.pdf
Introduction to algorithms and Wikipedia