没有合适的资源?快使用搜索试试~ 我知道了~
20151910042-刘鹏-DSA实验09-优先队列结构实验1
需积分: 0 0 下载量 198 浏览量
2022-08-08
18:54:06
上传
评论
收藏 3.91MB DOCX 举报
温馨提示
试读
25页
四、实验记录与实验结果分析1题程序代码:五、教材翻译TranslationChapter 9 Priority Queue*第九章 优先队列9.1 The Pr
资源详情
资源评论
资源推荐
云南大学数学与统计学院数学系信息与计算科学专业
第 1 页 共 25 页
云南大学数学与统计学院
上机实践报告
课程名称:数据结构与算法实验
年级:2015 级
上机实践成绩:
指导教师:陆正福
姓名:刘鹏
上机实践名称:高级语言面向对象编程实验
学号:20151910042
上机实践日期:2017-06-25
上机实践编号:No.09
组号:
上机实践时间:上午 3、4 节
一、实验目的
1. 熟悉与优先队列有关的数据结构与算法
2. 熟悉主讲教材 Chapter 9 的代码片段
二、实验内容
1. 优先队列有关的数据结构设计与算法设计
2. 调试主讲教材 Chapter 9 的 Python 程序
三、实验平台
Windows 10 1703 Enterprise 中文版;
Python 3.6.0;
Wing IDE Professional 6.0.5-1 集成开发环境。
四、实验记录与实验结果分析
1 题
程序代码:
云南大学数学与统计学院数学系信息与计算科学专业
第 2 页 共 25 页
五、教材翻译
Translation
Chapter 9 Priority Queue
*第九章 优先队列
9.1 The Priority Queue Abstract Data Type
*9.1 节 优先队列抽象数据结构
9.1.1 Priorities
*优先级
In Chapter 6, we introduced the queue ADT as a collection of
objects that are added and removed according to the first-in,
first-out (FIFO) principle. A company’s customer call center
embodies such a model in which waiting customers are told
“calls will be answered in the order that they were received.” In
that setting, a new call is added to the back of the queue, and
each time a customer service representative becomes available,
he or she is connected with the call that is removed from the front
of the call queue.
*在第 6 章中,我们介绍了先进先出的队列 ADT。公司的
客户呼叫中心体现了这样一种模式,其中等待客户被告知
“ 呼叫将按照接收到的顺序进行回复” 。在该设置中,新呼
叫被添加到队列的后面,每次客户服务变得可用时,队列
最前面的等待者也将从呼叫队列的前面移除。
In practice, there are many applications in which a queue-
like structure is used to manage objects that must be processed
in some way, but for which the first-in, first-out policy does not
suffice. Consider, for example, an air-traffic control center that
has to decide which flight to clear for landing from among many
approaching the airport. This choice may be influenced by
factors such as each plane’s distance from the runway, time
spent waiting in a holding pattern, or amount of remaining fuel.
It is unlikely that the landing decisions are based purely on a
FIFO policy.
*实际上,有许多应用程序使用队列式结构来管理对象,
但是有的时候先进先出的策略并不太合适。例如,一个空
中交通管制中心,必须决定在许多接近机场的飞机中,哪
些飞机应该最先着陆。这种选择可能受到诸如每个飞机与
跑道的距离,等待在持有模式中花费的时间或剩余燃料量
等因素的影响。着陆决定不太可能纯粹基于 FIFO 策略。
There are other situations in which a “first come, first serve”
policy might seem reasonable, yet for which other priorities
come into play. To use another airline analogy, suppose a certain
flight is fully booked an hour prior to departure. Be- cause of the
possibility of cancellations, the airline maintains a queue of
standby passengers hoping to get a seat. Although the priority of
a standby passenger is influenced by the check-in time of that
passenger, other considerations include the fare paid and
frequent-flyer status. So it may be that an available seat is given
to a passenger who has arrived later than another, if such a
passenger is assigned a better priority by the airline agent.
*
In this chapter, we introduce a new abstract data type known
as a priority queue. This is a collection of prioritized elements
that allows arbitrary element insertion, and allows the removal
of the element that has first priority. When an element is added
to a priority queue, the user designates its priority by providing
an associated key. The element with the minimum key will be the
next to be removed from the queue (thus, an element with key 1
will be given priority over an element with key 2). Although it
is quite common for priorities to be expressed numerically, any
Python object may be used as a key, as long as the object type
supports a consistent meaning for the test a < b, for any instances
a and b, so as to define a natural order of the keys. With such
generality, applications may develop their own notion of priority
for each element. For example, different financial analysts may
assign different ratings (i.e., priorities) to a particular asset, such
as a share of stock.
*
云南大学数学与统计学院数学系信息与计算科学专业
第 3 页 共 25 页
9.1.2 The Priority Queue ADT
Formally, we model an element and its priority as a key-value
pair. We define the priority queue ADT to support the following
methods for a priority queue P:
P.add(k, v):
Insert an item with key k and value v into
priority queue P.
P.min():
Return a tuple, (k,v), representing the key
and value of an item in priority queue P
with minimum key (but do not re- move
the item); an error occurs if the priority
queue is empty.
P.remove_min():
Remove an item with minimum key from
priority queue P, and return a tuple, (k,v),
representing the key and value of the
removed item; an error occurs if the
priority queue is empty.
P.is_empty( ):
Return True if priority queue P does not
contain any items.
len(P):
Return the number of items in priority
queue P.
A priority queue may have multiple entries with equivalent keys,
in which case methods min and remove min may report an
arbitrary choice of item having mini- mum key. Values may be
any type of object.
In our initial model for a priority queue, we assume that an
element’s key re- mains fixed once it has been added to a priority
queue. In Section 9.5, we consider an extension that allows a
user to update an element’s key within the priority queue.
Example 9.1: The following table shows a series of operations
and their effects on an initially empty priority queue P. The
“Priority Queue” column is somewhat deceiving since it shows
the entries as tuples and sorted by key. Such an internal
representation is not required of a priority queue.
云南大学数学与统计学院数学系信息与计算科学专业
第 4 页 共 25 页
9.2 Implementing a Priority Queue
In this section, we show how to implement a priority queue by
storing its entries in a positional list L. (See Section 7.4.) We
provide two realizations, depending on whether or not we keep
the entries in L sorted by key.
9.2.1 The Composition Design Pattern
One challenge in implementing a priority queue is that we must
keep track of both an element and its key, even as items are
relocated within our data structure. This is reminiscent of a case
study from Section 7.6 in which we maintain access counts with
each element. In that setting, we introduced the composition
design pattern, defining an Item class that assured that each
element remained paired with its associated count in our primary
data structure.
For priority queues, we will use composition to store items
internally as pairs consisting of a key k and a value v. To
implement this concept for all priority queue implementations,
we provide a PriorityQueueBase class (see Code Fragment 9.1)
that includes a definition for a nested class named Item. We
define the syntax a < b, for item instances a and b, to be based
upon the keys.
云南大学数学与统计学院数学系信息与计算科学专业
第 5 页 共 25 页
9.2.2 Implementation with an Unsorted List
In our first concrete implementation of a priority queue, we store
entries within an unsorted list. Our UnsortedPriorityQueue class
is given in Code Fragment 9.2, inheriting from the
PriorityQueueBase class introduced in Code Fragment 9.1. For
internal storage, key-value pairs are represented as composites,
using instances of the inherited Item class. These items are
stored within a PositionalList, identified as the data member of
our class. We assume that the positional list is implemented with
a doubly-linked list, as in Section 7.4, so that all operations of
that ADT execute in O(1) time.
We begin with an empty list when a new priority queue is
constructed. At all times, the size of the list equals the number
of key-value pairs currently stored in the priority queue. For this
reason, our priority queue len method simply returns the length
of the internal data list. By the design of our PriorityQueueBase
class, we inherit a concrete implementation of the is empty
method that relies on a call to our len method.
Each time a key-value pair is added to the priority queue, via the
add method, we create a new Item composite for the given key
and value, and add that item to the end of the list. Such an
implementation takes O(1) time.
The remaining challenge is that when min or remove min is
called, we must locate the item with minimum key. Because the
items are not sorted, we must inspect all entries to find one with
a minimum key. For convenience, we define a nonpublic find
min utility that returns the position of an item with minimum key.
Knowledge of the position allows the remove min method to
invoke the delete method on the positional list. The min method
simply uses the position to retrieve the item when preparing a
key-value tuple to return. Due to the loop for finding the
minimum key, both min and remove min methods run in O(n)
time, where n is the number of entries in the priority queue.
A summary of the running times for the UnsortedPriorityQueue
class is given in Table 9.1.
剩余24页未读,继续阅读
明儿去打球
- 粉丝: 15
- 资源: 327
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0