[NEWBIE] Priority Queue in Python
tim.one at home.com
Sun Feb 4 01:01:57 EST 2001
[David Boeren, timing priority queues]
Three of your Python priority queue guys have the extra burden (compared to
your Python insert_priority4 and your Java insert_priority) of mucking
around with an extra key_index argument. Stick to the same thing
Effort devoted to timing naive algorithms is better spent dreaming up better
algorithms. "The usual" naive priority queue in Python uses the std bisect
Then finding the place to insert is an O(log(len(list))) time operation,
although doing the actual insert remains O(len(list)) time, but where the
latter is at full C speed. While faster then doing a linear search, if your
priority queues are going to get large, this is a disastrous approach
regardless of language.
Note that Python compares tuples lexicographically (strings and lists too),
so even if the item is a tuple this works fine provided item is the
If your benchmarks are typical of your expected access pattern (i.e.,
nothing but inserts -- no lookups), or an approximation to your expected
access pattern (perhaps lots of inserts followed by lots of lookups), it
would be much faster to do the insert via
later before your first access. Python's list.sort() realizes if a list is
"sorted at the start, and then has 'some small amount of' disordered gunk at
the end", and in that case does a fast sequence of binary inserts under
covers to clean up the gunk. Yet this is still a disaster if priority
queues get very large.
A much better way to do priority queues if they can get large is via a heap.
Then insertion, deletion and lookup never take worse than O(log(n)) time.
Finally, note that the std module random has a random.shuffle function.
More information about the Python-list