Minimum priority queue (based on the minimum binary heap algorithm)

Source: Internet
Author: User

In the Minimum Spanning Tree prim algorithm, the minimum priority queue can be used to improve the time complexity. In the single-source shortest path Dijkstra algorithm, the minimum priority queue can also be used to improve the time complexity of the algorithm. The minimum priority queue can be implemented in many ways, such as based on the binary least heap or the Fibonacci heap. Here is the C # Implementation of the minimal binary heap. The principle is based on the pseudocode in the book, but I have made some improvements. For example, if the book key value is changed, the book can only become larger, this restriction is removed here. The function of selecting elements based on satellite values is also provided. The following code is used:

/// <Summary> /// queue element packaging class /// </Summary> /// <typeparam name = "T"> actual element type </typeparam> public class queueelement <t> {// <summary> // key value // </Summary> Public int keyValue {Get; internal set ;}/// <summary> /// actual object /// </Summary> Public t element {Get; private set ;}public queueelement (T item, int keyval) {keyValue = keyval; element = item ;}/// <summary> // minimum priority queue /// </Summary> /// <t Ypeparam name = "T"> </typeparam> public class minheapqueue <t> {// <summary> // stores queue elements, which is implemented by using list. /// </Summary> private list <queueelement <t> _ queuevalues = new list <queueelement <t> (); /// <summary> /// Number of queue elements /// </Summary> Public int count {get {return _ queuevalues. count ;}} /// <summary> /// obtain the element with the smallest key value in the queue /// </Summary> /// <returns> </returns> Public t getminimum () {return _ queuevalues [0]. Element ;} /// <summary> /// retrieve the element with the smallest key value from the queue /// </Summary> /// <returns> </returns> Public t extractmin () {If (_ queuevalues. count <= 0) {Throw new exception ("the queue is empty");} t themin = _ queuevalues [0]. element; int thetail = count-1; _ queuevalues [0] = _ queuevalues [thetail]; _ queuevalues. removeat (thetail); minheapify (0); Return themin, this function has the same functions as downadjust. /// </summar Y> // <Param name = "I"> </param> Public void minheapify (int I) {int heapsize = count; int Thel = heapl (I ); int ther = heapr (I); int theleast = I; If (Thel 

Note that the priority queue algorithm based on the maximum binary heap in my previous article is similar to this one, but there are minor errors in the previous algorithm, and the priority queue in this algorithm has passed the test, no problem. You can compare to see where the error is.

PS: Now that you are on the homepage, add some comments.

 

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.