Click here to Skip to main content
12,818,664 members (28,891 online)
Click here to Skip to main content
Add your own
alternative version

Stats

182.9K views
4.6K downloads
55 bookmarked
Posted 28 Mar 2004

Priority queue

, 28 Mar 2004
Rate this:
Please Sign up or sign in to vote.
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.

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

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
Questionhashtable Pin
warnockm20-Nov-07 16:03
memberwarnockm20-Nov-07 16:03 
GeneralCorrection Pin
David Krantz15-Dec-06 0:49
memberDavid Krantz15-Dec-06 0:49 
GeneralA-Star Implementation Pin
CastorTiu22-Sep-06 22:02
memberCastorTiu22-Sep-06 22:02 
GeneralTake care while using multiple threads Pin
novak.pavel31-May-06 3:08
membernovak.pavel31-May-06 3:08 
Hi,
I have taken a look at it and I think that Synchronized wrapper is not correctly implemented. You just use the inner ArrayList synchronized wrapper, but it is not enough. In many methods you access that list more than once, so single accesses are thread-safe, but the whole method not.

Hence, while using this queue from multiple threads use
lock (priorityQueue.SyncRoot)
{
  // accessing the priorityQueue
}


Pavel
QuestionDijsktra's algorithm Pin
vit.ruzicka26-May-06 6:01
membervit.ruzicka26-May-06 6:01 
GeneralRe: Dijsktra's algorithm Pin
warnockm20-Nov-07 16:05
memberwarnockm20-Nov-07 16:05 
GeneralRemove functionality Pin
bffgdggd6-Jan-06 4:59
memberbffgdggd6-Jan-06 4:59 
GeneralLicense for Priority Queue Pin
tosboy21-Dec-05 0:40
membertosboy21-Dec-05 0:40 
Generalconfused re: updating Pin
vmy1-Jun-04 17:23
membervmy1-Jun-04 17:23 
GeneralRe: confused re: updating Pin
BenDi3-Jun-04 2:34
memberBenDi3-Jun-04 2:34 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 29 Mar 2004
Article Copyright 2004 by BenDi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid