12,699,898 members (36,274 online)
alternative version

182K views
55 bookmarked
Posted

# 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.

## You may also be interested in...

 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
 i'm working on doing the same thing. I'm trying to replace my distance table w/ this PQ to improve the time it takes to get the minimum distance.
 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
 Thanks!! M.Shoaib Khan9-Apr-04 20:21 M.Shoaib Khan 9-Apr-04 20:21
 Last Visit: 31-Dec-99 19:00     Last Update: 22-Jan-17 18:46 Refresh « Prev12345 Next »