Heap sort and priority queue--An introduction to Algorithms (7)

Source: Internet
Author: User

1. Pre-knowledge

(1) Basic Concepts

, (binary) heap an array, which can be seen as an approximate complete binary tree. Each node in the tree corresponds to an element in the array. In addition to the bottom level, the tree is fully filled and fills from left to right. The array A of the heap includes two properties: A.length gives the length of the array, and A.heap-size indicates how many heap elements are stored in the array (since a may only be part of a valid element of the heap).

Because of this special structure of the heap, we can easily calculate the subscript of the parent node, the left child and the right child according to the subscript I of a node. The calculation formula is as follows:

Parent (i) = I/2;

Left-child (i) = 2i;

Right-child (i) = 2i + 1;

Binary heaps can usually be divided into two forms: maximum heap, minimum heap. In both of these heaps, the value of the node satisfies the nature of the heap. The difference is that in the maximum heap, all nodes except the root node are satisfied:

A[parent (i)]>=a[i],

The smallest heap is opposite. In the use, the maximum heap is usually used in the heap sorting algorithm; The minimum heap is often used to build priority queues.

We define the height of the nodes in the heap as the number of nodes to the top of the longest simple path of the leaf node. The height of the heap is then defined as the height of the root node.

(2) The nature of the maintenance heap

The input of the Max-heapify function is an array A and a subscript I, which is used to maintain the properties of the largest heap of sub-trees labeled I as the root node (this assumes the following left (i) as the root node of the subtree and the following sub-tree labeled Right (i) as the root node satisfies the nature of the maximum heap). The principle of max-heapify is to "step down" the value of a[i] in the maximum heap, so that the following sub-tree, which is the node, re-follows the nature of the maximum heap.

Here is the pseudo-code for the algorithm:

(3) building a heap

We can use the bottom-up method to convert an array a[1~n of size n=a.length to the maximum heap using the process max-heapify. The principle is very simple, that is, from the bottom 2nd level (for illustrative convenience, we call the root node 1th layer, its sub-node is called the 2nd layer, and so on) to start, called Max-heapfy method, until the root node. The algorithm is described as follows:

We can prove the correctness of the above algorithm by using the cyclic invariant (see algorithm Introduction (1)) described previously:

initialization: before the first iteration of the loop, the heap that needs to be built contains only the bottommost elements and is the trivial largest heap .

hold: It is assumed that the cyclic iteration is established at the time of the iteration of the 1≤i < A.LENGTH/2, i.e., the following element labeled I is the subtree of the root node satisfying the nature of the maximum heap; when i = i + 1 o'clock, the execution of the Max-heapify method is guaranteed, When the following left (i) is a subtree of the root node and the following sub-tree labeled Right (i) as the root node satisfies the nature of the maximum heap, the following sub-tree labeled I is the root node satisfies the nature of the maximum heap (this is exactly what this method does). Therefore, the cyclic iteration has a retention.

Termination: When the loop terminates, depending on the persistence, the heap that needs to be built is initialized to contain only the lowest-level elements, extending to include all elements.

Therefore, the algorithm is correct.

2. Heap sequencing (Heap-sort)

Having learned the above preliminary knowledge, we formally begin to introduce the heap sequencing.

The heap sorting algorithm is given below:

Simply put, the principle is based on the maximum nature of the root node element of the maximum heap . We first construct the array to be sorted as the largest heap array. Then traverse the entire tree, each time traversing the "take out" root node, and then call max-heapify to maintain the maximum heap properties of the subtree. This ensures that the element that has been "taken out" over time is the largest of the current remaining elements. ("Out" does not have to really take the element out of the array, we can achieve this by changing the value of the heap-size)

The Java implementation code is given below:

// Test
 Public Static voidMain (string[] args) {int[] Array = {2, 1, 6, 3, 9, 7, 4, 0, 4};heapsort (array);p Rintarray (array);}/** * Heap sort * @param array */ Public Static voidHeapsort (int[] array) {buildmaxheap (array);intHeapSize = Array.Length; for(inti = array.length-1; i > 0; i--) {inttemp = Array[i];array[i] = array[0];array[0] = temp;heapsize--;maxheapify (array, 0, heapsize);}}/** * Maintain maximum heap properties of a tree with index root node * @param array * Heap Array * @param index * node to maintain */ Public Static voidMaxheapify (int[] Array,intIndexintHeapSize) {intLeftindex = 2 * index + 1;intRightindex = 2 * index + 2;intLargeindex;if(Leftindex < heapsize && Array[leftindex] > Array[index]) {largeindex = Leftindex;}Else{largeindex = index;}if(Rightindex < heapsize && Array[rightindex] > Array[largeindex]) {largeindex = Rightindex;}if(Largeindex! = index) {inttemp = Array[largeindex];array[largeindex] = Array[index];array[index] = temp;maxheapify (array, Largeindex, heapSize);}}/** * Build array as maximum heap array * * @param array */ Public Static voidBuildmaxheap (int[] array) {intHeapSize = Array.Length; for(inti = (array.length-2)/2; i >-1; i--) {maxheapify (array, I, heapsize);}} Public Static voidPrintArray (int[] array) { for(intI:array) {System.out.print (i + ") ");} System.out.println ();}

Results:

3. Algorithm Analysis

We gradually analyze the time cost of the algorithm according to the order of execution of the Main method.

① first analyzes the Buildmaxheap method. To analyze the Buildmaxheap method, first analyze the Maxheapify method.

We assume that the Maxheapify method performs a recursive operation each time it executes, in the worst case (exactly half full at the bottom of the tree), the size of the subtree is the original 2/3. The other time is constant θ (1), so we can get a recursive run time:

T (n) ≤t (2N/3) +θ (1),

solvable, T (n) = O (LGN), i.e. the time complexity of the Maxheapify method is O (LGN).

We make a rough estimate of the Buildmaxheap method, which invokes the O (n) secondary maxheapify method, while the other time is the constant θ (1), so the total time complexity of the Buildmaxheap method is: O (NLGN).

But this upper bound is obviously not progressive. Because in fact the Maxheapify method time is related to the height of the node, and most of the nodes are very small in height.

We can use the following properties to get a tight upper bound: the height of the heap containing n elements is [LGN] ([] means rounding down), and a heap with a height of H contains "n/2^ (h+1)" ("" to go up).

The time cost of running the Maxheapify method on a node with a height of H is O (h), then the total time cost of the Buildmaxheap method can be expressed as:

and

Therefore, the time complexity of Buildmaxheap is O (n).

② we then analyze the for loop in the Heapsort. The For loop executes n-1 times, and the time complexity of each loop execution is O (LGN), so the total time complexity is O (NLGN).

③ other run time is constant O (1).

So the time complexity of Heapsort is O (NLGN).

4. Priority queue

In this section, we focus on how to implement the maximum priority queue based on the maximum heap.

(1) What is the maximum priority queue

The maximum priority queue is a data structure used to maintain a set of data composition S. Each of these elements has a critical value (key). A maximum priority queue supports the following operations:

①maximum (s): Returns the element with the largest keyword (key) in the set S;

②extractmax (s): Removes and returns the element with the largest keyword (key) in the set S;

③increasekey (S, x, K): Increases the value of element x to K (assuming that the value of K is not less than x);

④insert (S, x): Inserts the element x into the set S, i.e. S = S U x.

(2) Implementation of the method

The implementation of ①maximum (s) is simple and returns directly to the root node:

The implementation of ②extractmax (s) is as simple as "remove" it from the tree before returning the root node, mount the last element in the heap array to the root node, and then perform the Maxheapify method to maintain the maximum heap properties:

③increasekey (S, x, K) is implemented by modifying the value of the node labeled X to K and constantly comparing it to the value of its parent node until it finally finds the appropriate position to satisfy the maximum heap properties:

The ④insert (S, x) method takes advantage of the Increasekey (s, x, K) method. This is done by assigning x a very small value, and then calling the Increasekey (s, x, K) method to modify the value of X to K.

(3) Additional instructions

The specific code implementation and time cost analysis of the above methods are not given, similar to the heap ordering (in fact, the application deduction of heap sort).

The application of the maximum priority queue should be extensive. For example, for task scheduling, we can use the insert (S, x) method to submit a task, use the Extractmax (s) method to get the task, and the Increasekey (s, x, K) method can be used to modify the priority of the task.

Clearly, the maximum priority queue is simply a handle (handle)of the object that needs to be stored, and its specific representation depends on the application.

The opposite of the maximum priority queue is the minimum priority queue, which is implemented in the same way as the maximum priority queue (the opposite), and it has a different application scenario that will be given later.

Heap sort and priority queue--An introduction to Algorithms (7)

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.