Beauty of Structure -- three structures of priority queue (1) -- two items heap

Source: Internet
Author: User

The three articles mainly explainTwo items heap, Fibonacci heap, and pairing heapThese three structures are mainly used for the implementation of priority queues. For more information, see Introduction to algorithms and the Internet.

 

Chapter 4 of Introduction to algorithms introduces two topics

 

1. Introduction to item 2 heap

 

A two-item heap is a two-item heap composed of a group of Two-item trees. Before defining a two-item heap, we first define what a two-item tree is.

1.1 Binary Tree

The binary tree is a recursive ordered tree. Definition:

1. Tree B [0] contains only one node

B [k] is composed of two B [k-1] Two Trees, one of which is the left child of another tree.

Below (a) is the recursive definition of B [K], (B) the Binary Tree B [0] to B [4], (c) another way to look at the two-item tree:

The nature of the two trees:

1. For Tree B [K], the tree contains 2 ^ K nodes;

2. The height of the tree is K;

3. If the depth is I, it contains the CIK node, where I = 0, 1, 2..., K;

4. The root degree is K, the maximum,

Inference: The second tree of N nodes. the maximum degree of any node is log (n)

1.2 two items heap

A two-item heap consists of a group of two trees that meet the following requirements:

Every two trees in 1. h follow the minimum heap nature (similar to the small top heap );

2. For any integer k, there is only one k-Level Two Tree in h;

Other definitions:

1. The level of the two items heap is defined as the number of children;

2. Define the root table of the two items heap as a linked list formed by the root node of the two items tree;

 

2. Data Structure

 

After learning about the definition and application scenarios of the two items heap, let's take a look at how to store the two items heap?

First, define the type of each node in the two items heap:

1. Parent: pointing to the parent node

2. Sibling: pointing to the right brother Node

3. Child: defines the subnode of the node.

4. Degree: defines the degree of the node.

5. data required in other application scenarios

struct heap_node{struct heap_node*parent;struct heap_node*next;struct heap_node*child;unsigned intdegree;void*value;struct heap_node**ref;}; 

For defined fields, the nodes in the root table are different from those in the root table. All the nodes in the root table are empty, and sibling points to the next element in the root table; if a node is not in the root table, parent points to the parent node of the node, and sibling points to the sibling node of the node.

Define two types of heap data:

1. header nodes in the root table

2. data required by other applications

struct heap {struct heap_node*head;struct heap_node*min;}; 

 

3. Two heap operations

 

3.1 create a new two-item heap

Make-binomial-Heap,

Allocate and return an object H, head [H] = nil; running time is (1 ).

Only point the header node of the root table to NULL:

Static inline void heap_init (struct heap * heap) {heap-> head = NULL; heap-> min = NULL; // other operations

}

3.2 search for the minimum keyword

Binomial-heap-minimum,

Returns a pointer pointing to the smallest keyword node.

Because every two-item tree in the Two-item heap follows the minimum heap nature, the minimum element must be in the root table. You can traverse the root table once to find the minimum element:

BINOMIAL-HEAP-MINIMUM(H)

1  y  NIL

2  x head[H]

3  min 

4  while x  NIL

5      do if key[x] < min

6            then min key[x]

7                 y x

8         x sibling[x]

9  return y

3.3 merge operation (MOST)

Binomial-heap-Union

Merging two items is a complicated but crucial process. Here we assume that the two items to be merged are H1 and H2, if you simply merge the root tables of H1 and H2 (two linked lists), it may be a violation of the second definition of the two heaps:

B [k] is composed of two B [k-1] Two Trees, one of which is the child of another tree.

This is the main problem to be solved for the two heap merge operations: after the two heap merge operations are completed, two nodes with the same degree may exist in the root table, you need to combine nodes with the same degree into a new node.

Here we further convert this problem into: the degree of each node in the root table of H1 and H2 is known, and the root tables of H1 and H2 are sorted by degree, how to merge H1 and H2 root tables, and the new root table does not have two nodes with the same degree.

A simple idea is to merge H1 and H2, and then adjust the nodes with the same degree of degree to merge them, this ensures that the new root table does not store nodes with the same degree. Then the BASIC Program Framework:

Union (H1, H2)
{
#1: Merge the root tables of H1 and H2, generate a new root table h, and the nodes of H are sorted by degree;
#2: merge two nodes with the same moderate number in h until there are no nodes with the same degree in the root table h;
}
Continuing to refine the above ideas, #1 is relatively simple, similar to the merging idea in Merge Sorting, but for #2, because h is sorted, we only need to traverse H. If there are two identical degrees, merge them.

 

Case1: The degree [x] of the current node is not the same as the degree [next-x] of the subsequent node; move backward;

Case2: The degree [x] of the current node is equal to the subsequent degree [sibling [next-X]; move later; (the three depths are the same)

Else:

Degree [x] = degree [next-x]:

Case3: If (Key [x] <= Key [next-x]): sibling [x] = sibling [next-x], binomial-Link (next-x, x) // next-X is assigned to the left child of X.

Case4: If (Key [x]> key [next-x]): sibling [Prev-x] = Next-X, binomial-Link (x, next-x) // X is assigned to the left child of next-X.

 

3.4 insert a node

Binomial-heap-insert (H, X)

First, construct a two-way heap that only contains one element.H', O (1), merge again, O (log (n ));

1  H'  MAKE-BINOMIAL-HEAP()

2  p[x]  NIL

3  child[x]  NIL

4  sibling[x]  NIL

5  degree[x]  0

6  head[H']  x

7  H  BINOMIAL-HEAP-UNION(H,H')

3.5 minimum element Extraction

BINOMIAL-HEAP-EXTRACT-MIN

1  find the root x with the minimum key in the root list of H, MAKE-BINOMIAL-HEAP

and remove x from the root list of H

2  H'  MAKE-BINOMIAL-HEAP()

3  reverse the order of the linked list of x's children,

And setHead[H'] To point to the head of the resulting list reversely connects the child of XHead[H']

4HBinomial-heap-Union (H,H') Merge

5  return x

 

 

 

3.6 reduce the value of a keyword

Binomial-heap-decrease-Key (H, X, K)

Reduce y to 7 and then bubble up.

3.7 delete a keyword

Binomial-heap-delete (H, X)

Use the deleted element as an infinitely smallBinomial-heap-decrease-Key (H, X, K)Bubble rises to the root of the tree and then deletes it.BINOMIAL-HEAP-EXTRACT-MIN (H)

 

1 BINOMIAL-HEAP-DECREASE-KEY(H,x,-)

2 BINOMIAL-HEAP-EXTRACT-MIN(H)

 

4. Application Status

Under what circumstances should we use "two items Heap "?

If Union operations are not supported, the data structure of a normal heap is an ideal data structure. However, if you want to support merge operations on a set, it is best to use two heaps or a Fibonacci heap. The worst case for a normal heap in a union operation is O (n ), but the two heap and the Fibonacci heap are O (lgn ).

Binary heap binomial heap () Fibonacci heap

Binary heap (worst case) binary heap (worst case) (Fibonacci heap (split ))

Procedure    (worst-case)    (worst-case)       (amortized)

--------------------------------------------------------------

MAKE-HEAP         (1)            (1)            (1)

INSERT           (lg n)        O(lg n)           (1)

MINIMUM          (1)          O(lg n)           (l)

EXTRACT-MIN      (lg n)        (1g n)         O(lg n)

UNION             (n)          O(lg n)           (1)

DECREASE-KEY     (lg n)        (lg n)           (1)

DELETE            (1g n)        (lg n)         O(lg n)

 

5. References

Introduction to algorithms: http://net.pku.edu.cn /~ Course/cs101/2007/resource/intro2algorithm/book6/chapturehtm

Blog Garden (containing code): http://www.cnblogs.com/xuqiang/archive/2011/06/01/2065549.html

Iteye (full code): http://dsqiu.iteye.com/blog/1714961

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.