Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Looking for something faster than PriorityQueue

i'm using java on a big amount of data.

[i try to simplify the problem as much as possible]

Actually i have a small class (Element) containing an int KEY and a double WEIGHT (with getters&setters).

I read a lot of these objects from a file and i have to get the best (most weight) M objects.

Actually i'm using a PriorityQueue with a Comparator written to compare two Element, and it works, but it's too slow.

Do you know (i know you do) any faster way to do that?

Thank you

like image 946
BigG Avatar asked Aug 31 '09 19:08

BigG


1 Answers

A heap-based priority queue is a good data structure for this problem. Just as a sanity check, verify that you are using the queue correctly.

If you want the highest weight items, use a min-queue—where the top of the heap is the smallest item. Adding every item to a max-queue and examining the top M items when done is not efficient.

For each item, if there are less than M items in the queue, add the current item. Otherwise, peek at the top of the heap. If it's less than the current item, discard it, and add the current item instead. Otherwise, discard the current item. When all items have been processed, the queue will contain the M highest-weight items.

Some heaps have shortcut APIs for replacing the top of the heap, but Java's Queue does not. Even so, the big-O complexity is the same.

like image 129
erickson Avatar answered Sep 30 '22 13:09

erickson