473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Priority Queue

Hello,
I have implemented a C# priority queue using an ArrayList. The objects
being inserted into the priority queue are being sorted by 2 fields, Time
(ulong) and Priority (0-100).

When I enqueue, I do a binary search on where to put the object and then
insert using that index and arraylist.insert(index, obj)

The bottom of my arraylist is always my lowest, therefore, dequeueing is
very fast and effecient.

Removing is done using a binary search as well and calling the
arraylist.remove.

I need to optimize my priorityqueue implementatation further. Here are a
couple of questions I have:

1. Would it be faster to just use C# array instead of ArrayList?
2. Anyone else have some C# priority queue approaches they would be willing
to share/discuss?

Thanks in advanced,
Dan

Nov 15 '05 #1
5 13181
Dan,
Its been a while since I played with Priority Queues.

Would not a System.Collections.SortedList of System.Collections.Queue be
easier?

Each priority is an item in the SortedList, each item is a Queue.

I would key the SortedList by the priority, allowing easy retrieval &
indexing, while the Queue objects would take care of themselves.

I would encapsulate both in objects in a class or two to hide the actual
implementation details.

To add Items, I would look for the priority by key in the SortedList,
returning the queue, I would then add the item to the end of the queue. If
the item is not found I would create a new queue adding it to the
SortedList.

To remove items, the first ordinal position (element 0) in the SortedList is
the first queue, I would just dequeue the item. If the queue is now empty, I
would remove it from the SortedList.

Of course it may have been TOO long since I've used Priority Queues ;-)

The 'problem' is going to be if the Priority is an Integer, as you will need
to be specific to let the SortedList know when you are indexing by key or by
ordinal position.

Hope this helps
Jay
"Dan H." <Da**@projectavatar.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello,
I have implemented a C# priority queue using an ArrayList. The objects
being inserted into the priority queue are being sorted by 2 fields, Time
(ulong) and Priority (0-100).

When I enqueue, I do a binary search on where to put the object and then
insert using that index and arraylist.insert(index, obj)

The bottom of my arraylist is always my lowest, therefore, dequeueing is
very fast and effecient.

Removing is done using a binary search as well and calling the
arraylist.remove.

I need to optimize my priorityqueue implementatation further. Here are a
couple of questions I have:

1. Would it be faster to just use C# array instead of ArrayList?
2. Anyone else have some C# priority queue approaches they would be willi ng to share/discuss?

Thanks in advanced,
Dan

Nov 15 '05 #2
Thinking out loud...
Although you save using the binary search, you pay a couple different taxes
here. You pay on the overhead of both binary searches, you pay on
ArrayList.Insert() and RemoveAt() for every call as internally they do an
Array.Copy to adjust the internal array. Insert() will also allocate (by
2x) a new array and do an Array.Copy if it needs to grow.

So this may be faster, but would need to be perf tested.
Option1-
Use an ArrayList for you internal queue. Enqueue (and dequeue) your objects
using a struct or Class that you create that holds the priority, time, and
object fields. Your IComparer can be in this class. Enqueue the object and
sort the array using your IComparer in reverse and adjust your Enqueue and
Dequeue methods accordingly. Use reverse as removing the last element of an
arraylist will avoid the last Array.Copy and just set the last element to
null.

Option2-
Use object[] as your internal array for queue. Setup your queue as a
circular queue with head, tail, etc. If your producer's and consumer's will
be mostly even, then you can use a fixed length array to avoid growing, then
just sync your queue with wait()'s on gets and puts, etc. In a
producer/consumer, this is probably the way you want to go anyway, as your
producer will want to wait for more input on an empty queue (or a closed
flag) and not just return empty. Every Enqueue() will add the element and
sort the array ascending with your IComparer so your head element will
always be the next one for the next Dequeue(). As this is circular, you
don't need to copy the array or reallocate it in either case - just move the
head or tail ptr in your enqueue and dequeue (very fast). If you do want or
need to grow the array, just do it by a default or user supplied growth
factor during Enqueue(). So the only tax you pay here is the Sort in
Enqueue. And if your queue is small, this will be fast and maybe faster
then a binary search depending on the size. Overall, I would guess this
will be faster then the two binary searches, the allocations and the
Array.Copy's using the ArrayList method. The downside is, they both need to
be built and tested to discern the speed difference.

--
William Stacey, MVP

"Dan H." <Da**@projectavatar.com> wrote in message
news:#N**************@TK2MSFTNGP12.phx.gbl...
Hello,
I have implemented a C# priority queue using an ArrayList. The objects
being inserted into the priority queue are being sorted by 2 fields, Time
(ulong) and Priority (0-100).

When I enqueue, I do a binary search on where to put the object and then
insert using that index and arraylist.insert(index, obj)

The bottom of my arraylist is always my lowest, therefore, dequeueing is
very fast and effecient.

Removing is done using a binary search as well and calling the
arraylist.remove.

I need to optimize my priorityqueue implementatation further. Here are a
couple of questions I have:

1. Would it be faster to just use C# array instead of ArrayList?
2. Anyone else have some C# priority queue approaches they would be willing to share/discuss?

Thanks in advanced,
Dan

Nov 15 '05 #3
> Option2-
Use object[] as your internal array for queue. Setup your queue as a
circular queue with head, tail, etc. If your producer's and consumer's will be mostly even, then you can use a fixed length array to avoid growing,

then
....

There is another optimization here that I just thought about. Put the sort
logic in the Dequeue, not the Enqueue and use a "isSorted" flag. This
allows you to leverage the sort after you add your last element (Enqueue)
and with multiple Enqueues in a row (with not dequeue's in between.) So if
you add 10 elements and then start the dequeue, you only sort once and the
dequeues just grab the next element - no sort needed after the first. Set
the flag back to false after each Enqueue. hth

--
William Stacey, MVP
Nov 15 '05 #4
Thanks for the replies. I am digesting them now, but I just wanted to say
thanks for the info. Hopefully I can put together a small test harness to
test speeds soon.

Thanks,

dan
"Dan H." <Da**@projectavatar.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello,
I have implemented a C# priority queue using an ArrayList. The objects
being inserted into the priority queue are being sorted by 2 fields, Time
(ulong) and Priority (0-100).

When I enqueue, I do a binary search on where to put the object and then
insert using that index and arraylist.insert(index, obj)

The bottom of my arraylist is always my lowest, therefore, dequeueing is
very fast and effecient.

Removing is done using a binary search as well and calling the
arraylist.remove.

I need to optimize my priorityqueue implementatation further. Here are a
couple of questions I have:

1. Would it be faster to just use C# array instead of ArrayList?
2. Anyone else have some C# priority queue approaches they would be willing to share/discuss?

Thanks in advanced,
Dan

Nov 15 '05 #5

hi Jay,
that is not a valid approach. Keys in SortedList are expected to be
unique, while priorities are not. Many problems would arise from this,
and their resolving would probably require more effort than Dan's
ArrayList approach.
--GC

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Aaron W. LaFramboise | last post by:
Hello, I understand that an easy way to make the standard std::priority_queue stable is by including an integer stamp with each node that is incremented each time a new node is pushed into the...
3
by: Erik | last post by:
Hi Everyone, I was thinking of how to do this for a while now and I cant get my head round it so I though I'd ask the experts! :-) What I am doing is reading in large amounts of data over TCP...
16
by: Crirus | last post by:
hello I read somewhere about priority queue...what is that and Vb net have such class? -- Ceers, Crirus ------------------------------ If work were a good thing, the boss would take it...
4
by: vfunc | last post by:
Is the STL priority queue a proper implementation of a heap with siftup algorithm etc ? How do you implement an STL priority queue (link to an example) ? Is there a resort method, why ? Thanks
3
by: Chris Lasher | last post by:
Hello, I am working with large graphs (~150,000 to 500,000 nodes) which I need decompose node-by-node, in order of a node's value. A node's value is determined by the sum of its edge weights....
3
by: PicO | last post by:
i need some explanation about the difference between priority queue & set & heap ... as they all sort the data in ( n log n ) ... but the only i see that priority queue only can pop the top (...
1
by: operatingsystem | last post by:
I need to generate such scheduler in operating system subject which satisfy below conditions...may i request u if anybody knows abt this plz help me out in this..topics.. Scheduler...
4
by: jjh5030 | last post by:
This is a programming assignment. You are asked to work with pointers. Be aware that error messages are often not very helpful when your pointers point to bad locations. Therefore, reserve...
14
by: AlarV | last post by:
Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.