Priority queue [Post]

Source: Internet
Author: User

 

Ah, although the dislike of noip information osai delayed my full learning of the data structure until now, I still want to scream: I hate information Olympics, a boring competition!

However, these days I systematically began to learn the data structure. In fact, every data structure has infinite power. Even the graph theory algorithm I used to sneer at, these two days seem to be worth learning. Take notes on the data structure.

The first thing to talk about is the priority queue.

Why not start with the simplest structure of tables, stacks, and queues? I don't think it is necessary. I don't think it is necessary for myself.

Priority queue: adds a logic to the ADT of a common queue to give priority to elements that meet certain conditions. Generally, the priority queue is divided into the maximum priority queue and the minimum priority queue.

The core implementation of priority queue is a heap.

Heap is a special data structure. It is a binary tree. elements must be filled in the last layer from left to right, and then a new layer is created. Therefore, heap satisfies many interesting properties:

1) if the number of heap elements is N, the height cannot exceed [log n] + 1.

2) Priority Queues can be represented by arrays. In this case, the parent node of node N is at n/2, the left child node is at N * 2, and the right child node is at N * 2 + 1.

These two properties are the basis for implementing the high-performance ADT of the priority queue.

The priority queue is defined on the basis of the heap: the parent node cannot be greater than or less than the child node-depending on whether the heap is maximized or the heap is minimized.

The idea of priority queue is actually very simple. First, construct a heap (this does not need to be explained ).

// Predefined constant <br/> const int minint32 = int_min; <br/> // struct definition <br/> struct priorityqueue <br/>{< br/> int maxsize; <br/> int size; <br/> int * data; <br/>}; <br/> // create a priority queue. <Br/> priorityqueue buildpriorityqueue (INT maxsize) <br/>{< br/> priorityqueue ret; <br/> ret. data = (int *) malloc (sizeof (INT) * (maxsize + 1); <br/> memset (Ret. data, 0, sizeof (INT) * (maxsize + 1); <br/> ret. data [0] = minint32; <br/> ret. maxsize = maxsize; <br/> ret. size = 0; <br/> return ret; <br/>}< br/> void freepriorityqueue (priorityqueue & Q) <br/>{< br/> free (Q. data); <br/>} 

    What I do is minimize the heap and create a dummy element at Q. Data [0. One of the advantages of this is that it fully complies with the sequence number rules of N, N/2, N * 2, N * 2 + 1 (IMAGINE 0/2, 0*2, 0*2 + 1...... ). Second, it is very convenient to join the team.

    The main idea of joining the team is "filtering ". What is filtering? Take a look at the following priority queue:

    4-5--7--10-6

    It can be seen that this heap is in line with the ADT of the priority queue. To insert element 3, we must create a hole in the last entry of the array.

    4-5--7--10-6--[3]

    The introduction of this 3 completely destroys the ADT. In order to make 3 conform to the rules, we need to switch it to position 6.

    4-5--7--10-[3]--6

    It still does not match. We will filter it again until the root.

    [3]-5--7--10-4--6

    The ADT of the priority queue is finally met! This is also the intention of using a dumb dollar-no need to judge whether the root is reached, as long as the dumb dollar is small enough, the root will naturally stop.

    However, there is a problem with repeated yuan. If the duplicate element is more filtered than the original element, the queue will be damaged (first-in-first-out ). Therefore, we should pay special attention to the comparison.

    Provide my enqueue code (there is no actual test repeat metaproblem, but theoretically it verifies the nature of first-in-first-out ).

    Bool enqueue (INT value, priorityqueue & Q) <br/>{< br/> // check parameters <br/> If (Q. size> = Q. maxsize) <br/> return false; <br/> // create a unit. <br/> q. size ++; <br/> int lastindex = Q. size; <br/> int lasthalf = lastindex/2; <br/> q. data [lastindex] = value; <br/> // filter <br/> while (value <q. data [lasthalf]) <br/>{< br/> q. data [lastindex] = Q. data [lasthalf]; <br/> lastindex = lasthalf; <br/> lasthalf = lastindex/2; <br/>}< br/> q. data [lastindex] = value; <br/> return true; <br/>}< br/> 

      Check can be skipped if the time requirement is high ~

      There are also teams. It seems easy to leave the team. Because everything is ready, you can return the root element. However, this creates a hole at the root element, which must be dependent on the "filter down"-basically consistent with the "filter up" idea.

      Code.

      Int dequeue (priorityqueue & Q) <br/>{< br/> // check the parameter <br/> If (Q. size <= 0) <br/> return minint32; <br/> // obtain the minimum unit. <br/> int firstitem = Q. data [1]; <br/> int lastitem = Q. data [q. size --]; <br/> int childindex = 1, I = 0; <br/> // filter <br/> for (I = 1; I * 2 <= Q. size; I = childindex) <br/>{< br/> childindex = I * 2; <br/> If (childindex! = Q. size & Q. data [childindex]> q. data [childindex + 1]) <br/>{< br/> childindex ++; <br/>}< br/> If (lastitem> q. data [childindex]) <br/> q. data [I] = Q. data [childindex]; <br/> else <br/> break; <br/>}< br/> q. data [I] = lastitem; <br/> return firstitem; <br/>}< br/> 

        It is much more difficult to get out of the queue, because there is no assistance from the dumb yuan, and the code efficiency is slightly lower than that of the team.

        As for the increasekey and decreasekey of the priority queue, you can use the method of filtering up and down to complete the process. It is only necessary to know the subscript of the array where the element is located, which leads to the loss of the purity of the priority queue ADT.
        In addition, the delete operation can first decreasekey and then dequeue. (I have not implemented this process, that is, the filtering operation ).

        P.s. The priority queue has a deep relationship with heap sorting. Later, I tried to use this routine to sort 100000 pieces of data, which worked well. Of course, it was a little slower than fast sorting.

         

        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.