Princeton Open Class algorithm 4-1: Priority queue API and basic implementation

Source: Internet
Author: User

The priority queue is one of the containers that can add or remove data to the priority queue and only the largest or smallest number when the data is taken out. Other containers, such as queues and stacks, are taken out in the order in which they are inserted.


The interface for the priority queue is as follows:

public class Maxpq<key extends comparable<key>> {    maxpq ();    void Insert (Key x);    Key Popmax ();    Boolean isEmpty ();}


Application of Priority queue


  • Event-driven simulations

  • Numeric operations

  • Data compression

  • Looking up a graph

  • Arithmetical

  • AI (* find)

  • Statistics

  • Operating system (load Balancing)

  • Discrete optimization (knapsack problem)

  • Junk e-mail filtering (Bayesian spam filtering)


Stacks and queues are special cases of priority queues


Examples of problems


Now there is a large TXT file that contains many integers, the amount of data in the file is very large and the entire file cannot be read into memory. The maximum of 5 integers in the file.


Answer


The problem is solved with a priority queue, which reads one row at a time, joins the data to the priority queue, and removes the smallest element if the queue is longer than 5. When the entire file is read, the remaining elements in the priority queue are the maximum 5 digits required.


Complexity of


It is necessary to find the largest m element in N data.

    • If the sorting algorithm is used to solve the problem, the time complexity is N logn, and the spatial complexity is n.

    • If the base priority queue is solved, the time complexity is n LOGM and the spatial complexity is M.

    • If the data structure of the heap is used, then the time complexity is N LOGM, and the space complexity is M.

    • In theory, the minimum time complexity is n, and the spatial complexity is M.


When using the heap algorithm to solve the problem, its complexity is very close to the theoretical limit.


Code


The simplest way to implement a priority queue is shown below. The complexity of the insertion is 1, and the complexity of the deletion is N.


public class Unorderedpq<key extends comparable<key>> {    private key[] items;    private int N;     public UNORDEREDPQ (int capacity) {        items = (key[]) new comparable[capacity];    }     public void Insert (Key item) {        Items[n] = Item;        n++;    }     Public Key Popmax () {        //Find the largest number        int max = 0;        for (int i=1;i<n;i++) {            if (sortutil.less (Items[max], items[i])) {                max=i;            }        }         Delete the largest number        Key result = Items[max];        Sortutil.exch (items, Max, N-1); Note: Here is N-1, not N        n--;         Returns the maximum number of return        result    ;     public Boolean isEmpty () {        return n==0;    }}


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.