Introduction to algorithms Chapter 1 Fibonacci heap

Source: Internet
Author: User

You can only write out a part of it.

I. Summary 1. Fibonacci heap

The Fibonacci heap is a merged heap.

In an operation that does not involve deletion (except extract and delete), the operation only requires the O (1) run time.

When the number of extract and delete operations is small, the Fibonacci heap can achieve better operation efficiency.

The Fibonacci heap cannot effectively support search operations.

Fast Algorithms used to solve problems such as the Minimum Spanning Tree and finding the single-source shortest path all use the Fibonacci heap.

2. Fibonacci Heap Structure

The Fibonacci heap consists of a group of smallest heaps, which are unordered trees with roots.

Node structure:

// Struct node {int key; // key word int data; // satellite data node * P; // pointer to the parent node, parent or left brother node * child; // pointer to one of the children node * left; // pointer to left brother node * right; // pointer to the right brother int degree; // bool mark ;}

// Fibonacci Heap Structure class fib_heap {public: node * min; node * nil ;}


Ii. Code 1. fib_heap.h
# Include <iostream> # include <cmath> using namespace STD; // two-item heap node Structure struct node {int key; // key word int data; // satellite data node * P; // pointer to the parent node, parent or left brother node * child; // pointer to a child node * left; // pointer to the left sibling node * right; // pointer to the right sibling int degree; // degree bool mark; // initialize node (int n, node * nil ): key (N), P (NiL), Child (NiL), left (this), right (this), degree (0), mark (false ){}}; // two-item Heap Structure class fib_heap {public: node * min; node * nil; int N; // number of nodes currently contained in H // constructor fib_he AP (): n (0) {nil = new node (-1, nil);} fig (node * nil): n (0) {nil = nil ;} // 19.2 void make_fib_heap (); node * maid (); void maid (node * y, node * X); void maid (Fig * H2 ); void fib_heap_insert (node * X); node * fib_heap_extract_min (); void Merge (node * X, int K); void fib_heap_delete (node * X); void merge lidate (); void cut (node * X, node * Y); void cascading_cut (node * y ); }; // Construct an empty two-way heap void fib_heap: make_fib_heap () {// initialize the object min = nil;} // find the minimum keyword node * fib_heap :: fib_heap_minimum () {// The minimum keyword must be in the node * x = min, * Y = nil; int min = 0x7fffff; // traverse the root node do of each two trees {// find the minimum value if (X-> key <min) {min = x-> key; y = x ;} X = x-> right;} while (X! = Min); Return y;} // connect the tree with node y as the root to the tree with node Z as the root, so that Z becomes the parent node void fib_heap of Y :: fib_heap_link (node * y, node * X) {// remove y from the root list of Hy-> left-> right = Y-> right; y-> right-> left = Y-> left; // make y a child of X, incrementing degree [x] Y-> right = x-> child-> right; y-> left = x-> child; X-> child-> right-> left = y; X-> child-> right = y; y-> P = X; x-> degree ++; y-> MARK = false;} // merge the two heap into void fib_heap: fib_heap_union (FIB _ Heap * H2) {// H is the merging node for outputting fib_heap * H = new fib_heap (NiL); H-> make_fib_heap (); fib_heap * H1 = this; // concatenate the root list if H2 with the root list of conditioned (H1-> Min! = Nil & H2-> Min! = Nil) {node * t = h1-> Min-> right; H1-> Min-> right = h2-> min; h2-> Min-> left-> right = T; t-> left = h2-> Min-> left; H2-> Min-> left = h1-> min ;} if (H1-> min = NULL | (H2-> Min! = NULL & H2-> Min-> key <H1-> Min-> key) H-> min = h2-> min; // set N to the sum point h-> N = h1-> N + H2-> N; // free objects H1 and H2 min = H-> min; N = H-> N;} // insert node X into heap H. Void fib_heap: fib_heap_insert (node * X) {/* X-> P = nil; x-> child = nil; X-> sibling = nil; X-> degree = 0; hh-> head = X; * /// concatenate the root list containing X with root list (Min! = Nil) {X-> right = min-> right; X-> left = min; Min-> right-> left = x; Min-> right = x ;} if (min = nil | X-> key <min-> key) min = x; n ++;} // extracts the node * fib_heap with the minimum Keyword :: fib_heap_extract_min () {node * X, * Y; node * z = min; If (Z! = Nil) {x = z-> child; if (X! = Nil) {do {Y = x-> right; // Add X to the root list of HX-> right = min-> right; X-> left = min; min-> right-> left = x; Min-> right = x; X-> P = nil; X = y;} while (X! = Z-> child);} // remove Z from the root list of Hz-> right-> left = z-> left; z-> left-> right = z-> right; if (Z = z-> right) min = nil; else {min = z-> right; consolidate ();} n --;} return Z;} // reduce the keyword of a node X in two heap h to a new value kvoid fib_heap :: fib_heap_decrease_key (node * X, int K) {// error if (k> X-> key) {cout <"new key is greater than current key" <Endl; return;} // reduce a keyword in the same way as the minimum binary heap, so that the keyword bubbles up In the heap X-> key = K; node * Y = x-> P; If (y! = Nil & X-> key <Y-> key) {cut (x, y); cascading_cut (y) ;}if (X-> key <min-> key) min = x;} // delete a keyword void fib_heap: fib_heap_delete (node * X) {// change the value to a minimum value and raise it to the top of the heap: fib_heap_decrease_key (x,-0x7fffff ); // Delete the top heap element maid () ;}# define pnode node * void MAID: interval lidate () {int d = Log (N * 1.0), I; node ** A = new node * [d + 1]; // node * A [10]; for (I = 0; I <= D; I ++) A [I] = nil; node * w = min, * X, * Y; int D; If (W! = Nil) {do {x = W; D = x-> degree; while (A [d]! = Nil) {Y = A [d]; If (X-> key> Y-> key) Swap (x, y); fig (Y, X ); A [d] = nil; D ++;} A [d] = x; W = W-> right;} while (W! = Min);} min = nil; for (I = 0; I <= D; I ++) {if (a [I]! = Nil) {// Add a [I] to the root list of ICF (Min! = Nil) {A [I]-> right = min-> right; A [I]-> left = min; Min-> right-> left = A [I]; min-> right = A [I];} If (min = nil | A [I]-> key <min-> key) min = A [I] ;}}void fib_heap: Cut (node * X, node * Y) {// remove X from the Child list of y, devrementing degree [y] If (Y-> degree = 1) Y-> child = nil; else {Y-> child = x-> right; x-> left-> right = x-> right; X-> right-> left = x-> left;} y-> degree = 0; // Add X to the root list of HX-> right = Min-> right; X-> left = min; Min-> right-> left = x; Min-> right = x; X-> P = nil; x-> MARK = false;} void fib_heap: cascading_cut (node * Y) {node * z = Y-> P; If (Z! = Nil) {If (Y-> MARK = false) Y-> MARK = true; else {cut (Y, Z); cascading_cut (z );}}}


2. Main. cpp


# Include <iostream> using namespace STD; # include "maid" int main () {char ch; int N; // generate an empty two-way heap. fib_heap * H = new fib_heap; H-> make_fib_heap (); // various tests. While (CIN> CH) {Switch (CH) {Case 'I': // insert an element {CIN> N; node * x = new node (n, H-> nil); H-> maid (X ); break;} case 'M': // returns the minimum value {node * ret = H-> fig (); If (ret = H-> nil) cout <"empty" <Endl; elsecout <ret-> key <Endl; break;} case 'K': // change the value of a keyword, make it smaller {// because there is no search function, you can only test node * ret = H-> fig (); If (ret = H-> nil) for the smallest Node) cout <"empty" <Endl; else {CIN> N; H-> fig (Ret, n);} break;} case 'E ': // extract the minimum keyword value and delete {node * ret = H-> fig (); If (ret = H-> nil) from the heap) cout <"empty" <Endl; elsecout <ret-> key <Endl; break;} case 'D ': // delete a node {node * ret = H-> maid (); If (ret = H-> nil) cout <"empty" <Endl; elseh-> maid (RET); break ;}}return 0 ;}
Iii. Exercise 20.1 Fibonacci heap 20.2 merged heap


20.3 reduce a keyword and delete a node

This statement is hard to understand: P300. once the second child is lost, the connection between x and its parent node is cut off.



20.4 The most generous World 4. Another method to delete question 20-1 is 20-2 other Fibonacci heap operations

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: 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.