Click here to Skip to main content
12,405,700 members (62,565 online)
Click here to Skip to main content
Add your own
alternative version


55 bookmarked

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.


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.


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;))


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

IPriorityQueue PQ = new BinaryPriorityQueue();
Random R = new Random();
int i;


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

    Created IPriorityQueue interface.


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


About the Author

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

GeneralDanka !!! :) Pin
Samedi Ishmael Medivh25-Oct-14 10:53
memberSamedi Ishmael Medivh25-Oct-14 10:53 
GeneralThanks a lot Pin
majdaldin122-Apr-11 2:59
membermajdaldin122-Apr-11 2:59 
GeneralMy vote of 3 Pin
LassarRRRRRR17-Feb-11 5:01
memberLassarRRRRRR17-Feb-11 5:01 
GeneralMy vote of 4 Pin
s.hamed_rezvani12-Jan-11 3:48
members.hamed_rezvani12-Jan-11 3:48 
GeneralGeneric version Pin
Olmo del Corral12-Mar-09 11:01
memberOlmo del Corral12-Mar-09 11:01 
GeneralUpdated for non duplicate value in list. Pin
bestwasin2-Feb-09 5:26
memberbestwasin2-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
		return -99;  //return, we dont do anything
		if (p == 0)
		p2 = (p - 1) / 2;
		if (OnCompare(p, p2) < 0)
			SwitchElements(p, p2);
			p = p2;
	}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;
            return mid; // found
    return -1;

Any suggest is welcomed. Smile | :)
Questionimplemntinig priority Quesues Pin
Member 331791728-Aug-08 7:42
memberMember 331791728-Aug-08 7:42 
Questionimplemntinig priority Quesues Pin
Member 331791728-Aug-08 7:35
memberMember 331791728-Aug-08 7:35 
NewsFast Priority Queue Implementation of Dijkstra Shortest Path Algorithm Pin
Tolga Birdal31-Mar-08 5:38
memberTolga Birdal31-Mar-08 5:38 
Questionremove method Pin
belal_haija22-Mar-08 15:41
memberbelal_haija22-Mar-08 15:41 
Questionhashtable Pin
warnockm20-Nov-07 15:03
memberwarnockm20-Nov-07 15:03 
GeneralCorrection Pin
David Krantz14-Dec-06 23:49
memberDavid Krantz14-Dec-06 23:49 
GeneralA-Star Implementation Pin
CastorTiu22-Sep-06 21:02
memberCastorTiu22-Sep-06 21:02 
GeneralTake care while using multiple threads Pin
novak.pavel31-May-06 2:08
membernovak.pavel31-May-06 2:08 
QuestionDijsktra's algorithm Pin
vit.ruzicka26-May-06 5:01
membervit.ruzicka26-May-06 5:01 
GeneralRe: Dijsktra's algorithm Pin
warnockm20-Nov-07 15:05
memberwarnockm20-Nov-07 15:05 
GeneralRemove functionality Pin
bffgdggd6-Jan-06 3:59
memberbffgdggd6-Jan-06 3:59 
GeneralLicense for Priority Queue Pin
tosboy20-Dec-05 23:40
membertosboy20-Dec-05 23:40 
Generalconfused re: updating Pin
vmy1-Jun-04 16:23
membervmy1-Jun-04 16:23 
GeneralRe: confused re: updating Pin
BenDi3-Jun-04 1:34
memberBenDi3-Jun-04 1:34 
GeneralRe: confused re: updating Pin
james kolpack12-Jan-06 6:35
memberjames kolpack12-Jan-06 6:35 
GeneralThanks!! Pin
M.Shoaib Khan9-Apr-04 19:21
memberM.Shoaib Khan9-Apr-04 19:21 
QuestionWhat is wrong with Queue? Pin
sandeman2-Apr-04 9:16
membersandeman2-Apr-04 9:16 
AnswerRe: What is wrong with Queue? Pin
BenDi2-Apr-04 9:36
memberBenDi2-Apr-04 9:36 
GeneralRe: What is wrong with Queue? Pin
BenDi2-Apr-04 9:38
memberBenDi2-Apr-04 9:38 
GeneralRe: What is wrong with Queue? Pin
sandeman2-Apr-04 14:00
membersandeman2-Apr-04 14:00 
GeneralRe: What is wrong with Queue? Pin
BenDi2-Apr-04 21:57
memberBenDi2-Apr-04 21:57 
GeneralYour PriorityQueue code converted to Visual Basic Pin
Enigmativity30-Mar-04 22:51
memberEnigmativity30-Mar-04 22:51 
GeneralRe: Your PriorityQueue code converted to Visual Basic Pin
BenDi31-Mar-04 0:14
memberBenDi31-Mar-04 0:14 
QuestionCare to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux30-Mar-04 2:20
memberJonathan de Halleux30-Mar-04 2:20 
AnswerRe: Care to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux30-Mar-04 2:31
memberJonathan de Halleux30-Mar-04 2:31 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
BenDi30-Mar-04 3:16
memberBenDi30-Mar-04 3:16 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux30-Mar-04 3:27
memberJonathan de Halleux30-Mar-04 3:27 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
BenDi2-Apr-04 22:45
memberBenDi2-Apr-04 22:45 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux3-Apr-04 0:41
memberJonathan de Halleux3-Apr-04 0:41 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux3-Apr-04 7:51
memberJonathan de Halleux3-Apr-04 7:51 
GeneralRe: Care to join a &quot;all collection&quot; project ? Pin
Jonathan de Halleux2-Apr-04 3:46
memberJonathan de Halleux2-Apr-04 3:46 
QuestionWhich heap ? Pin
Jonathan de Halleux29-Mar-04 11:15
memberJonathan de Halleux29-Mar-04 11:15 
AnswerRe: Which heap ? Pin
BenDi30-Mar-04 2:55
memberBenDi30-Mar-04 2:55 
GeneralRe: Which heap ? Pin
Jonathan de Halleux30-Mar-04 3:00
memberJonathan de Halleux30-Mar-04 3:00 
GeneralRe: Which heap ? Pin
BenDi30-Mar-04 3:24
memberBenDi30-Mar-04 3: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 29 Mar 2004
Article Copyright 2004 by BenDi
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid