An introduction to the algorithm-Fibonacci heap

Source: Internet
Author: User

A heaps is a set of root trees with the smallest heap , that is, each tree in the collection has a keyword that has a parent node that is less than or equal to a child node.

For each node x, the main properties are as follows:

Name

Description

Recorded as

Key words

Value of node storage

X.key

Parent node

Father of the knot.

X.p

Left brother

The left brother of the knot.

X.left

Right brother

The right brother of the knot.

X.right

Children

Knot of a son of a knot point

X.child

Degree

Number of sons of the knot, not including grandchildren and the lower level

X.degree

Mark

Does the knot have a son being removed?

X.mark

Heap h itself property:

Name

Description

Recorded as

Minimum junction point

The smallest root node.

H.min

Number of nodes

The number of nodes in the entire heap.

H.n

The set of root nodes of all trees is called the root list, which can be formed by the left-right value of the h.min and the minimum nodes.

Operation

Two-item heap (worst case scenario)

Netherfield Heaps (amortization)

Make-heap

O (1)

O (1)

INSERT

O (LGN)

O (1)

MINIMUM

O (1)

O (1)

Extract-min

O (LGN)

O (LGN)

UNION

O (N)

O (1)

Decrease-key

O (LGN)

O (1)

DELETE

O (LGN)

O (LGN)

As you can see, the two-item heap has a constant level of time on the insert, merge, and modify value operations, and is more efficient.

Insert Operation

Inserts a node x into H, creates a new heap with only x if the heap is empty, or adds x to the root list

21 is inserted into the heap as shown in the figure

The code is as follows:

1 voidFibheapinsert (h,x) {2X.degree =0;3X.P =NULL;4X.child =NULL;5X.mark =false;6     if(H.min = =NULL) {7         //Create a new root list containing only x8H.min =x;9     }Ten     Else{ One         //Insert x into the root list of H A         if(x.key<H.min.key) -H.min =x; -h.n++; the     } -}
finding the smallest node.

Get it directly from H.min

merger of two Fibonacci heaps

Connect the root chain of the two heaps directly, and then determine the new minimum node.

1 fibheapunion (h1,h2) {2     H = Makefibheap (); // Initializes a new heap 3      for (p=h1.min;p.right!=null;p=p.right); 4     P.right = H2.min; // Link the root chain of the H2 to the right side of the H1 's root list 5     H.min = (H1.min < h2.min)? H1.min:H2.min; 6     H.N = H1.N + H2.N; 7     return H; 8 }
Extracting minimum points

First, each child of the minimal node becomes the root node, and the minimum node is removed from the root list. H.min points to the right sibling of the smallest node, and then merges the nodes with the same degrees to form a new root-linked list, the synthesis process is iterative, the number of nodes after the synthesis is added 1, and then the excess numbers are checked for the same nodes, until all the degrees of the nodes in the root list have only one.

(a)-(b) 3 of the 3 sons were moved to the root link and then 3 were deleted, and 3 of the right brothers 17 were h.min. (c)-(d) Traversing the root chain list starting from 17, with a degree of 2,23 of 0 for the degree of 1,24, which is the first occurrence of the degree value, recorded in the secondary array. (e) The degree of 7 is also 0, and 23 repeats, and 7<23, so 23 of the sons inserted into the 7, 7 of the degree becomes 1. (f)-(g) 7 of the new value and 17 repetitions, 17 to 7 of the sons, 7 of the degree into 2. (h) A new degree of 7 and 24 repetitions, and 24 of the sons inserted into 7, 7 to 3. (i)-(j) 21 The degree of 0,18 is 1, inserted into the auxiliary array. (k) The degree of 52 is 0, the son inserted into 21, 21 degrees into 1 and 18 repetitions, 21 inserted into the son of 18.

Fibheapextractmin (H) {z=h.min; if(Z! =NULL) {         for(x=z.child;x!=null;x=x.right)            {H.rootlist.add (x); X.P=NULL;        } h.rootlist.remove (z); if(z==z.right) H.min=NULL; Else{h.min=Z.right;        Consolidate (H); } H.N--; }    returnZ;} Consolidate (H) {a[n+1];//The auxiliary array used to store the number of degrees, and N is the maximum degree of the root node in the entire heap     for(i=0; i<=n;i++) {A[i]=NULL; }     for(w=h.child;w!=null;w=w.right) {x=W; D=X.degree;  while(A[d]!=null) {//has the same degree of node, merging it with the previous node .y=A[d]; if(x.key>y.key) Exchange (x, y);//The key value is large as a child nodeFibheaplink (H,Y,X);//y Insert in the sub-node of xa[d]=NULL; D++; } A[d]=x; } h.min=NULL;  for(i=0; i<=n;i++) {//rebuild the root list and the minimum node after the merge is complete.        if(a[i]!=NULL) {            if(h.min==NULL) {                //Create a new heap with only a[i]H.min =A[i]; }            Else{h.rootlist.add (a[i]); if(a[i].key<h.min.key) H.min=A[i];    }}}}fibheaplink (h,y,x) {h.rootlist.remove (y);  for(t=x.child;t.right!=null;t=t.right); T.right=y; X.degree++; Y,mark=false;}
keyword impairment

The key of the X node is changed to K value, if the change will affect the minimum heap properties, the X is moved to the root list, and the parent node is marked, and if the previous parent node has been marked, the parent node is also moved to the root linked list, the tag of the node. Recursive this process until you mark an unmarked parent node, this operation is called cascade Delete. Finally, if the new key value is less than the original minimum node value, the minimum node becomes x.

(a)-(b) Change 46 to 15 and 15 to the root list, and 24 to be marked. (c) Change 35 to 5, 26 is already marked SO (d) 26 is also moved to the root chain list. (e) 26 of the parent node 24 is also marked, so 24 is also moved to the root linked list, 24 of the parent node 7 is already on the root linked list, the process terminates.

1 Fibheapdecreasekey (h,x,k) {2     if(k>X.key)3         return;4X.key =K;5y =X.P;6     if(Y!=nulll && X.key <Y.key) {7 Cut (h,x,y);8Cascadingcut (H,y);//when the X.key is less than the parent key, X moves to the root list and cascade deletes the parent node .9     }Ten     if(x.key<H.min.key) OneH.min =x; A } -  - Cut (h,x,y) { the      for(t=y.child;t.right!=x;t=t.right); -T.right = T.right.right;//remove X from child list of Y -y.degree--; -      for(t=h.min;t.left!=null;t=t.left); +T.left = x;//add x to the root list of H -X.P =NULL; +X.mark =false; A } at  - Cascadingcut (h,y) { -z =Y.P; -     if(z!=NULL) { -         if(y.mark==false) -Y.mark =true;//y is not marked, then Y is marked in         Else{ - Cut (h,y,z); toCascadingcut (H,Z);//y is marked, the z is moved to the root list and the parent node of y is cascade deleted . +         } -     } the}
Delete a node.

Modify the value of node x to infinity, and then perform the extract minimum operation.

An introduction to the algorithm-Fibonacci heap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.