heapq
— Heap queue algorithm堆队列算法¶
Source code: Lib/heapq.py
This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.此模块提供堆队列算法的实现,也称为优先级队列算法。
Heaps are binary trees for which every parent node has a value less than or equal to any of its children. 堆是二叉树,其中每个父节点的值都小于或等于其任何子节点。This implementation uses arrays for which 该实现使用数组,其中heap[k] <= heap[2*k+1]
and heap[k] <= heap[2*k+2]
for all k, counting elements from zero. heap[k] <= heap[2*k+1]
和heap[k] <= heap[2*k+2]
用于所有k,从零开始计数元素。For the sake of comparison, non-existing elements are considered to be infinite. 为了便于比较,不存在的元素被认为是无限的。The interesting property of a heap is that its smallest element is always the root, 堆的有趣特性是,它的最小元素始终是根,即heap[0]
.heap[0]
。
The API below differs from textbook heap algorithms in two aspects: (a) We use zero-based indexing. 下面的API在两个方面不同于教科书中的堆算法:(a)我们使用基于零的索引。This makes the relationship between the index for a node and the indexes for its children slightly less obvious, but is more suitable since Python uses zero-based indexing. 这使得节点的索引与其子节点的索引之间的关系稍微不那么明显,但更适合,因为Python使用基于零的索引。(b) Our pop method returns the smallest item, not the largest (called a “min heap” in textbooks; a “max heap” is more common in texts because of its suitability for in-place sorting).(b) pop方法返回最小的项,而不是最大的项(在教科书中称为“最小堆”,“最大堆”在文本中更常见,因为它适合就地排序)。
These two make it possible to view the heap as a regular Python list without surprises: 这两个选项使我们可以将堆视为一个常规的Python列表而不会感到意外:heap[0]
is the smallest item, and heap.sort()
maintains the heap invariant!heap[0]
是最小的项,而heap.sort()
保持堆不变!
To create a heap, use a list initialized to 要创建堆,请使用初始化为[]
, or you can transform a populated list into a heap via function heapify()
.[]
的列表,或者可以通过函数heapify()
将填充的列表转换为堆。
The following functions are provided:提供以下功能:
-
heapq.
heappush
(heap, item)¶ Push the value item onto the heap, maintaining the heap invariant.将值item推送到heap上,保持堆不变。
-
heapq.
heappop
(heap)¶ Pop and return the smallest item from the heap, maintaining the heap invariant.弹出并返回heap中最小的项,保持堆不变。If the heap is empty,如果堆为空,则引发IndexError
is raised.IndexError
。To access the smallest item without popping it, use要访问最小的项而不弹出它,请使用heap[0]
.heap[0]
。
-
heapq.
heappushpop
(heap, item)¶ Push item on the heap, then pop and return the smallest item from the heap.将item推送到heap上,然后弹出并返回heap中最小的项。The combined action runs more efficiently than组合操作的运行效率高于heappush()
followed by a separate call toheappop()
.heappush()
,然后单独调用heappop()
。
-
heapq.
heapify
(x)¶ Transform list x into a heap, in-place, in linear time.在线性时间内就地将列表x转换为堆。
-
heapq.
heapreplace
(heap, item)¶ Pop and return the smallest item from the heap, and also push the new item.弹出并返回heap中最小的项,同时推送新item。The heap size doesn’t change.堆大小不变。If the heap is empty,如果堆为空,则引发IndexError
is raised.IndexError
。This one step operation is more efficient than a这种一步操作比heappop()
followed byheappush()
and can be more appropriate when using a fixed-size heap.heappop()
后跟heappush()
更有效,并且在使用固定大小的堆时更合适。The pop/push combination always returns an element from the heap and replaces it with item.pop/push组合总是从堆中返回一个元素,并将其替换为item。The value returned may be larger than the item added.返回的值可能大于添加的item。If that isn’t desired, consider using如果不希望这样,请考虑改用heappushpop()
instead.heappushpop()
。Its push/pop combination returns the smaller of the two values, leaving the larger value on the heap.它的push/pop组合返回两个值中较小的一个,将较大的值留在堆上。
The module also offers three general purpose functions based on heaps.该模块还提供了三个基于堆的通用函数。
-
heapq.
merge
(*iterables, key=None, reverse=False)¶ Merge multiple sorted inputs into a single sorted output (for example, merge timestamped entries from multiple log files).将多个排序输入合并到单个排序输出中(例如,合并多个日志文件中的时间戳条目)。Returns an iterator over the sorted values.返回排序值的迭代器。Similar to与sorted(itertools.chain(*iterables))
but returns an iterable, does not pull the data into memory all at once, and assumes that each of the input streams is already sorted (smallest to largest).sorted(itertools.chain(*iterables))
类似,但返回一个iterable,不会一次将数据拉入内存,并假设每个输入流都已排序(从最小到最大)。Has two optional arguments which must be specified as keyword arguments.有两个可选参数,必须指定为关键字参数。key specifies a key function of one argument that is used to extract a comparison key from each input element.key指定一个参数的键函数,用于从每个输入元素中提取比较键。The default value is默认值为None
(compare the elements directly).None
(直接比较元素)。reverse
is a boolean value.是布尔值。If set to如果设置为True
, then the input elements are merged as if each comparison were reversed.True
,则合并输入元素,就像每个比较都被反转一样。To achieve behavior similar to为了实现类似于sorted(itertools.chain(*iterables), reverse=True)
, all iterables must be sorted from largest to smallest.sorted(itertools.chain(*iterables), reverse=True)
的行为,所有可迭代对象必须从最大到最小排序。Changed in version 3.5:版本3.5中更改:Added the optional key and reverse parameters.添加了可选的key和reverse参数。
-
heapq.
nlargest
(n, iterable, key=None)¶ Return a list with the n largest elements from the dataset defined by iterable.从iterable定义的数据集中返回一个包含n个最大元素的列表。key, if provided, specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example,key(如果提供)指定一个参数的函数,用于从iterable中的每个元素中提取比较键(例如,key=str.lower
).key=str.lower
)。Equivalent to:等价于:sorted(iterable, key=key, reverse=True)[:n]
.sorted(iterable, key=key, reverse=True)[:n]
。
-
heapq.
nsmallest
(n, iterable, key=None)¶ Return a list with the n smallest elements from the dataset defined by iterable.从iterable定义的数据集中返回一个包含n个最小元素的列表。key, if provided, specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example,key(如果提供)指定一个参数的函数,用于从iterable中的每个元素中提取比较键(例如,key=str.lower
).key=str.lower
)。Equivalent to:等价于:sorted(iterable, key=key)[:n]
.sorted(iterable, key=key)[:n]
。
The latter two functions perform best for smaller values of n. 后两个函数对于较小的n值表现最好。For larger values, it is more efficient to use the 对于较大的值,使用sorted()
function. sorted()
函数更有效。Also, when 此外,当n==1
, it is more efficient to use the built-in min()
and max()
functions. n==1
时,使用内置的min()
和max()
函数更有效。If repeated usage of these functions is required, consider turning the iterable into an actual heap.如果需要重复使用这些函数,请考虑将iterable转换为实际堆。
Basic Examples基本示例¶
A heapsort can be implemented by pushing all values onto a heap and then popping off the smallest values one at a time:堆排序可以通过将所有值推送到堆上,然后一次弹出一个最小值来实现:
>>> def heapsort(iterable):
... h = []
... for value in iterable:
... heappush(h, value)
... return [heappop(h) for i in range(len(h))]
...
>>> heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
This is similar to 这类似于sorted(iterable)
, but unlike sorted()
, this implementation is not stable.sorted(iterable)
,但不同于sorted()
,此实现不稳定。
Heap elements can be tuples. 堆元素可以是元组。This is useful for assigning comparison values (such as task priorities) alongside the main record being tracked:这有助于在跟踪的主记录旁边分配比较值(例如任务优先级):
>>> h = []
>>> heappush(h, (5, 'write code'))
>>> heappush(h, (7, 'release product'))
>>> heappush(h, (1, 'write spec'))
>>> heappush(h, (3, 'create tests'))
>>> heappop(h)
(1, 'write spec')
Priority Queue Implementation Notes优先级队列实施说明¶
A priority queue is common use for a heap, and it presents several implementation challenges:优先级队列是堆的常见用法,它带来了几个实现挑战:
Sort stability: how do you get two tasks with equal priorities to be returned in the order they were originally added?排序稳定性:如何使优先级相同的两个任务按最初添加的顺序返回?Tuple comparison breaks for (priority, task) pairs if the priorities are equal and the tasks do not have a default comparison order.如果优先级相等且任务没有默认比较顺序,则(优先级,任务)对的元组比较中断。If the priority of a task changes, how do you move it to a new position in the heap?如果任务的优先级发生变化,如何将其移动到堆中的新位置?Or if a pending task needs to be deleted, how do you find it and remove it from the queue?或者,如果需要删除挂起的任务,如何找到它并将其从队列中删除?
A solution to the first two challenges is to store entries as 3-element list including the priority, an entry count, and the task. 前两个挑战的解决方案是将条目存储为三元素列表,包括优先级、条目计数和任务。The entry count serves as a tie-breaker so that two tasks with the same priority are returned in the order they were added. 条目计数用作平局破坏者,以便按添加顺序返回具有相同优先级的两个任务。And since no two entry counts are the same, the tuple comparison will never attempt to directly compare two tasks.由于没有两个条目计数相同,元组比较永远不会尝试直接比较两个任务。
Another solution to the problem of non-comparable tasks is to create a wrapper class that ignores the task item and only compares the priority field:不可比较任务问题的另一个解决方案是创建一个包装类,该类忽略任务项,只比较优先级字段:
from dataclasses import dataclass, field
from typing import Any
@dataclass(order=True)
class PrioritizedItem:
priority: int
item: Any=field(compare=False)
The remaining challenges revolve around finding a pending task and making changes to its priority or removing it entirely. 剩下的挑战围绕着找到一个挂起的任务,并更改其优先级或将其完全删除。Finding a task can be done with a dictionary pointing to an entry in the queue.查找任务可以使用指向队列中条目的字典来完成。
Removing the entry or changing its priority is more difficult because it would break the heap structure invariants. 删除条目或更改其优先级更为困难,因为这会破坏堆结构不变量。So, a possible solution is to mark the entry as removed and add a new entry with the revised priority:因此,一种可能的解决方案是将条目标记为已删除,并添加具有修订优先级的新条目:
pq = [] # list of entries arranged in a heap
entry_finder = {} # mapping of tasks to entries
REMOVED = '<removed-task>' # placeholder for a removed task
counter = itertools.count() # unique sequence count
def add_task(task, priority=0):
'Add a new task or update the priority of an existing task'
if task in entry_finder:
remove_task(task)
count = next(counter)
entry = [priority, count, task]
entry_finder[task] = entry
heappush(pq, entry)
def remove_task(task):
'Mark an existing task as REMOVED. Raise KeyError if not found.'
entry = entry_finder.pop(task)
entry[-1] = REMOVED
def pop_task():
'Remove and return the lowest priority task. Raise KeyError if empty.'
while pq:
priority, count, task = heappop(pq)
if task is not REMOVED:
del entry_finder[task]
return task
raise KeyError('pop from an empty priority queue')
Theory理论¶
Heaps are arrays for which 堆是针所有k的a[k] <= a[2*k+1]
and a[k] <= a[2*k+2]
for all k, counting elements from 0. a[k] <= a[2*k+1]
和a[k] <= a[2*k+2]
的数组,从0开始计算元素。For the sake of comparison, non-existing elements are considered to be infinite. 为了便于比较,不存在的元素被认为是无限的。The interesting property of a heap is that 堆的有趣特性是a[0]
is always its smallest element.a[0]
始终是其最小的元素。
The strange invariant above is meant to be an efficient memory representation for a tournament. 上面的奇异不变量是一种有效的锦标赛内存表示。The numbers below are k, not 下面的数字是k,而不是a[k]
:a[k]
:
0
1 2
3 4 5 6
7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
In the tree above, each cell k is topping 在上面的树中,每个单元格k位于2*k+1
and 2*k+2
. 2*k+1
和2*k+2
的顶部。In a usual binary tournament we see in sports, each cell is the winner over the two cells it tops, and we can trace the winner down the tree to see all opponents s/he had. 在体育界常见的二元锦标赛中,每个单元格都是其顶部两个单元格的获胜者,我们可以沿着树追踪获胜者,以查看他/她拥有的所有对手。However, in many computer applications of such tournaments, we do not need to trace the history of a winner. 然而,在这类锦标赛的许多计算机应用程序中,我们不需要追踪获胜者的历史。To be more memory efficient, when a winner is promoted, we try to replace it by something else at a lower level, and the rule becomes that a cell and the two cells it tops contain three different items, but the top cell “wins” over the two topped cells.为了提高内存效率,当一个获胜者被提升时,我们尝试用较低级别的其他内容替换它,规则是一个单元格及其顶部的两个单元格包含三个不同的项目,但顶部的单元格“获胜”于顶部的两个单元格。
If this heap invariant is protected at all time, index 0 is clearly the overall winner. 如果这个堆不变量始终受到保护,那么索引0显然是总的赢家。The simplest algorithmic way to remove it and find the “next” winner is to move some loser (let’s say cell 30 in the diagram above) into the 0 position, and then percolate this new 0 down the tree, exchanging values, until the invariant is re-established. 移除它并找到“下一个”赢家的最简单算法方法是将一些失败者(比如上图中的单元格30)移动到0位置,然后将这个新的0渗透到树中,交换值,直到重新建立不变量。This is clearly logarithmic on the total number of items in the tree. 这显然是树中项目总数的对数。By iterating over all items, you get an O(n log n) sort.通过对所有项进行迭代,可以得到O(n log n)
排序。
A nice feature of this sort is that you can efficiently insert new items while the sort is going on, provided that the inserted items are not “better” than the last 0’th element you extracted. 这种排序的一个很好的特性是,只要插入的项目不比提取的最后0个元素“更好”,就可以在排序过程中有效地插入新项目。This is especially useful in simulation contexts, where the tree holds all incoming events, and the “win” condition means the smallest scheduled time. 这在模拟环境中尤其有用,其中树包含所有传入事件,“赢”条件意味着最小的计划时间。When an event schedules other events for execution, they are scheduled into the future, so they can easily go into the heap. 当一个事件调度其他事件执行时,它们被调度到未来,因此它们可以很容易地进入堆。So, a heap is a good structure for implementing schedulers (this is what I used for my MIDI sequencer :-).因此,堆是实现调度程序的良好结构(这是我用于MIDI序列器的结构:-)。
Various structures for implementing schedulers have been extensively studied, and heaps are good for this, as they are reasonably speedy, the speed is almost constant, and the worst case is not much different than the average case. 已经对实现调度程序的各种结构进行了广泛的研究,堆对这一点很好,因为它们速度相当快,速度几乎恒定,最坏的情况与平均情况相差不大。However, there are other representations which are more efficient overall, yet the worst cases might be terrible.然而,总的来说,还有其他更有效的表示法,但最坏的情况可能很糟糕。
Heaps are also very useful in big disk sorts. 堆在大型磁盘排序中也非常有用。You most probably all know that a big sort implies producing “runs” (which are pre-sorted sequences, whose size is usually related to the amount of CPU memory), followed by a merging passes for these runs, which merging is often very cleverly organised 1. 你们很可能都知道,大排序意味着产生“运行”(这是预排序序列,其大小通常与CPU内存量有关),然后是这些运行的合并过程,合并过程通常组织得非常巧妙1。It is very important that the initial sort produces the longest runs possible. Tournaments are a good way to achieve that. 初始排序产生尽可能长的运行时间是非常重要的。锦标赛是实现这一目标的好方法。If, using all the memory available to hold a tournament, you replace and percolate items that happen to fit the current run, you’ll produce runs which are twice the size of the memory for random input, and much better for input fuzzily ordered.如果使用所有可用内存来举办比赛,您替换并筛选恰好适合当前跑步的项目,您将生成两倍于内存大小的跑步,用于随机输入,对于输入顺序模糊的情况更好。
Moreover, if you output the 0’th item on disk and get an input which may not fit in the current tournament (because the value “wins” over the last output value), it cannot fit in the heap, so the size of the heap decreases. 此外,如果您在磁盘上输出第0个项目,并获得一个可能不适合当前锦标赛的输入(因为值“赢”于最后一个输出值),则它无法适合堆,因此堆的大小减小。The freed memory could be cleverly reused immediately for progressively building a second heap, which grows at exactly the same rate the first heap is melting. 释放的内存可以立即被巧妙地重用,以逐步构建第二个堆,该堆的增长速度与第一个堆的融化速度完全相同。When the first heap completely vanishes, you switch heaps and start a new run. 当第一个堆完全消失时,切换堆并开始新的运行。Clever and quite effective!聪明而且相当有效!
In a word, heaps are useful memory structures to know. I use them in a few applications, and I think it is good to keep a ‘heap’ module around. :-)总之,堆是有用的记忆结构。我在一些应用程序中使用了它们,我认为保留一个“堆”模块很好
Footnotes
- 1
The disk balancing algorithms which are current, nowadays, are more annoying than clever, and this is a consequence of the seeking capabilities of the disks.目前流行的磁盘平衡算法与其说聪明,不如说恼人,这是磁盘搜索能力的结果。On devices which cannot seek, like big tape drives, the story was quite different, and one had to be very clever to ensure (far in advance) that each tape movement will be the most effective possible (that is, will best participate at “progressing” the merge).对于无法搜索的设备,如大型磁带机,情况完全不同,人们必须非常聪明,以确保(远远提前)每个磁带移动都尽可能有效(即,最好参与“推进”合并)。Some tapes were even able to read backwards, and this was also used to avoid the rewinding time.有些磁带甚至可以倒读,这也是为了避免倒带时间。Believe me, real good tape sorts were quite spectacular to watch!相信我,真正好的磁带种类是相当壮观的观看!From all times, sorting has always been a Great Art! :-)从古至今,排序一直是一门伟大的艺术!:-)