5.4.1 using the heap algorithm to implement the superior rank queue

Source: Internet
Author: User
Tags throw exception

A priority queue is usually implemented using a heap algorithm, and the main difficulty in implementing a priority queue is the following:

1) Sequencing Stability: How do I return the task that was first added when I implemented two priority-level tasks?

2) in a tuple comparison, if the (priority, Task) pairs are the same, there is no order of comparison.

3) If the priority of a task changes, how do you move it to a new location in the heap?

4) When the task is deleted, how do you find it is deleted and remove it from the heap queue?

To solve the previous two issues, a list of three elements is implemented, which includes: priority, entry count, task. The entry count is the assignment of an increased number when the task is added so that you know the order in which the task is added so that you can resolve the task priority and return the task that was added first. At the same time because the entry count is different, do not compare to the task, it has been determined that the priority order of the task.


The remaining problem is to find a task that has not been performed and change its priority, or delete it altogether. A lookup task can be implemented in a dictionary that saves a pointer to a task list. Deleting a task or changing the priority will cause the heap ordering of the task queue to be unstable, so it is removed using the tagged method and does not actually delete the related task.

Example:

#python 3.4

Import HEAPQ

Import Itertools

PQ = [] # save a list of heap sorts

Entry_finder = {} # save dictionary for task lookup

removed = ' <removed-task> ' # task-deleted tags

Counter = Itertools.count () # generates a count of task entry order

def add_task (Task, priority=0):

' add a task or update a task's priority '

If task in Entry_finder:

Remove_task (Task)

Count = Next (counter)

Entry = [Priority, count, task]

Entry_finder[task] = entry

Heapq.heappush (PQ, entry)

def remove_task (Task):

' mark an existing task to delete:removed. If there is no throw exception keyerror '

Entry = Entry_finder.pop (Task)

ENTRY[-1] = removed

Def pop_task ():

' Delete the task that has been flagged for deletion and return the task with the lowest priority . If there is no throw Keyerror '

While PQ:

Priority, count, task = Heapq.heappop (PQ)

If task is not removed:

Del Entry_finder[task]

Return task

Raise Keyerror (' Pop from a empty priority queue ')

Add_task (' abc ', 5)

Add_task (' B ', 1)

Add_task (' C ', 2)

Add_task (' BB ', 1)

Print (Pop_task ())

Print (Pop_task ())

Print (Pop_task ())

The resulting output is as follows:

B

Bb

C


Cai Junsheng qq:9073204 Shenzhen

5.4.1 using the heap algorithm to implement the superior rank queue

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.