我什么时候使用优先队列? [英] When would I use a priority queue?

查看:29
本文介绍了我什么时候使用优先队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所知道的使用优先级队列的唯一示例是 Dijkstra 算法(用于计算最小成本)

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?

推荐答案

这是一个实际的例子 - 用于业务应用程序:

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)

这篇关于我什么时候使用优先队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆