When would I use a priority queue?

Oleksiy picture Oleksiy · Aug 5, 2013 · Viewed 18.9k times · Source

The only example of using the priority queue I know of, is the Dijkstra's Algorithm (for calculating minimum cost)

In what other situations would it be useful?

Answer

Vaughan Hilts picture Vaughan Hilts · Aug 5, 2013

Here's a practical example - for a business application:

You're running a hospital and patients are coming in. There's only one doctor on staff. The first man walks in - and he's served immediately. Next, a man with a cold comes in and requires assistance. You add him to the queue and he waits in line for the doctor to become available. Next, a man with an axe in his head comes through the door. He is assigned a higher priority because he has a higher medical liability. So the man with the cold is bumped down in line. Next, someone comes in with breathing problems. So, once again, the man with the cold is bumped down in priority. This is called triaging in the real world - but in this case it's a medical line.

Implementing this in code would use a priority queue and a worker thread (the doctor) to perform work on the consumable / units of work (the patients)