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.