11,432,119 members (59,097 online)

# Priority queue

, 28 Mar 2004
 Rate this:
Another addition to the System.Collections namespace - a priority queue, also known as a heap.

## Introduction

Many algorithms - for instance Dijkstra's well-known search for the shortest path - run most efficient with a 'semi-sorted' list, which always has the smallest element up front. The most efficient structure for this purpose is a heap, also called priority queue.

## Usage

The `PriorityQueue` class is built in the guidelines of the `System.Collections` namespace. Comparison is done through the `IComparer` interface, or, if none is specified, through the `IComparable` implementation of the provided objects. The objects in the PQ need not have all the same type but they have to be comparable with each other.

Next to the standard `System.Collections` methods (`Count`, `Contains`, ... you name it), the PQ supports 3 main operations:

• `Push()`: This will add an object to the PQ, reforming the structure so that this element is at the front if it is the currently smallest. The complexity of the operation is O(ld n).
• `Pop()`: This will remove and return the current head of the PQ, which always is the smallest element. Complexity O(ld n).
• `Update()`: It might be necessary to change elements that are already in the queue. Because this is not very common (you need to find the element first), it can only be done by the explicit `IList` implementation (the indexer should not be used in any other case). Once you set the indexer, the PQ will automatically reorder. Complexity O(ld n) (surprise;))

## Example

```/// We create a PQ, add some random numbers and retreive them in sorted order:

IPriorityQueue PQ = new BinaryPriorityQueue();
Random R = new Random();
int i;
for(i=0;i<50;i++)
{
PQ.Push(R.Next(1000));
}
while(PQ.Count>0)
Console.WriteLine(PQ.Pop());```

## History

• 30.3.04 - Inserted.
• 31.3.04 - renamed the main class to `BinaryPriorityQueue`.

Created `IPriorityQueue` interface.

A list of licenses authors might use can be found here

## Share

Software Developer (Senior)
Germany
I did my diploma in Dresden and Sydney where I dealt with algorithms, agents and other cool AI stuff. Now I moved to Frankfurt to work on my PhD dealing with software structures for artificial intelligence systems. If I can, I do things in C# and ASP.NET, but if I have to, my C++, Java and SQL are not that bad.
Long Live .NET.

 First Prev Next
 Danka !!! :) Samedi Ishmael Medivh25-Oct-14 11:53 Samedi Ishmael Medivh 25-Oct-14 11:53
 Thanks a lot majdaldin122-Apr-11 3:59 majdaldin1 22-Apr-11 3:59
 My vote of 3 LassarRRRRRR17-Feb-11 6:01 LassarRRRRRR 17-Feb-11 6:01
 My vote of 4 s.hamed_rezvani12-Jan-11 4:48 s.hamed_rezvani 12-Jan-11 4:48
 Generic version Olmo del Corral12-Mar-09 12:01 Olmo del Corral 12-Mar-09 12:01
 Updated for non duplicate value in list. bestwasin2-Feb-09 6:26 bestwasin 2-Feb-09 6:26
 implemntinig priority Quesues Member 331791728-Aug-08 8:42 Member 3317917 28-Aug-08 8:42
 implemntinig priority Quesues Member 331791728-Aug-08 8:35 Member 3317917 28-Aug-08 8:35
 Fast Priority Queue Implementation of Dijkstra Shortest Path Algorithm Tolga Birdal31-Mar-08 6:38 Tolga Birdal 31-Mar-08 6:38
 remove method belal_haija22-Mar-08 16:41 belal_haija 22-Mar-08 16:41
 hashtable warnockm20-Nov-07 16:03 warnockm 20-Nov-07 16:03
 Correction David Krantz15-Dec-06 0:49 David Krantz 15-Dec-06 0:49
 A-Star Implementation CastorTiu22-Sep-06 22:02 CastorTiu 22-Sep-06 22:02
 Take care while using multiple threads novak.pavel31-May-06 3:08 novak.pavel 31-May-06 3:08
 Dijsktra's algorithm vit.ruzicka26-May-06 6:01 vit.ruzicka 26-May-06 6:01
 Re: Dijsktra's algorithm warnockm20-Nov-07 16:05 warnockm 20-Nov-07 16:05
 Remove functionality bffgdggd6-Jan-06 4:59 bffgdggd 6-Jan-06 4:59
 License for Priority Queue tosboy21-Dec-05 0:40 tosboy 21-Dec-05 0:40
 confused re: updating vmy1-Jun-04 17:23 vmy 1-Jun-04 17:23
 Re: confused re: updating BenDi3-Jun-04 2:34 BenDi 3-Jun-04 2:34
 Re: confused re: updating james kolpack12-Jan-06 7:35 james kolpack 12-Jan-06 7:35
 I also had a need to do this, so I derived a class which handles it. It does this by using a key-value lookup (in mObjectIndex) and updating this after any operation on the queue. Note: this means changing the base class by 1) making sure all the methods this overriding are virtual and 2) renaming Update to UpdateIndex. Also, one of the constructors from the original is missing. It definitely will take a performance hit to do these operations, but if you need it, here it is. Kudos to BenDi for posting the base class. /// /// Adds update functionality to objects in the priority queue /// public class BinaryPriorityQueueWithUpdate : BinaryPriorityQueue { private Hashtable mObjectIndex; /// /// Determines whether the specified BinaryPriorityQueue contains object. /// /// The obj. public bool ContainsObject(object obj) { return mObjectIndex.Contains(obj); } #region contructors /// /// Initializes a new instance of the class. /// public BinaryPriorityQueueWithUpdate() : base() { mObjectIndex = new Hashtable(); } /// /// Initializes a new instance of the class. /// /// The comparer. public BinaryPriorityQueueWithUpdate(IComparer comparer) : base(comparer) { mObjectIndex = new Hashtable(); } /// /// Initializes a new instance of the class. /// /// The capacity. public BinaryPriorityQueueWithUpdate(int capacity) : base(capacity) { mObjectIndex = new Hashtable(capacity); } /// /// Initializes a new instance of the class. /// /// The comparer. /// The capacity. public BinaryPriorityQueueWithUpdate(IComparer comparer, int capacity) : base(comparer, capacity) { mObjectIndex = new Hashtable(capacity); } #endregion /// /// Get the smallest object and remove it. /// /// The smallest object public override object Pop() { mObjectIndex.Remove(InnerList[0]); if (InnerList.Count > 0) { mObjectIndex.Remove(InnerList[InnerList.Count-1]); mObjectIndex.Add(InnerList[InnerList.Count-1], 0); } return base.Pop(); } /// /// Push an object onto the PQ /// /// The new object /// /// The index in the list where the object is _now_. This will change when objects are taken from or put onto the PQ. /// public override int Push(object obj) { mObjectIndex.Add(obj, InnerList.Count); return base.Push(obj); } /// /// Switches the elements. /// /// The i. /// The j. protected override void SwitchElements(int i, int j) { object iObj = this.InnerList[i]; object jObj = this.InnerList[j]; mObjectIndex.Remove(iObj); mObjectIndex.Remove(jObj); mObjectIndex.Add(iObj, j); mObjectIndex.Add(jObj, i); base.SwitchElements (i, j); } /// /// Updates the specified obj. /// /// The obj. /// The new obj. public void Update(object obj, object newObj) { int index = (int) mObjectIndex[obj]; mObjectIndex.Remove(obj); mObjectIndex.Add(newObj, index); InnerList[index] = newObj; base.UpdateIndex(index); } /// /// Updates the specified obj. /// /// The obj. public void Update(object obj) { int index = (int) mObjectIndex[obj]; base.UpdateIndex(index); } }
 Thanks!! M.Shoaib Khan9-Apr-04 20:21 M.Shoaib Khan 9-Apr-04 20:21
 What is wrong with Queue? sandeman2-Apr-04 10:16 sandeman 2-Apr-04 10:16
 Re: What is wrong with Queue? BenDi2-Apr-04 10:36 BenDi 2-Apr-04 10:36
 Re: What is wrong with Queue? BenDi2-Apr-04 10:38 BenDi 2-Apr-04 10:38
 Re: What is wrong with Queue? sandeman2-Apr-04 15:00 sandeman 2-Apr-04 15:00
 Re: What is wrong with Queue? BenDi2-Apr-04 22:57 BenDi 2-Apr-04 22:57
 Your PriorityQueue code converted to Visual Basic Enigmativity30-Mar-04 23:51 Enigmativity 30-Mar-04 23:51
 Re: Your PriorityQueue code converted to Visual Basic BenDi31-Mar-04 1:14 BenDi 31-Mar-04 1:14
 Care to join a "all collection" project ? Jonathan de Halleux30-Mar-04 3:20 Jonathan de Halleux 30-Mar-04 3:20
 Re: Care to join a "all collection" project ? Jonathan de Halleux30-Mar-04 3:31 Jonathan de Halleux 30-Mar-04 3:31
 Re: Care to join a "all collection" project ? BenDi30-Mar-04 4:16 BenDi 30-Mar-04 4:16
 Re: Care to join a "all collection" project ? Jonathan de Halleux30-Mar-04 4:27 Jonathan de Halleux 30-Mar-04 4:27
 Re: Care to join a "all collection" project ? BenDi2-Apr-04 23:45 BenDi 2-Apr-04 23:45
 Re: Care to join a "all collection" project ? Jonathan de Halleux3-Apr-04 1:41 Jonathan de Halleux 3-Apr-04 1:41
 Re: Care to join a "all collection" project ? Jonathan de Halleux3-Apr-04 8:51 Jonathan de Halleux 3-Apr-04 8:51
 Re: Care to join a "all collection" project ? Jonathan de Halleux2-Apr-04 4:46 Jonathan de Halleux 2-Apr-04 4:46
 Which heap ? Jonathan de Halleux29-Mar-04 12:15 Jonathan de Halleux 29-Mar-04 12:15
 Re: Which heap ? BenDi30-Mar-04 3:55 BenDi 30-Mar-04 3:55
 Re: Which heap ? Jonathan de Halleux30-Mar-04 4:00 Jonathan de Halleux 30-Mar-04 4:00
 Re: Which heap ? BenDi30-Mar-04 4:24 BenDi 30-Mar-04 4:24
 Last Visit: 31-Dec-99 19:00     Last Update: 4-May-15 13:02 Refresh 1