13,044,705 members (45,968 online)
alternative version

#### Stats

184.7K views
55 bookmarked
Posted 28 Mar 2004

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

 First PrevNext
 Danka !!! :) Samedi Ishmael Medivh25-Oct-14 10:53 Samedi Ishmael Medivh 25-Oct-14 10:53
 Thanks a lot majdaldin122-Apr-11 2:59 majdaldin1 22-Apr-11 2:59
 My vote of 3 LassarRRRRRR17-Feb-11 5:01 LassarRRRRRR 17-Feb-11 5:01
 My vote of 4 s.hamed_rezvani12-Jan-11 3:48 s.hamed_rezvani 12-Jan-11 3:48
 Generic version Olmo del Corral12-Mar-09 11:01 Olmo del Corral 12-Mar-09 11:01
 Updated for non duplicate value in list. bestwasin2-Feb-09 5:26 bestwasin 2-Feb-09 5:26
 First of all thanks to BenDi for your great PQ. Only one feature that I want more is 'allow no duplicated value in list', so I decide to extend his code. What I have done is just to perform the binary search before Push() any object in the list, just to check that it is already there or not. If it's there then we do nothing. I choose binary search because this will limit the big-o to O(log N), so it will be like almost the same complexity with PQ. (I'm not sure whether Contains() method in ArrayList() is perform in linear or binary so I do it for myself here.) This is modified Push() method /// If the object is already existed in the list then return -99. public int Push(object O) { int p = InnerList.Count, p2; if (CheckValueInList(O) == -1) InnerList.Add(O); // E[p] = O else return -99; //return, we dont do anything do { if (p == 0) break; p2 = (p - 1) / 2; if (OnCompare(p, p2) < 0) { SwitchElements(p, p2); p = p2; } else break; }while (true); return p; } And the binary search method is here // Check the existance of the specified object in the list, // if it's exist then return the index of the existed object, otherwise return -1 int CheckValueInList(Object O) { //if no element if (InnerList.Count == 0) return -1;   //binary search int low = 0; int high = InnerList.Count - 1; int mid; while (low <= high) { mid = low + (high - low) / 2; if (Comparer.Compare(InnerList[mid], O) < 0) high = mid - 1; else if (Comparer.Compare(InnerList[mid], O) > 0) low = mid + 1; else return mid; // found } return -1; } Any suggest is welcomed.
 implemntinig priority Quesues Member 331791728-Aug-08 7:42 Member 3317917 28-Aug-08 7:42
 implemntinig priority Quesues Member 331791728-Aug-08 7:35 Member 3317917 28-Aug-08 7:35
 Fast Priority Queue Implementation of Dijkstra Shortest Path Algorithm Tolga Birdal31-Mar-08 5:38 Tolga Birdal 31-Mar-08 5:38
 remove method belal_haija22-Mar-08 15:41 belal_haija 22-Mar-08 15:41
 Last Visit: 31-Dec-99 18:00     Last Update: 21-Jul-17 21:55 Refresh 12345 Next »