Click here to Skip to main content
15,848,349 members
Articles / Mobile Apps / Windows Mobile
Article

Priority queue

Rate me:
Please Sign up or sign in to vote.
4.50/5 (23 votes)
28 Mar 20041 min read 220.4K   5.1K   55   41
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

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Germany 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.

Comments and Discussions

 
GeneralRe: confused re: updating Pin
James Kolpack12-Jan-06 7:35
James Kolpack12-Jan-06 7:35 
GeneralThanks!! Pin
abc8769-Apr-04 20:21
abc8769-Apr-04 20:21 
QuestionWhat is wrong with Queue? Pin
sandeman2-Apr-04 10:16
sandeman2-Apr-04 10:16 
AnswerRe: What is wrong with Queue? Pin
BenDi2-Apr-04 10:36
BenDi2-Apr-04 10:36 
GeneralRe: What is wrong with Queue? Pin
BenDi2-Apr-04 10:38
BenDi2-Apr-04 10:38 
GeneralRe: What is wrong with Queue? Pin
sandeman2-Apr-04 15:00
sandeman2-Apr-04 15:00 
GeneralRe: What is wrong with Queue? Pin
BenDi2-Apr-04 22:57
BenDi2-Apr-04 22:57 
GeneralYour PriorityQueue code converted to Visual Basic Pin
Enigmativity30-Mar-04 23:51
Enigmativity30-Mar-04 23:51 
Hey, I hope you don't mind, but I converted your code to Visual Basic. I wanted to make sure I understood it properly. I'm still trying to learn C#.

I've blogged about it.

http://www.enigmativity.com/blog/PermaLink.aspx?guid=3abe5bc8-d9bc-4cd3-a58a-3c1bf9b2d3ee


James R-S.
http://www.enigmativity.com/blog
GeneralRe: Your PriorityQueue code converted to Visual Basic Pin
BenDi31-Mar-04 1:14
BenDi31-Mar-04 1:14 
QuestionCare to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux30-Mar-04 3:20
Jonathan de Halleux30-Mar-04 3:20 
AnswerRe: Care to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux30-Mar-04 3:31
Jonathan de Halleux30-Mar-04 3:31 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
BenDi30-Mar-04 4:16
BenDi30-Mar-04 4:16 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux30-Mar-04 4:27
Jonathan de Halleux30-Mar-04 4:27 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
BenDi2-Apr-04 23:45
BenDi2-Apr-04 23:45 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux3-Apr-04 1:41
Jonathan de Halleux3-Apr-04 1:41 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux3-Apr-04 8:51
Jonathan de Halleux3-Apr-04 8:51 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux2-Apr-04 4:46
Jonathan de Halleux2-Apr-04 4:46 
QuestionWhich heap ? Pin
Jonathan de Halleux29-Mar-04 12:15
Jonathan de Halleux29-Mar-04 12:15 
AnswerRe: Which heap ? Pin
BenDi30-Mar-04 3:55
BenDi30-Mar-04 3:55 
GeneralRe: Which heap ? Pin
Jonathan de Halleux30-Mar-04 4:00
Jonathan de Halleux30-Mar-04 4:00 
GeneralRe: Which heap ? Pin
BenDi30-Mar-04 4:24
BenDi30-Mar-04 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.